Nothing really fancy here! I just wrote a simple rsync script which can be used to do a mirror of a set of directories from one place to the other. I use this to do incremental mirror of local Debian repositories from my machine, to my external USB hard-disk, so that I can give it to somebody who needs the entire Debian repo.
Of course, it’s application is not limited to copying just a single directory but, you can have an entire system backup by giving space separated list of directories to mirror. This script creates a directory corresponding to the basename of each of the source directories. For example, if the source directory is /opt/mystuff/projects and the destination root directory(named as RTARGET in the script) is /media/disk/backup, the script will create a directory called ‘projects’ under /media/disk/backup and mirrors the contents from /opt/mystuff/projects to /media/disk/backup/projects.
Note: This script comes with absolutely no warranty of any kind. You may use it at your own risk :-) Remember to change the values of SOURCES and RTARGET according to your need. You can save the shell script below into a file called mbakdir.sh and place it in /usr/local/sbin directory. Don’t forget to set its executable bits (chmod 755).
Of course, it’s application is not limited to copying just a single directory but, you can have an entire system backup by giving space separated list of directories to mirror. This script creates a directory corresponding to the basename of each of the source directories. For example, if the source directory is /opt/mystuff/projects and the destination root directory(named as RTARGET in the script) is /media/disk/backup, the script will create a directory called ‘projects’ under /media/disk/backup and mirrors the contents from /opt/mystuff/projects to /media/disk/backup/projects.
Note: This script comes with absolutely no warranty of any kind. You may use it at your own risk :-) Remember to change the values of SOURCES and RTARGET according to your need. You can save the shell script below into a file called mbakdir.sh and place it in /usr/local/sbin directory. Don’t forget to set its executable bits (chmod 755).
#!/bin/sh
# mbakdir.sh -- secure local backup using rsync.
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
# Directories to backup. Separate with a space.
#Exclude trailing slash!
SOURCES="/opt/mystuff1 /opt/mystuff2"
# Directory to backup to on the remote machine.
#This is where your backup(s) will be stored
# Exclude trailing slash!
RTARGET="/media/disk/backup"
echo "Verifying Sources..."
for source in $SOURCES; do
echo "Checking $source..."
if [ ! -x "$source" ]; then
echo "Error with $source!"
echo "Directory either does not exist, or \
you do not have proper permissions."
exit 2
fi
done
echo "Sources verified. Running rsync..."
RBAKOPTS="--delete -zavl"
# Now, do current week's incremental backup
for source in $SOURCES; do
mkdir -p $RTARGET/$(basename $source)
rsync $RBAKOPTS $source/ $RTARGET/$(basename $source)/
done
exit 0