Linux 使用 Cron 作业进行每日数据库备份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6578533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:56:25  来源:igfitidea点击:

Daily Database backup using Cron Job

mysqllinuxcroncrontabcrontrigger

提问by Hussy

Hi i want to take database backup at daily mid night using cron job... and the name of database backup should append with current date... the format of backup file should be mydata_yyyy_mm_dd.sql ... backup file should be placed in /root directory

嗨,我想在每天午夜使用 cron 作业进行数据库备份...并且数据库备份的名称应附加当前日期...备份文件的格式应为 mydata_yyyy_mm_dd.sql ...备份文件应放在/根目录

采纳答案by wonk0

something like

就像是

0 0 * * * /path/to/mysqldump ... > /path/to/backup/mydata_$( date +"%Y_%m_%d" ).sql

should work.

应该管用。

Please read

请阅读

  • man date
  • man 5 crontab
  • 授权
  • 男人 5 crontab

回答by KumarA

Create a cron.sh file with this content:

使用以下内容创建一个 cron.sh 文件:

 mysqldump -u root -p{PASSWORD} DBNAME 2>> "/filename_`date '+%Y-%m-%d'`.sql"

And give the Read permission or full access permission for that cron.sh file.

并为该 cron.sh 文件授予读取权限或完全访问权限。

and add this line into crontab file ($ crontab -e)

并将这一行添加到 crontab 文件中($ crontab -e)

 0 0 * * *   cron.sh