Bash script to Copy Backup data from Remote Server to Local System Linux

copy backup data
copy backup data

Remote Server To Local System Backup Script

Importance of Backup

In this article, I have shared with you a backup script that helps you to copy backup data from a remote server to the local system.

Backup of all important data and files every time is the best quality of Sysadmin.

If your server crashed then we have a copy backup data file on the local system as well which we used as an option to restoring an application.

Also, I have explained to you each line of code mentioned in this script which gives you a deeper understanding of the shared script file.

The backup script is useful for the ones who are not using any other third-party backup options. 

I have also written some backup scripts which help you to copy backup data directly to the S3 Cloud.

For that please refer below the AWS S3 backup script.

Read: AWS S3 Backup Script

Backup Script

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

#!/bin/bash

##################################################################
# Title         : CodeBackup.sh                     #
# Description   : Backup data from Remote Server to Local System #
##################################################################

#Variables define to store commands and values.

TODAY_DATE=$(date +%Y%m%d) #Store current date
MKDIR=$(which mkdir)    #store mkdir command
RSYNC=$(which sync)      #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='[email protected]'
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 [email protected]:/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 [email protected]:/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

Save the above script and give execute permission. Bash script always has executed permission to run the script. 

$ chmod +x  script.sh

Test Script

You test the script with the bash command along with the X parameter. X parameter will display the each step that we are defined in the bash 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 a 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 on your queries or problem via the comment section, till then stay tuned to Linuxgrow.com for more such valuable articles.

Some More Backup Scripts…

  • Mysql Database Backup Script
  • Amazon S3 backup script

Prashant

Welcome to Linuxgrow. I'm Prashant, a tech-blogger from Mumbai, India. I started Linuxgrow as a passion and to share my knowledge about technologies. Here at Linuxgrow, I write about Linux technologies, Aws Cloud, Wordpress blogging and scripting knowledge. You can read more about me at About us page. Thank You :)

You may also like...

Leave a Reply

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

Facebook