Black God

HOWTO: Damn simple backup script in Linux based on rsync and NFS share

M Home network setup is like this: A small NAS Box from QNAP (TS-110) powered by Debian (Here is how I setup this server) connected with network along with my laptop and Home PC. Laptop is powered by Linux Mint and Home PC by Ubuntu 11.10. I need to take backup of both the machines in regular fashion. But being a lazy guy, I thought of automating it without any special additional applications.

Here the idea is to share a directory in backup server through NFS standard. This share mounted in workstations with right permissions. rsync is a wonderful file synchronization tool, which can take differential backup to make the backup less time consuming and efficient. In workstations we will configure a small script to automate it this backup to mounted NFS share.

1. NFS Share setup

I have installed and configured NFS share in Server. You need to install nfs-common, portmap and nfs-kernel-server packages. Here is how to do it. Here the share directory is your backup destination planned.

2. rsync backup script

Client means the workstation to be backup. You need to have rsync in this machine. In my case the client machines are Debian based linux distros, which comes with rsync preinstalled. The below procedure will call a backup script whenever you shutdown (not reboot) the workstation. So that every time you shutdown the workstation, backup will be in sync.

Step-1: Create a file called backup.sh as shown below in /etc/init.d directory.

# Initializing init variables
. /lib/init/vars.sh
. /lib/lsb/init-functions

echo -n “Backup initiated….” >> /root/backup.log
date >> /root/backup.log

if [ `mount | grep -c "/media/backup"` = 1 ]
then

log_action_begin_msg “Backup the data on NFS mount point before unmount”

/usr/bin/rsync -avH –delete –omit-dir-times /home/* /media/backup >> /root/backup.log 2>&1

else

echo “Destination /media/backup not mounted! Skipping backup…!”

fi

log_action_end_msg 0

The above script runs rsync command with the following options:

-a,  The  files  are  transferred in “archive” mode, which ensures that sym‐       bolic links, devices, attributes,  permissions,  ownerships,  etc.  are       preserved  in  the transfer.

-v, increase verbosity

-H, preserve hard links

–delete, this will ensure that the files are deleted in server also if it is deleted in workstation

–omit-dir-times, directories are excluded from preserving modification times. Without this setting you may get the following error.

rsync: failed to set times on "/mnt/NAS-Box": Operation not permitted (1) rsync error: some files could not be transferred (code 23)

–exclude, the pattern of files are excluded from backup

In case of Red Hat Enterprise Linux or CentOS, I use the following script instead of above one.

echo -n “Backup initiated….” >> /root/backup.log
date >> /root/backup.log

if [ `mount | grep -c "/media/backup"` = 1 ]
then

echo -n “Backup the data on NFS mount point before unmount….”

/usr/bin/rsync -avH –delete –omit-dir-times –exclude “.*” /home/blackgod /media/backup >>/root/backup.log 2>&1

echo “Done!”

else

echo “Destination /media/backup not mounted! Skipping backup…!”

fi

Step-2: Create a symbolic link to above script in /etc/rc0.d, which is having scripts for shutdown run level, so that your script is called automatically during shutdown.

# cd /etc/rc0.d

# ln -s ../init.d/backup.sh S01mbackup

That is it. You are done. If you want to do manual backup, just run /etc/init.d/backup.sh. You logs are directed in the file mentioned in the above script.

 

1 comment for “HOWTO: Damn simple backup script in Linux based on rsync and NFS share

Leave a Reply

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