[python] view plaincopyprint?
db_user="root" db_passwd="123456" db_host="localhost" backup_dir="/usr/local/apache2/htdocs/backup" time="$(date +"%Y%m%d%H%M")" MYSQL="/usr/local/mysql/bin/mysql" MYSQLDUMP="/usr/local/mysql/bin/mysqldump" MKDIR="/bin/mkdir" RM="/bin/rm" MV="/bin/mv" GZIP="/bin/gzip" test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0 test ! -d "$backup_dir/$time" && $MKDIR -p "$backup_dir/$time" all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"
for db in $all_db do $MYSQLDUMP "——default-character-set=gbk" -u $db_user -h $db_host -p$db_passwd $db | $GZIP -9 > "$backup_dir/$time/$db.gz" done exit 0;
設(shè)置定時(shí)任務(wù)
使用crontab -e命令編輯定時(shí)任務(wù)列表
[html] view plaincopyprint?
15 3 * * * sh /usr/scripts/mysql_backup.sh
表示每天凌晨3點(diǎn)15分執(zhí)行該腳本進(jìn)行數(shù)據(jù)庫備份