Restartable RSYNC and CP – Loops until completion

Version 1 of this can be found here: version1 (note version2 isnt necessarily better, its just shorter)

This is another version of a restartable RSYNC and CP script. This variation is in one line. You can edit the commands to meet your needs.

The commands loop by looking at the exit code of cp or rsync. If one of those exit(or crash) with an exit code other then 0 (0 meaning successful run), then the script will repeat the cp or rsync command.

RSYNC VARIATION

### RSYNC LOOP TILL SUCCESS - 1 LINE ###

logs=/mnt/databackup.log; source=/maindata/; destination=/mnt/backup/; N=0; while true; do echo "### ---- rsync ${source} to ${destination} - logs at ${logs}${N} - try ${N} ---- ###"; rsync -avhP --log-file=${logs}${N} ${source} ${destination}; result=$?; if [ $result -eq 0 ]; then echo "### ---- rsync success complete - backup complete ---- ###"; break; else echo "### ---- restarting rsync in 5 seconds - incrementing retry count ---- ###"; N=$((N+1)); sleep 5; fi; done;

You can test the command before running the big loop like so:

### RSYNC TEST COMMAND ###

logs=/mnt/databackup.log; source=/maindata/; destination=/mnt/backup/; rsync -avhP --log-file=${logs}${N} ${source} ${destination}

If the above loop script (not the test command) is confusing and you want to see it with newline characters for prettier more human readable output here it is:

### RSYNC LOOP TILL SUCCESS - PRETTY MULTILINE ###

logs=/mnt/databackup.log;
source=/maindata/;
destination=/mnt/backup/;
N=0;
while true; do
 echo "### ---- rsync ${source} to ${destination} - logs at ${logs}${N} - try ${N} ---- ###";
 rsync -avhP --log-file=${logs}${N} ${source} ${destination};
 result=$?;
 if [ $result -eq 0 ]; then
  echo "### ---- rsync success complete - backup complete ---- ###";
  break;
 else
  echo "### ---- restarting rsync in 5 seconds - incrementing retry count ---- ###";
  N=$((N+1));
  sleep 5;
 fi;
done;

 CP VARIATION

### CP LOOP TILL SUCCESS - 1 LINE ###

logs=/mnt/databackup.log; source=/maindata/; destination=/mnt/backup/; N=0; while true; do echo "### ---- cp ${source} to ${destination} - logs at ${logs}${N} - try ${N} ---- ###"; cp -rvp ${source} ${destination} | tee ${logs}${N}; result=$?; if [ $result -eq 0 ]; then echo "### ---- cp success complete - backup complete ---- ###"; break; else echo "### ---- restarting cp in 5 seconds - incrementing retry count ---- ###"; N=$((N+1)); sleep 5; fi; done;

You can test cp like this before running the above command:

### CP TEST COMMAND ###

logs=/mnt/databackup.log; source=/maindata/; destination=/mnt/backup/; cp -rvp ${source} ${destination} | tee ${logs}${N}

If the above loop script (not the test command) is confusing and you want to see it with newline characters for prettier more human readable output here it is:

### CP LOOP TILL SUCCESS - PRETTY MULTILINE ###

logs=/mnt/databackup.log;
source=/maindata/;
destination=/mnt/backup/;
N=0;
while true; do
 echo "### ---- cp ${source} to ${destination} - logs at ${logs}${N} - try ${N} ---- ###";
 cp -rvp ${source} ${destination} | tee ${logs}${N};
 result=$?;
 if [ $result -eq 0 ]; then
  echo "### ---- cp success complete - backup complete ---- ###";
  break;
 else
  echo "### ---- restarting cp in 5 seconds - incrementing retry count ---- ###";
  N=$((N+1));
  sleep 5;
 fi;
done;

Leave a Reply

Your email address will not be published. Required fields are marked *