Script to take backup of code in Bash

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/

#!/bin/bash

DATE=$(date +%F)
LOGFILE=/home/admin/Archive/LOGFILE
LOG=$LOGFILE/backup-$DATE.log
COMPLETE_CODE=/home/admin/Archive/code/$DATE

#####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" helpdesk@techoism.com

Enjoy it!

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.