Mysql Database Backup Script


#!/bin/bash
now=”$(date +’%d_%m_%Y_%H_%M_%S’)”
filename=”db_backup_$now”.gz
backupfolder=”/var/www/html/backups”
fullpathbackupfile=”$backupfolder/$filename”
logfile=”$backupfolder/”backup_log_”$(date +’%Y_%m’)”.txt
echo “mysqldump started at $(date +’%d-%m-%Y %H:%M:%S’)” >> “$logfile”
mysqldump -u root -p databasename | gzip > “$fullpathbackupfile”
echo “mysqldump finished at $(date +’%d-%m-%Y %H:%M:%S’)” >> “$logfile”
exit 0

Path Details
Backup Path –> /var/www/html/backups (Ubuntu 14.04 & CentOS)
Backup Path –> /var/www/backups (Ubuntu 12.04)

Output Details
Web Interface
Ip-address/backups/ (Checking Your Data)

Multiple Database Backup Script


#!/bin/bash
MySQL User
USER=’Username’
MySQL Password
PASSWORD=’Password’
Backup Directory
OUTPUT=”/home/backup”
TIMESTAMP=date +%Y%m%d_%H%M%S;
mkdir $OUTPUT/$TIMESTAMP;
cd $OUTPUT/$TIMESTAMP;
Starting MySQL Backup
echo date;
databases=mysql --user=$USER --password=$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database
for db in $databases; do
if [[ “$db” != “information_schema” ]] && [[ “$db” != _* ]] ; then
echo “Dumping database: $db”
mysqldump –force –opt –user=$USER –password=$PASSWORD –databases $db > $OUTPUT/dbbackup-$TIMESTAMP-$db.sql
gzip $OUTPUT/dbbackup-$TIMESTAMP-$db.sql
fi
done
Finished MySQL Backup
echo date;

Path Details
/home/backup/

Output Details
/home/backup/dbbackup-20150827_155251-mysql.sql.gz & dbbackup-20150827_155251-performance_schema.sql.gz

CentOS Server Backup Script for DB, Users & Services


CentOS Server Backup Script for DB & Services :
———————————————————————
#!/bin/bash

echo -e “Centos-Server-Backup-Script”

echo -e “Backuping System files, DataBase files, User files & Services”

echo -e “Check List”

function checkLists {
if ! [ -f $backuplistfile ] ; then
echo “Missing backup Listfile. create $backuplistfile”
exit
fi;
}

echo -e “Check Backup Status”

function checkBackupStatus {
if $BACKUP_DAILY_ONLY_ONCE ; then
if [ -f $backupdir/0/$filename ] ; then
echo “Backup already exist – try again tomorrow.”
exit;
fi;
fi;
if [ -d $tempdir ] ; then
echo “”
echo “Backup is already running. remove temp folder to reset.”
exit;
fi;
}

echo -e “Create Temporary Folder”

function createTemporaryFolder {
printf “Creating temporary directory.. ”
if $WRITE_CHANGES ; then
mkdir $tempdir
printf “Ok\n”
else printf “Skipping\n”
fi;
}

echo -e “Delete Old Backup”

function deleteOldestBackup {
if $WRITE_CHANGES ; then
if [ -d $backupdir/$1/ ] ;
then
echo “Deleteing Number $1”;
rm -r -f $backupdir/$1/ ;
fi;
fi;
}

echo -e “Shift Backup”

function shiftBackup {
if [ -d $backupdir/$1/ ] ; then
printf “Moving Number $1 to Number $2.. “;
if $WRITE_CHANGES ; then
mv $backupdir/$1 $backupdir/$2/ ;
printf “Ok\n”
else printf “Skipping\n”
fi;
fi;
}

echo -e “Shift Backups”

function shiftBackups {
for (( c=$BACKUP_COPIES; c>0; c– ))
do
b=$c
let “b -= 1”
shiftBackup $b $c
done
}

echo -e “Dump Sql”

function dumpSQL {
printf “Regenerating DB list file.. “;
if $WRITE_CHANGES ; then
mysql -u $SQL_USER -p$SQL_PASSWD -Bse ‘show databases’ > $listfile
printf “Dumping SQL Databases.. “;
cat $listfile | while read line
do
dbname=$line
if [ $line != “information_schema” ] ;
then
mysqldump –events –ignore-table=mysql.events -u $SQL_USER -p$SQL_PASSWD $dbname > $tempdir/$dbname.sql
fi
done
printf “Ok\n”
else printf “Skipping\n”
fi;
}

