Last active
March 28, 2020 13:05
-
-
Save anandiamy/fe393a39084b567c727720901e8a00ad to your computer and use it in GitHub Desktop.
Backup Mysql and Delete file 7 days ago
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/sh | |
DATE=`date +%d%b-%H%M%S` | |
unset PATH | |
# USER VARIABLES | |
MYSQLUSER=your-user | |
MYSQLPWD=your-password | |
MYSQLHOST=localhost | |
MYSQLBACKUPDIRROOT=~/backup/mysql | |
MYSQLBACKUPDIR=$MYSQLBACKUPDIRROOT/$DATE | |
MYSQLDB=your-database | |
FILE=$MYSQLBACKUPDIR.tgz; # No need to modify this one | |
# PATH VARIABLES – It depend on your server program | |
MK=/bin/mkdir; | |
RM=/bin/rm; | |
GREP=/bin/grep; | |
MYSQL=/usr/bin/mysql; | |
MYSQLDUMP=/usr/bin/mysqldump; | |
XARGS=/usr/bin/xargs; | |
FIND=/usr/bin/find; | |
TAR=/bin/tar; | |
UUENCODE=/usr/bin/uuencode; | |
# CREATE MYSQL BACKUP | |
# Delete the old ones. The +7 means 7 days ago | |
$FIND $MYSQLBACKUPDIRROOT/* -mtime +7 | $XARGS $RM -rf | |
# Create new backup dir | |
$MK -p $MYSQLBACKUPDIR | |
#Dump new files | |
$MYSQLDUMP \ | |
-u$MYSQLUSER -p$MYSQLPWD -h$MYSQLHOST \ | |
-Q -c -C --add-drop-table --add-locks --quick --skip-lock-tables \ | |
$MYSQLDB > $MYSQLBACKUPDIR/$MYSQLDB.sql; | |
# collect and compress the sqls | |
$TAR -cvzpf $MYSQLBACKUPDIR.tgz -P $MYSQLBACKUPDIR | |
$RM -r $MYSQLBACKUPDIR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment