Created
November 15, 2012 05:47
-
-
Save umeyuki/4076853 to your computer and use it in GitHub Desktop.
Backup mysql lotation of 30 days
This file contains 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 | |
# | |
# Name: mysql_backup | |
# | |
# Purpose: | |
# Backup mysql lotation of 30 days | |
# | |
# Usage: | |
# mysql_backup {db_user} {db_password} {db_host} {db_name dump_dir} | |
######################################################## | |
echo `date`" [$$] mysql_backup started." | |
DATE=$(date +'%Y%m%d%H%M%S') | |
CMDNAME=`basename $0` | |
if [ $# -ne 5 ]; then | |
echo "Usage: $CMDNAME {db_user} {db_password} {db_host} {db_name dump_dir}" 1>&2 | |
exit 1; | |
fi | |
USER="-u $1" | |
PASSWORD="-p$2" | |
if [ $3 != "localhost" ]; then | |
DB_HOST="-h $3" | |
else | |
DB_HOST="" | |
fi | |
DB_NAME=$4 | |
DUMP_DIR=$5 | |
BACKUP_FILE=${DUMP_DIR}"/"${DB_NAME}_`date +'%Y%m%d'`.sql | |
if [ -f $BACKUP_FILE.gz ];then | |
rm -f $BACKUP_FILE.gz | |
fi | |
mysqldump $USER $PASSWORD $HOST $DB_NAME > $BACKUP_FILE | |
gzip $BACKUP_FILE | |
find $DUMP_DIR -mtime +30 -type f -exec rm -f {} \; | |
echo `date`" [$$] mysql_backup finished." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment