Simple Backup Script For Code and MySql

admin/ July 6, 2016/ Linux/ 1 comments

A simple Bash Backup Script that can help you to take regular backup of code and database.

#!/bin/bash
BackupDirectory="/home/backuptest"
DBUser="root"
DBPass="dbpass"
DBHost="localhost"
WebDocRoot="/var/www/"
CurrentBackupDirectory=$BackupDirectory/$(date +%d-%m-%Y)
if [ ! -d "$MySqlBackupDirectory" ]; then
        mkdir -p $MySqlBackupDirectory
fi
############################################----MysqlDump----############################################
MySQL=$(which mysqldump)
GZip=$(which gzip)

db=$(mysql -u$$DBUser -p$DBPass -e "SHOW DATABASES;" | grep -Ev "Database")

for i in $db ; do  echo $i ; done

$MySQL --skip-lock-tables -u$DBUser -p$DBPass $i > $MySqlBackupDirectory/$i.sql
############################################----CodeBase Backup Script----############################################

IgnoreFile=$BackupDirectory/ignore.txt
Log=$BackupDirectory/log.txt
Source=$WebDocRoot
Destination=$CurrentBackupDirectory
Option="-avh --exclude-from $IgnoreFile "
if [ ! -f $IgnoreFile ]; then
touch $IgnoreFile
fi
rsync $Option $Source $Destination 2>> $Log

##################################################---zip Backup Directory-----##############################################

ZiP=$(which zip)

zip -r $CurrentBackupDirectory.zip $CurrentBackupDirectory
rm -rf $CurrentBackupDirectory

1 Comment

Leave a Reply