anrpry anrpry - 20 days ago 7
Linux Question

Rerun Rsync After Unsuccessful Connection in Cron

I have a bash script in my

cron
that has a passwordless
rsync
command to pass files from my local system to a web server.

Bash Script Code:

rsync -avzhe "ssh -p2222" ---chmod=Du=rw,Dg=r,Do=r,Fu=rw,Fg=r,Fo=r -p /home/sysadmin/some_file_{0..10}.png username@web.server:public_html/some.directory/


Over the past few days, I have noticed that the connection is randomly refused sometimes. I have correctly set up
openssh-client
and
openssh-server
and have set up the password
ssh
login successfully so, I'm not sure what is causing the connection to randomly be refused sometimes.

Now, I am looking for some code to force the
rsync
code to rerun until the files are successfully passed to the web.

Rsync Code:

RC=1
while [[ $RC -ne 0 ]]
do
rsync -avzhe "ssh -p2222" ---chmod=Du=rw,Dg=r,Do=r,Fu=rw,Fg=r,Fo=r -p /home/sysadmin/some_file_{0..10}.png username@web.server:public_html/some.directory/
RC=$?
done


Is this the best method to try and circumvent the issue?

Answer

You don't need to store the return code of rsync, just do a loop like

#!/bin/bash

until rsync -avzhe "ssh -p2222" ---chmod=Du=rw,Dg=r,Do=r,Fu=rw,Fg=r,Fo=r -p /home/sysadmin/some_file_{0..10}.png username@web.server:public_html/some.directory/; do
  sleep 5              # waiting for 5 seconds before re-starting the command.
done
Comments