Created
February 15, 2018 05:33
-
-
Save amitn322/009834f7dde3cf34f21fe24492c7b104 to your computer and use it in GitHub Desktop.
MySQL Backup and Rotate Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BACKUP_DIR=/path/to/backup/ | |
DB_NAME=dbname | |
TSTAMP=`date +%Y%m%d` | |
FILENAME=$BACKUP_DIR/$DB_NAME.gz | |
Notify=0 | |
Rotate=8 | |
logfile=$BACKUP_DIR/backup.log | |
echo ".............Backup Script Running on $TSTAMP............" >> $logfile | |
let i=$Rotate-1 | |
if [ -f "$FILENAME.$Rotate" ];then | |
echo "$FILENAME.$Rotate Found,Deleting" >> $logfile | |
rm -rf $FILENAME.$Rotate | |
else | |
echo "$FILENAME.$Rotate Not Found, Not Removing" >> $logfile | |
fi | |
while [ $i -ge 1 ] | |
do | |
let j=$i+1; | |
if [ -f "$FILENAME.$i" ];then | |
echo $FILENAME.$i exists and is being moved to $FILENAME.$j >> $logfile | |
mv $FILENAME.$i $FILENAME.$j | |
else | |
echo $FILENAME.$i not found, not moving to $FILENAME.$j >> $logfile | |
fi | |
let i=$i-1 | |
done | |
mysqldump $DB_NAME | gzip > $FILENAME.1 | |
echo "..........Backup Script Completed,Exiting Now.$TSTAMP........." >> $logfile | |
if [ $Notify == 1 ];then | |
SUBJECT="Database Backup Completed on:`hostname`" | |
ADMIN="admin email" | |
function message { | |
echo -e "Hi there,\n" | |
echo -e "..........................................................\n" | |
echo -e "This is to notify that database backup has completed on:`hostname`\n" | |
echo -e "Date-Time:`date`\n" | |
echo -e "--------------------------------------------------------\n" | |
} | |
message|mail -s "$SUBJECT" "$ADMIN" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment