Script to take backup of code in Bash

Share It!

One of the easiest ways to backup a system is using a shell script. Case in point, script can be used to configure which directories to backup, and pass those directories as contentions to the tar utility, which makes a document record. The document record can then be moved or replicated to another area.

Backup of code

The tar utility makes one document record out of numerous records or indexes. tar can also filter the files through compression utilities, consequently decreasing the measure of the file record.

This tutorial will help you to take a backup of your website code directory and also send email when backup has been completed.

Simple Shell Script

Backup Location: /home/admin/Archive/code
LogFile: /home/admin/Archive/LOGFILE
Code Location: /usr/local/apache2/htdocs/


DATE=$(date +%F)

#####Creating Directory
mkdir -p '/home/admin/Archive/code/'$DATE
chmod -R 777 /home/admin/Archive/code/$DATE
cd /usr/local/apache2/htdocs/

#####Backup of code on Server
tar cvzf $COMPLETE_CODE/techosim.tar.gz techosim

## Backup Status
echo -e "===========Backup Status============\n" > $LOG
echo -e "Backup Successful of Code on Server\n " >> $LOG
Subject="Successfully Server Backup :: (${DATE})"
echo -e "====================================\n" >> $LOG
## Check Size of Backup Status
echo "==============Size of Backup=============" >> $LOG
du -sh /home/admin/Archive/code/$DATE/* >> $LOG
echo -e "====================================\n" >> $LOG

cat $LOG | mail -s "$Subject"

Enjoy it!

Leave a Reply

Your email address will not be published.