echo -e “Create Backup”

function createBackup {
echo “Creating TGZ Backup file for..”;
echo “directories:”
cat $backuplistfile | while read line
do
for d in $line; do
echo $d

echo  -e “take target directory to backup and replace / with _ for backup filename”

target_backup_file=$tempdir/${d//[\/]/_}$filename
if $WRITE_CHANGES ; then
tar zcfP $target_backup_file $d > $workdir/log/$filename
fi;
done
break
done
echo “databases”
if $WRITE_CHANGES ; then
tar zcfP $tempdir/db.$filename $tempdir/*.sql > $workdir/log/db.$filename
fi;
}

echo -e “Move Backup”

function moveBackup {
printf “Move from temp to Backup Number 0.. “;
if $WRITE_CHANGES ; then
mkdir $backupdir/0/
mv $tempdir/*$filename $backupdir/0/ ;
printf “Ok\n”
else printf “Skipping\n”
fi;
}

echo -e “Clean Backup”

function cleanBackup {
printf “Cleaning.. “;
if $WRITE_CHANGES ; then
rm -r -f $tempdir/
printf “Ok\n”
else printf “Skipping\n”
fi;
}

echo -e “Start Backup”

function startBackup {
if $DISABLED ; then
echo “Skipping backup – script disabled”
exit
else
checkLists
checkBackupStatus
if $WRITE_CHANGES ; then
echo “Starting Backup”
else
echo “Running in test mode”
fi;

echo -e “delete oldest backup”
deleteOldestBackup $BACKUP_COPIES

echo -e “shift the middle snapshots(s) back by one, if they exist”
shiftBackups

echo -e “dump sql dbs”
dumpSQL

echo -e “create new backup”
createBackup

echo -e “move to location 0”
moveBackup

echo -e “clear temp for the next run”
cleanBackup
fi;
}

echo -e “Load Settings”

SCRIPTDIRECTORY=$(cd dirname $0 && pwd)
cd $SCRIPTDIRECTORY
if [ -f settings.cfg ] ; then
echo “Loading settings…”
source settings.cfg
else
echo “ERROR: Create settings.cfg (from settings.cfg.example)”
exit
fi;

echo “Starting Backup”

startBackup

echo -e “Backup is Finished”

echo
if $showfsz ; then
df -h
fi;

echo “All done”

How to Backup /www Dir


#!/bin/bash

P=’/home/backup’

if [ -d “$P” ]
then
cd /
sudo tar cvpzf “$P/www-backup-date +%d.%m.%Y.tar.gz” –one-file-system –exclude-tag-under=.ignorebackup –exclude=/proc –exclude=/media –exclude=/lost+found –exclude=/sys –exclude=/tmp –exclude=/mnt –exclude=/media –exclude=/dev –exclude-caches-all /
else
echo -e “Invalid folder: $P\n”
exit 1
fi

echo -e “\nDone.\n”
exit 0

Path Details
/home/backup

Output Details
/home/backup/
“www-backup-date.tar.gz”

How to Backup /root dir


#!/bin/bash

P=’/home/backup’

if [ -d “$P” ]
then
cd /
sudo tar cvpzf “$P/root-backup-date +%d.%m.%Y.tar.gz” –one-file-system –exclude-tag-under=.ignorebackup –exclude=/proc –exclude=/media –exclude=/lost+found –exclude=/sys –exclude=/tmp –exclude=/mnt –exclude=/media –exclude=/dev –exclude-caches-all /
else
echo -e “Invalid folder: $P\n”
exit 1
fi

echo -e “\nDone.\n”
exit 0

Path Details
/home/backup

Output Details
/home/backup/
“root-backup-date.tar.gz”

Conntrack Backup Script


#/bin/bash

Database credentials
user=”conntrack”
password=”conn123″
host=”localhost”
db_name=”conntrack”

Other Options
backup_path=”/home/conntrack”
date=$(date +”%Y-%m-%d”)

Dump database into SQL file
mysqldump -u conntrack -pconn123 database tablename > $backup_path/$date.sql

Path Details
Backup Path –> /home/conntrack

Output Details
Output –> File will dump in Date Format with Sql Extension 2015-08-28.sql