Linux下定時備份資料庫
阿新 • • 發佈:2018-12-16
1.建立備份使用者
create user 'db_backup'@'%' identified by 'db_backup_password';
-- 賦zd_setmeal庫只讀許可權
grant select on zd_setmeal.* to [email protected]"%";
grant lock tables on zd_setmeal.* to [email protected]'%';
flush privileges;
2.編寫定時指令碼
#!/bin/bash db_name=zd_setmeal db_host=127.0.0.1 db_port=3307 db_user=db_backup db_pswd=db_backup_password db_backup_path=/opt/db_back/ date_one=`date -d "1 days ago" +%Y%m%d` file_name=$db_backup_path$db_name"_"$date_one".sql" #刪除七天前的備份 find $db_backup_path -name $db_name"*.sql" -type f -mtime +7 -exec rm -f {} \; #匯出資料 mysqldump -h$db_host -P$db_port -u$db_user -p$db_pswd $db_name > $file_name echo "done"
3.設定定時任務
crontab -e
# 新增一條記錄
1 0 * * * /bin/bash /opt/scripts/db_backup.sh > /opt/log/db_backup.log 2>&1