Backup script data from Remote Server to Local System

Backup Script

Importance of Backup

Backup of all relevant data and files on time is the best quality of good sysadmin.
In this article, I have provided you a backup script which helps you to backup your valuable data from remote server to local system.
If your server crashed, we have a copy of the backup file on the local machine which we used as an option to restoring a website.
Also, I have explained you each line of code mentioned in this script which gives you a deeper understanding of working with the script.

Backup Script

Just copy below script on your local system and save file then give the execute permission for file.

#!/bin/bash

#############################################################
# Title                : CodeBackups.sh                                                                                  #
# Description   : Backup code base from Remote Server to Local System        # 
# Author            : PRASHANT THORAT                                                                         #
# Date                : 23 AUG 2016                                                                                           #
##############################################################
#Variables define to store commands and values.

TODAY_DATE=$(date +%Y%m%d) #Store current date
MKDIR=$(which mkdir)    #store mkdir command
RSYNC=$(which rsync)    #store rsync command
DEL=$(which rm)             #store rm command
TAR=$(which tar)             #store tar command
MAIL=$(which mail)         #store mail command
LOGFILE='/tmp/ProductionCodebase.log' #store path of log file & touch this file in /tmp directory

#Add Email id in mailto for getting notifications in mail

MAILTO='youremail@id.com'
BODY="nnFor more information please check $LOGFILE.nnnThanks,n$0"

#Create Directory to store backup files

PRODBACKDIR=$TODAY_DATE/production-backup/codebase
BLOGBACKDIR=$TODAY_DATE/production-backup/blog_codebase

#if condition check directory is present or not and if not present create directory with provided dir structure.

if [ ! -d "{$PRODBACKDIR}" ] && [ `$MKDIR -p "$PRODBACKDIR"` ];
then
echo -e "Directory Created for Production Codebase";  
fi

if [ ! -d "{$BLOGBACKDIR}" ] && [ `$MKDIR -p "$BLOGBACKDIR"` ];
then
echo -e "Directory Created for Production Blog Codebase";  
fi

#Remove backup files 7 days older

find /path-of-backup-dir/ -mtime +7 -exec rm -rf {} ;

#Rsync command  to sync code or data from remote server to local using sshpass to pass password of remote server

sshpass -p 'password' $RSYNC --progress -avz -e ssh user@remote-server-ip:/var/www/html $PRODBACKDIR

#For loop to check backup files and compressed it with .tar.gz file which saves disk space on local system and also remove non compressed files.

for DOC in $PRODBACKDIR/html; do
echo "`date '+%Y-%m-%d'` : ${DOC} backup started";
if [ -d ${DOC} ]; then
          FILENAME=$(echo $DOC | awk -F '/' '{print $NF}');
          TARFILE="$FILENAME-$TODAY_DATE.tar.gz";
          echo "`date '+%Y-%m-%d'` : ${DOC} archiving started.";
          $TAR -cvzf $PRODBACKDIR/$TARFILE $PRODBACKDIR/$FILENAME;  

#if condition check errors while creating tar.gz and report if tar command fails.

if [ $? -eq 0 ]; then
                       echo "`date '+%Y-%m-%d'` : ${DOC} archiving done."
                       $DEL -rf $PRODBACKDIR/$FILENAME
                       echo "`date '+%Y-%m-%d'` : ${DOC} deleting of non-archive directory done."
               else
                       echo "Error while creating tar."
                       echo -e "Error while creating tar file.$BODY" | $MAIL -s 'Error : Creating tar file' $MAILTO
                       exit 1
               fi
       fi
       echo "`date '+%Y-%m-%d %H:%M:%S'` : ${DOC} backup completed"
done

#Rsync command  to sync another code or data from remote server to local using sshpass to pass password of remote server

sshpass -p 'password' $RSYNC --progress -avz -e ssh user@emote-server-ip:/var/www/blog/  $BLOGBACKDIR

#For loop to check backup files and compressed it with .tar.gz file which saves disk space on local system and also remove non compressed files.

for BLOG in $BLOGBACKDIR/hdfc_blog; do
echo "`date '+%Y-%m-%d'` : ${BLOG} backup started";
if [ -d ${BLOG} ]; then
          FILENAME=$(echo $BLOG | awk -F '/' '{print $NF}');
          TARFILE="$FILENAME-$TODAY_DATE.tar.gz";
          echo "`date '+%Y-%m-%d'` : ${BLOG} archiving started.";
          $TAR -cvzf $BLOGBACKDIR/$TARFILE $BLOGBACKDIR/$FILENAME;  

#if condition check errors while creating tar.gz and report if tar command fails.

if [ $? -eq 0 ]; then
                       echo "`date '+%Y-%m-%d'` : ${DOC} archiving done."                       
                       $DEL -rf $BLOGBACKDIR/$FILENAME
                       echo "`date '+%Y-%m-%d'` : ${DOC} deleting of non-archive directory done."
               else
                       echo "Error while creating tar."
                       echo -e "Error while creating tar file.$BODY" | $MAIL -s 'Error : Creating tar file' $MAILTO
                       exit 1
               fi
       fi
       echo "`date '+%Y-%m-%d %H:%M:%S'` : ${BLOG} backup completed"
done

#Send the notification email after successful completion of backup.

echo -e "Backups successfully done for Production codebase" | $MAIL -s "Completed : Backup of Production Codebase & Blog Seccessfully Done." $MAILTO

Note : Remove comment from script which is red color before use.

Save above script and give execute permission.

  • chmod +x  script.sh

Test Script

  • bash -x script.sh

Cron Schedule

After a successful test of backup script. We will plan our backup script to execute at a particular time.

To schedule backup script. create file under the /etc/cron.d/ directory and set your cron.

vim /etc/cron.d/CodeBackups
#Add cron like below
00 23 * * * root /bin/bash /opt/scripts/script.sh  > /tmp/CodeMediaBackups.log 2>&1

If you’re facing any difficulties while configuring backup script please, do comment your queries or problem via the comment section, till then stay tuned to techthings.org for more such valuable articles.

Useful Scripts :

Latest Comments
  1. LALBAHADUR VISHWAKARMA September 27, 2016

Leave a Reply

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