Posts Tagged ‘备份’

mysql 备份脚本

#!/bin/sh
# mysql_backup.sh: backup mysql databases and keep newest 5 days backup.
# -----------------------------
db_user="root"
db_passwd="sdfsa"
db_host="localhost"

# the directory for story your backup file.
backup_dir="/data/app/zahess/wwwroot/dbbackup"

# date format for backup file (dd-mm-yyyy)
time="$(date +"%d-%m-%Y")"

# mysql, mysqldump and some other bin's path
MYSQLDUMP="/usr/local/mysql/bin/mysqldump"
MKDIR="$(which mkdir)"
RM="$(which rm)"
MV="$(which mv)"
GZIP="$(which gzip)"

# check the directory for store backup is writeable
test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0

# the directory for story the newest backup
test ! -d "$backup_dir" && $MKDIR "$backup_dir"

# get all databases

for db in  wp_yang
do
        $MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP -9 > "$backup_dir/$time.$db.gz"
done

#delete the oldest backup 7 days ago
find $backup_dir -name "*.gz" -mtime +7 |xargs rm -rf

exit 0;