Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if rsync command ran successful

The following bash-script is doing a rsync of a folder every hour:

#!/bin/bash
rsync -r -z -c /home/pi/queue [email protected]:/home/foobar
rm -rf rm /home/pi/queue/*
echo "Done"

But I found out that my Pi disconnected from the internet, so the rsync failed. So it did the following command, deleting the folder. How to determine if a rsync-command was successful, if it was, then it may remove the folder.

like image 850
user1670816 Avatar asked Jun 27 '14 14:06

user1670816


People also ask

How do I know if rsync is completed?

Method 1: Using –progress option to see the rsync progress:Use the “–progress” in the rsync command and “-av” to get a summary at the end of file transfer, consisting of transfer rate, sent/receive bytes, speed of transfer, and total file size.

What is the command to check rsync?

Show rsync Progress During Data TransferAdd the --progress flag to the rsync command to view the amount of data transferred, transfer speed, and the remaining time.

How do I know if rsync is installed on Linux?

Chances are that you already have it: rsync is built-in with Linux and macOS. Check if it is installed. Run this command in the Terminal of your local machine: rsync --version # If installed, it will output the version number.

Does rsync ignore existing files?

Rsync with --ignore-existing-files: We can also skip the already existing files on the destination. This can generally be used when we are performing backups using the –link-dest option, while continuing a backup run that got interrupted. So any files that do not exist on the destination will be copied over.


2 Answers

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 [email protected]:/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 [email protected]:/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

like image 134
Benjamin Sonntag Avatar answered Sep 28 '22 02:09

Benjamin Sonntag


you need to check the exit value of rsync

#!/bin/bash
rsync -r -z -c /home/pi/queue [email protected]:/home/foobar
if [[ $? -gt 0 ]] 
then
   # take failure action here
else
   rm -rf rm /home/pi/queue/*
   echo "Done"
fi

Set of result codes here: http://linux.die.net/man/1/rsync

like image 21
dethorpe Avatar answered Sep 28 '22 02:09

dethorpe