This is how to blog, Create Backup of Folder and keep compress using Tar, Send backup report over E-mail in Linux.
For a System Administrator is required to keep the backup of Data to avoid unnecessary recovery or Disaster.
Requirement -
1- Linux box - In my scenario, I am using Centos 6.4 final release.
2- TAR utility.
3- SSMTP Utility.
Step 1-
Setup E-mail Client so that backup report can forward to an e-mail address. We are using SSMTP for sending the email.
Please review my Blog- How to setup ssmtp email client to send email from gmail account.
Step 2 -
Create Backup destination directory or Mount NAS. In my scenario, I am using my local / root partition for demonstration.
[root@cent ~]# mkdir /backupdir
Step 3 -
In my scenario, We are taking backup of /home/amar name directory.
Create #!/bin/bash Script to create backup and email report.
Create Script file using Vi editor
[root@cent backupdir]# vi backup.sh
Append below syntax to script.
#!/bin/bash
# Add your backup dir location, and tar commmand path
DATE=`date +%Y_%m_%d_%H_%M`
# Backup destination to save user data
BACKUP_DIR="/backupdir"
# Give tar command path (you can find using command (find / -name tar))
tar=/bin/tar
# Path of ssmtp command
ssmtp=/usr/sbin/ssmtp
# To create a new directory into backup directory location
mkdir -p $BACKUP_DIR/$DATE
# define command to take bakcup of folder
$tar -zvcf $BACKUP_DIR/$DATE/amar.tar.gz /home/amar/
#Do ls and append backup content into a log file
/bin/ls -l $BACKUP_DIR/$DATE | cat >>$BACKUP_DIR/$DATE/$DATE.log
#Change email ID and log dir path to send report
echo "Backup" | $ssmtp -vvv amar.icreon@gmail.com <$BACKUP_DIR/$DATE/*.log
# Change number of days to keep backup
find $BACKUP_DIR/* -type d -ctime +2 -exec rm -rf {} \;
Save and Exit from Script
Step 4 -
Make Script Executable.
[root@cent backupdir]# chmod a+x backup.sh
Step 5 -
Run Script to check test - If everything goes well output should like this -
[root@cent backupdir]# ./backup.sh
/bin/tar: Removing leading `/' from member names
/home/amar/
/home/amar/public/
/home/amar/public/phpinfo.php
/home/amar/public/index.php
/home/amar/.bash_history
/home/amar/logs/
/home/amar/logs/access_log
/home/amar/logs/error.log
/home/amar/.bashrc
/home/amar/.bash_logout
/home/amar/.bash_profile
[<-] 220 smtp.gmail.com ESMTP ro14sm473268pab.32 - gsmtp
[->] EHLO gmail.com
[<-] 250 SMTPUTF8
[->] STARTTLS
[<-] 220 2.0.0 Ready to start TLS
[->] EHLO gmail.com
[<-] 250 SMTPUTF8
[->] AUTH LOGIN
[<-] 334 VXNlcm5hbWU6
[->] bWV0cm9sYW5kYW1hckBnbWFpbC5jb20=
[<-] 334 UGFzc3dvcmQ6
[<-] 235 2.7.0 Accepted
[->] MAIL FROM:<root@gmail.com>
[<-] 250 2.1.0 OK ro14sm473268pab.32 - gsmtp
[->] RCPT TO:<amar.icreon@gmail.com>
[<-] 250 2.1.5 OK ro14sm473268pab.32 - gsmtp
[->] DATA
[<-] 354 Go ahead ro14sm473268pab.32 - gsmtp
[->] Received: by gmail.com (sSMTP sendmail emulation); Thu, 11 Aug 2016 11:22:26 +0530
[->] From: "root" <root@gmail.com>
[->] Date: Thu, 11 Aug 2016 11:22:26 +0530
[->] total 4
[->] -rw-r--r-- 1 root root 0 Aug 11 11:22 2016_08_11_11_22.log
[->] -rw-r--r-- 1 root root 2436 Aug 11 11:22 amar.tar.gz
[->]
[->] .
[<-] 250 2.0.0 OK 1470894752 ro14sm473268pab.32 - gsmtp
[->] QUIT
[<-] 221 2.0.0 closing connection ro14sm473268pab.32 - gsmtp
[root@cent backupdir]#
Step 6 -
Let's Check email for the backup report, Login to you Gmail account.
You will get new email having details of backup Directory.
We have successfully configured Backup of folder as compress and report forwarded to email.
That's All
!!!Cheers!!!
No comments:
Post a Comment