Check if rsync command ran successful
Usually, any Unix command shall return 0 if it ran successfully, and non-0 in other cases.
Look at man rsync for exit codes that may be relevant to your situation, but I'd do that this way :
#!/bin/bash
rsync -r -z -c /home/pi/queue root@server.mine.com:/home/foobar && rm -rf rm /home/pi/queue/* && echo "Done"
Which will rm and echo done only if everything went fine.
Other way to do it would be by using $? variable which is always the return code of the previous command :
#!/bin/bash
rsync -r -z -c /home/pi/queue root@server.mine.com:/home/foobar
if [ "$?" -eq "0" ]
then
rm -rf rm /home/pi/queue/*
echo "Done"
else
echo "Error while running rsync"
fi
see man rsync, section EXIT VALUES
Copy or rsync command
Rsync is better since it will only copy only the updated parts of the updated file, instead of the whole file. It also uses compression and encryption if you want. Check out this tutorial.
Bash scripting rsync: rsync: link_stat (blah) failed: No such file or directory (2)
Check that your \
characters have no whitespace after them at the end of the line. This will cause BASH to not interpret the line wrap correctly, giving the rsync error above.
SCP copy from another host if file not found in one host
You could use
rsync
instead.rsync -r -z -c
will return better error codes.You could use
sshpass
to automatically enter a password
Then you could..
if sshpass PA55WORD rsync -r -z -c "username@host1:/tmp/$1*" ~/$1;
then echo "done";
else sshpass PASSWORD rsync -r -z -c "username@host2:/tmp/$1*" ~/$1;
fi
I would personally recommend against typing a password in plain text. ssh private keys are useful for this exact problem. You could use sshpass -f FILE_PATH
or some other clever encryption method of getting the password in, but sshpass
is your friend here.
- For multiple hosts, all with the same username and password:
HOSTS=( "host1" "host2" "host3" )
for H in ${HOSTS[@]}
do
sshpass -p PASSWORD rsync -r -z -c "username@${H}:/tmp/$1*" ~/$1 && break
done
For multiple hosts, all with different usernames and passwords
HOSTS=( "host1" "host2" "host3" )
USERS=( "user1" "user2" "user3" )
PASSS=( "pass1" "pass2" "pass3" )
for i in ${!HOSTS[@]}
do
sshpass -p ${PASSS[$i]} rsync -r -z -c "${USERS[$i]}@${HOSTS[$i]}:/tmp/$1*" ~/$1 && break
done
rsync error: failed to set times on /foo/bar: Operation not permitted
If /foo/bar
is on NFS (or possibly some FUSE filesystem), that might be the problem.
Either way, adding -O
/ --omit-dir-times
to your command line will avoid it trying to set modification times on directories.
rsync connection refused error
Ok, I've done. It was simply, just gain super user privilege and then enable and start rsync process:
sudo su
enter your password, then digit
systemctl enable rsync
systemctl start rsync
if you don't have a systemctl based terminal just use "service" instead.
service rsync restart
You can check that rsync is now working by accessing to /var/log/rsyncd.log. The bind() error is now gone.
Related Topics
Archiving Hidden Directories with Tar
Fast Concatenate Multiple Files on Linux
Using Assertion in the Linux Kernel
How to See Top Processes Sorted by Actual Memory Usage
How Much Memory Is Consumed by the Linux Kernel Per Tcp/Ip Network Connection
Linux's Thread Local Storage Implementation
Device Tree Compiler Not Recognizes C Syntax for Include Files
How to Programmatically Create Videos
How to Check If Hadoop Daemons Are Running
Apt-Get Error: Sub-Process /Usr/Bin/Dpkg Returned an Error Code (1)
Convert Utf8 to Utf16 Using Iconv
Replace Whitespaces with Tabs in Linux