In this recipe, we will show you how to do data backups, on a regular basis, that will take snapshots of some of your system's directory using the crond
daemon. This will run the rsync
program at regular intervals to implement a fully automated backup solution.
To complete this recipe, you will require a working installation of the CentOS 7 operating system with root privileges and a console-based text editor of your choice. It is also advantageous if you have read the Synchronizing files and doing more with rsync and Scheduling tasks with cron recipes in this chapter to get a deeper understanding of used commands.
It's important to install the rsync
program on your server before proceeding with this recipe.
mkdir /backups
mkdir ~/bin;vi ~/bin/mybackup.sh
/backups
in the environment variable DEST
and SOURCE
with the one you would like to backup as well as the recipient's EMAIL
:#!/bin/bash SBJT="cron backup report for `hostname -s` from $(date +%Y%m%d:%T)" FROM=root@domain [email protected] SOURCE=/root DEST=/backups LFPATH=/tmp LF=$LFPATH/$(date +%Y%m%d_%T)_logfile.log rsync --delete --log-file=$LF -avzq $SOURCE $DEST (echo "$SBJT"; echo; cat $LF ) | sendmail -f $FROM -t $EMAIL
chmod a+x /root/bin/mybackup.sh
crontab
using:crontab -e
30 20 * * * /root/bin/mybackup.sh
In this recipe, we have created a full automatic backup solution for a single system directory, which will create a snapshot of the files at a certain time point. At the time the backup process is complete you will receive an e-mail informing you that a backup has been made with a brief review of the actions taken.
So what did we learn from this experience?
We started this recipe by creating a directory where our backup will be placed. Next we created the actual script and filled it with some commands. Line 1 defines the file as a bash script, lines 2-6 are variables you can modify and customize to fit your own needs. lines 7-8 create a path and name for the log file based on the date, and line 9 calls rsync
which will synchronize all our source files to the target directory /backups. It uses a special --log-file
parameter which writes all output to the given file. The final line (10) sends the content of this log file to an email address.
Remember, you should customize the values as required (that is, change the e-mail address used, select a source directory, and choose a destination directory, and so on.). Before it can be used and executed by cron
, we made it executable. Finally, we added this script as a cron job to run on a daily schedule at 20:30 hours. However, as this may be some hours away, if you would like to test your script right now, you can execute it on the command line using the following:
/root/bin/mybackup.sh
In conclusion, it will go without saying that a backup should be located on an external drive or on a separate partition, but having completed this introduction I think you will agree that rsync
is ideally positioned in such a way that it will enable any server administrator to develop their own policy with regard to maintaining an effective backup of important data.