Scripts
Posted on Jan 08, 2011 at 08:45
Backups - Rsync: A Simple Bash Script
Download File
rsync2store.sh
Explanation
This is a very simple and straight forward rsync script I put together to backup data from one server to another. The main rsync command is displayed below and give's a good standard reference to the command's usage. A special thanks goes out to Jason Fink for supplying the bomb() function included in the script.
Rsync command in script: |
rsync -atvze ssh --rsync-path='sudo rsync' $LOCAL_BACKUP steved@blackbox:$REMOTE_BACKUP || bomb "Could not sync ${LOCAL_BACKUP} to ${REMOTE_BACKUP}"
This rsync command can be broken down as follows:
-atvze ssh: This tells rsync to execute in archive mode [a], preserve timestamps [t], increase verbosity [v], compress the data during transfer [z] and specify the remote shell to use for the transfer [e] which is specified as ssh.
--rsync-path='sudo rsync': This is the rsync command to run on the remote server. Here we are saying to run rsync as the sudo user on the remote machine. This is necessary if you are storing data on a device that requires sudo permissions.
$LOCAL_BACKUP: This is the variable set in the script that specifies the local directory to backup.
steved@blackbox:$REMOTE_BACKUP: Here we are specifying the [user]@[hostname] to use for logging in under ssh at the remote box. $REMOTE_BACKUP is the variable set in the script that specifies the remote directory to send the data to we are backing up.
|| bomb "Could not sync ${LOCAL_BACKUP} to ${REMOTE_BACKUP}": In the event the rsync command fails this "OR" (double pipe) condition will kick in that displays a simple error message and then ends the process.