[root@linux011 ~]# vim backup_script.sh
1
|
#!/bin/bash
|
1
|
#Please note - This script tested on Centos-6,7/Redhat-6,7.
|
1
|
#Check Internet Connection
|
1
|
IS=`/bin/ping -c 5 4.2.2.2 | grep -c "64 bytes"`
|
1
|
if (test "$IS" -gt "2") then
|
1
|
internet_conn="1"
|
1
|
#Check Gdrive Software Install Or Not
|
1
|
file="/usr/bin/gdrive"
|
1
|
if [ -f "$file" ]
|
1
|
then
|
1
|
echo "Backup Process Starting......................."
|
1
|
else
|
1
|
#Download And Install Gdrive
|
1
|
if [ `getconf LONG_BIT` = "64" ]
|
1
|
then
|
1
|
wget "https://docs.google.com/uc?id=0B3X9GlR6EmbnQ0FtZmJJUXEyRTA&export=download" -O /usr/bin/gdrive
|
1
|
chmod 777 /usr/bin/gdrive
|
1
|
gdrive list
|
1
|
clear
|
1
|
echo "Backup Process Starting......................."
|
1
|
else
|
1
|
wget "https://docs.google.com/uc?id=0B3X9GlR6EmbnQ0FtZmJJUXEyRTA&export=download" -O /usr/bin/gdrive
|
1
|
chmod 777 /usr/bin/gdrive
|
1
|
gdrive list
|
1
|
clear
|
1
|
echo "Backup Process Starting......................."
|
1
|
fi
|
1
|
fi
|
1
|
################################################################ Change the below mentioned server details to your own Server - Database Details, Backup Local Path, Remote Backup Path Name, And WeB Server FDirectory Path, Your Email ID.##############################################################
|
1
|
#Database credentials
|
1
|
user="root"
|
1
|
password="admin@123"
|
1
|
host="localhost"
|
1
|
db_name="--all-databases"
|
1
|
#Define local path for backups
|
1
|
BACKUPPATH="/tmp/Web_Server_Backup"
|
1
|
#Define remote backup path
|
1
|
BACKUPPATHREM="Web-Backup"
|
1
|
#Web Server Folder Path
|
1
|
SITESTORE="/var/www"
|
1
|
#Mention Your Email ID For Backup Status
|
1
|
EMAIL="ashutoshsmaurya@gmail.com"
|
1
|
###############################################################
|
1
|
#Date prefix
|
1
|
DATEFORM=$(date +"%Y-%m-%d")
|
1
|
#Days to retain - In the DAYSKEEP variable you can specify how many days of backups you would like to keep, any older ones will be deleted from Google Drive.
|
1
|
DAYSKEEP=7
|
1
|
#Calculate days as filename prefix
|
1
|
DAYSKEPT=$(date +"%Y-%m-%d" -d "-$DAYSKEEP days")
|
1
|
#Create array of sites based on folder names
|
1
|
SITELIST=($(cd $SITESTORE; echo $PWD | rev | cut -d '/' -f 1 | rev))
|
1
|
#Make sure the backup folder exists
|
1
|
mkdir -p $BACKUPPATH
|
1
|
#For Cleanup
|
1
|
CLEAN=rm
|
1
|
#Check remote backup folder exists on gdrive
|
1
|
BACKUPSID=$(gdrive list --no-header | grep $BACKUPPATHREM | grep dir | awk '{ print $1}')
|
1
|
if [ -z "$BACKUPSID" ]; then
|
1
|
gdrive mkdir $BACKUPPATHREM
|
1
|
BACKUPSID=$(gdrive list --no-header | grep $BACKUPPATHREM | grep dir | awk '{ print $1}')
|
1
|
Fi
|
1
|
#Start the loop
|
1
|
for SITE in $SITELIST; do
|
1
|
#Delete old backup, get folder id and delete if exists
|
1
|
OLDBACKUP=$(gdrive list --no-header | grep $DAYSKEPT-$SITE | grep dir | awk '{ print $1}')
|
1
|
if [ ! -z "$OLDBACKUP" ]; then
|
1
|
gdrive delete $OLDBACKUP
|
1
|
fi
|
1
|
#Create the local backup folder if it doesn't exist
|
1
|
if [ ! -e $BACKUPPATH/$SITE ]; then
|
1
|
mkdir $BACKUPPATH/$SITE
|
1
|
fi
|
1
|
#Enter the Web folder
|
1
|
cd $SITESTORE/
|
1
|
#Back up the Web folder
|
1
|
tar -czf $BACKUPPATH/$SITENAME/$SITE/$DATEFORM-$SITE.tar.gz .
|
1
|
#Back up the Mysql database
|
1
|
mysqldump --user=$user --password=$password --events --ignore-table=mysql.event --host=$host $db_name | gzip > $BACKUPPATH/$SITE/$DATEFORM-$SITE.sql.gz
|
1
|
#Get current folder ID
|
1
|
SITEFOLDERID=$(gdrive list --no-header | grep $SITE | grep dir | awk '{ print $1}')
|
1
|
#Create folder if doesn't exist
|
1
|
if [ -z "$SITEFOLDERID" ]; then
|
1
|
gdrive mkdir --parent $BACKUPSID $SITE
|
1
|
SITEFOLDERID=$(gdrive list --no-header | grep $SITE | grep dir | awk '{ print $1}')
|
1
|
fi
|
1
|
#Upload Web Server Data tar
|
1
|
gdrive upload --parent $SITEFOLDERID --delete $BACKUPPATH/$SITE/$DATEFORM-$SITE.tar.gz
|
1
|
#Upload Server Mysql database
|
1
|
gdrive upload --parent $SITEFOLDERID --delete $BACKUPPATH/$SITE/$DATEFORM-$SITE.sql.gz
|
1
|
#Web Folder Log And Email Format
|
1
|
echo "Hi," >> /tmp/log01
|
1
|
echo " " >> /tmp/log01
|
1
|
gdrive list --no-header | grep $DATEFORM-$SITE.tar.gz | awk '{ print $1}' > /tmp/web_log.txt
|
1
|
[ -s /tmp/web_log.txt ] && echo "Web Server Backup Successfully Done.. File Name $DATEFORM-$SITE.tar.gz" >> /tmp/log01 || echo " Web Server Data Backup Error..!!" >> /tmp/log01
|
1
|
#Database Log And Email Format
|
1
|
gdrive list --no-header | grep $DATEFORM-$SITE.sql.gz | awk '{ print $1}' > /tmp/database_log.txt
|
1
|
[ -s /tmp/database_log.txt ] && echo "Database Backup Successfully Done.. File Name - $DATEFORM-$SITE.sql.gz" >> /tmp/log01 || echo " Database Backup Error..!!" >> /tmp/log01
|
1
|
echo " " >> /tmp/log01
|
1
|
echo " " >> /tmp/log01
|
1
|
echo "Thanks," >> /tmp/log01
|
1
|
echo "Ashutosh" >> /tmp/log01
|
1
|
#Log Status - Send Mail
|
1
|
cat -v /tmp/log01 | mutt -s "Backup Server Status Log - $(date)" $EMAIL
|
1
|
#Final Cleanup
|
1
|
chmod -R 777 /tmp/*
|
1
|
$CLEAN /tmp/web_log.txt
|
1
|
$CLEAN /tmp/database_log.txt
|
1
|
$CLEAN /tmp/log01
|
1
|
done
|
1
|
#Dispaly Internet Connection Error Message
|
1
|
else
|
1
|
internet_conn="0"
|
1
|
echo "------(ERROR)---------(ERROR)-------------(ERROR)------------(ERROR)----------------“
|
1
|
echo "############### Please Check Your Internet Connection. #############"
|
1
|
echo "---------------------------------------------------------------------------------------"
|
1
|
fi
|
1
|
:wq (Save & Quit)
|
2- Set Permission
1
|
[root@linux011 ~]# chmod 755 backup_script.sh
|
2- Run Script
[root@linux011 ~]# ./ backup_script.sh
https://docs.google.com/uc?id=0B3X9GlR6EmbnQ0FtZmJJUXEyRTA&export=download
HTTP request sent, awaiting response… 200 OK
Length: unspecified [application/x-executable] Saving to: “/usr/bin/gdrive”
[ <=> ] 3,506,980 49.9K/s
HTTP request sent, awaiting response… 200 OK
Length: unspecified [application/x-executable] Saving to: “/usr/bin/gdrive”
[ <=> ] 3,506,980 49.9K/s
2017-11-02 09:45:37 (63.3 KB/s) – “/usr/bin/gdrive” saved [7805504]
Authentication needed
Go to the following url in your browser:
Go to the following url in your browser:
https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=367116221053-7n0vf5akeru7on6o2fjinrecpdoe99eg.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=state
Enter verification code:
Copy the above url and open web browser- and login to your gmail account…
Allow Gdrive
Copy this code and paste Linux terminal and press enter
Enter verification code: 4/py5er42W-v-8wGNr8krnf9dSulvT6Rjh6blhvA
Backup Process Starting…………………..
Directory 1vX23Rm4JafhgjtbkiRLRHxLScNroyr7 created
Directory 1sbWO9ruAnS2BHrRkeIu7P3XtRHvoMN created
Uploading /tmp/Web_Server_Backup/www/2017-11-02-www.tar.gz
Uploaded 1QAow3L_ALWUmvdGpzWSg8YK9Kjtsf at 50.8 KB/s, total 26.8 MB
Removed /tmp/Web_Server_Backup/www/2017-11-02-www.tar.gz
Uploading /tmp/Web_Server_Backup/www/2017-11-02-www.sql.gz
Uploaded 1a6yYn6Z55to_-bGwRX4kCTTVvnp4Xp at 89.9 KB/s, total 14.1 MB
Removed /tmp/Web_Server_Backup/www/2017-11-02-www.sql.gz
Directory 1vX23Rm4JafhgjtbkiRLRHxLScNroyr7 created
Directory 1sbWO9ruAnS2BHrRkeIu7P3XtRHvoMN created
Uploading /tmp/Web_Server_Backup/www/2017-11-02-www.tar.gz
Uploaded 1QAow3L_ALWUmvdGpzWSg8YK9Kjtsf at 50.8 KB/s, total 26.8 MB
Removed /tmp/Web_Server_Backup/www/2017-11-02-www.tar.gz
Uploading /tmp/Web_Server_Backup/www/2017-11-02-www.sql.gz
Uploaded 1a6yYn6Z55to_-bGwRX4kCTTVvnp4Xp at 89.9 KB/s, total 14.1 MB
Removed /tmp/Web_Server_Backup/www/2017-11-02-www.sql.gz
Successfully Done……
Verify Backup Complete or Not
1- Check Your Mail – Backup Log
2- Google Dive
Now add the script as a cronjob so it runs every day
[root@linux011 ~]# crontab –e
@daily /bin/bash /root/backup_script.sh # Run Script Daily
Now your Web Server & Database backups will automatically be uploaded to Google Drive…..
0 Response to "Backup Web Server dan Database ke Google Drive"
Posting Komentar