mysqldump doesn't work in crontab

前端 未结 9 2106
我寻月下人不归
我寻月下人不归 2020-12-08 21:26

I\'m trying to add a cronjob in the crontab (ubuntu server) that backups the mysql db.

Executing the script in the terminal as root works well, but inserted in the c

相关标签:
9条回答
  • 2020-12-08 21:37

    You need to escape % character with \

    mysqldump -u 'username' -p'password' DBNAME > /home/eric/db_backup/liveDB_`date +\%Y\%m\%d_\%H\%M`.sql
    
    0 讨论(0)
  • 2020-12-08 21:38

    I was trying the same but I found that dump was created with 0KB. Hence, I got to know about the solution which saved my time.

    Command :

    0 0 * * * mysqldump -u 'USERNAME' -p'PASSWORD' DATEBASE > /root/liveDB_`date +\%Y\%m\%d_\%H\%M\%S`.sql
    

    NOTE: 1) You can change the time setting as per your requirement. I have set every day in above command.

    2) Make sure you enter your USERNAME, PASSWORD, and DATABASE inside single quote (').

    3) Write down above command in Crontab.

    I hope this helps someone.

    0 讨论(0)
  • 2020-12-08 21:38

    Check cron logs (should be in /var/log/syslog) You can use grep to filter them out.

    grep CRON /var/log/syslog

    Also you can check your local mail box to see if there are any cron mails

    /var/mail/username

    You can also set up other receiving mail in you crontab file

    MAILTO=your@mail.com

    0 讨论(0)
  • 2020-12-08 21:45

    Create a new file and exec the code there to dump into a file location and zip it . Run that script via a cron

    0 讨论(0)
  • 2020-12-08 21:48

    You might also need to restart the service to load any of your changes.

    service cron restart
    

    or

    /etc/init.d/cron restart
    
    0 讨论(0)
  • 2020-12-08 21:49

    I am using Percona Server (a MySQL fork) on Ubuntu. The package (very likely the regular MySQL package as well) comes with a maintenance account called debian-sys-maint. In order for this account to be used, the credentials are created when installing the package; and they are stored in /etc/mysql/debian.cnf.

    And now the surprise: A symlink /root/.my.cnf pointing to /etc/mysql/debian.cnf gets installed as well.

    This file is an option file read automatically when using mysql or mysqldump. So basically you then had login credentials given twice - in that file and on command line. This was the problem I had.

    So one solution to avoid this condition is to use --no-defaults option for mysqldump. The option file then won't be read. However, you provide credentials via command line, so anyone who can issue a ps can actually see the password once the backup runs. So it's best if you create an own option file with user name and password and pass this to mysqldump via --defaults-file.

    You can create the option file by using mysql_config_editor or simply in any editor.

    Running mysqldump via sudo from the command line as root works, just because sudo usually does not change $HOME, so .my.cnf is not found then. When running as a cronjob, it is.

    0 讨论(0)
提交回复
热议问题