fbpx CentOS backup and taking snapshots
24/7/365 Support

CentOS backups and taking snapshots

In this process, 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 Start With: What Do You Need?

To complete this process, 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 process to get a deeper understanding of used commands.

The Process

It’s important to install the rsync program on your server before proceeding with this process.

  1. First, log in as root and create a directory where our backups will land:
    mkdir /backups
  2. Now, we will create the following shell script file and open it for editing:
    mkdir ~/bin;vi ~/bin/mybackup.sh
  3. Put in the following content, replacing /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=johndoe@internet.com
    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
  4. Make the script executable:
    chmod a+x /root/bin/mybackup.sh
  5. Now, open crontab using:
    crontab -e
  6. Next, create the following entry by adding the following line to the end of the document, then save and close it:
    30 20 * * * /root/bin/mybackup.sh

How it works…

In this process, we have created a fully 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 process 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.

 

Help Category:

What Our Clients Say