Check If Rsync Command Ran Successful

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

  1. You could use rsync instead. rsync -r -z -c will return better error codes.

  2. 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.


  1. 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



Leave a reply



Submit