首页 > 代码库 > [转载]Back up all of your mysql databases nightly
[转载]Back up all of your mysql databases nightly
Put the following into something like /usr/local/bin/mysql_backup.sh and since it has MySQL’s root password in it, make sure that you chmod 700 to it so no one else can read it.
#!/bin/bashDB_BACKUP="/backups/mysql_backup/`date +%Y-%m-%d`"DB_USER="root"DB_PASSWD="secretttt"HN=`hostname | awk -F. ‘{print $1}‘`# Create the backup directorymkdir -p $DB_BACKUP# Remove backups older than 10 daysfind /backups/mysql_backup/ -maxdepth 1 -type d -mtime +10 -exec rm -rf {} \;# Backup each database on the systemfor db in $(mysql --user=$DB_USER --password=$DB_PASSWD -e ‘show databases‘ -s --skip-column-names|grep -viE ‘(staging|performance_schema|information_schema)‘);do mysqldump --user=$DB_USER --password=$DB_PASSWD --events --opt --single-transaction $db | gzip > "$DB_BACKUP/mysqldump-$HN-$db-$(date +%Y-%m-%d).gz";done
By the way, we’re skipping tables ‘performance_schema’ and ‘information_schema’…
Then just call it from cron by creating a root cron entry:
30 3 * * * /usr/local/bin/mysql_backup.sh
(完)
[转载]Back up all of your mysql databases nightly
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。