mysqldump doesn't work in crontab

前端 未结 9 2133
我寻月下人不归
我寻月下人不归 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:49

    Local Host mysql Backup: 0 1 * * * /usr/local/mysql/bin/mysqldump -uroot -ppassword --opt database > /path/to/directory/filename.sql

    (There is no space between the -p and password or -u and username - replace root with a correct database username.)

    It works for me. no space between the -p and password or -u and username

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

    Ok, I had a similar problem and was able to get it fixed.

    In your case you could insert that mysqldump command to a script then source the profile of the user who is executing the mysqldump command for eg:

    . /home/bla/.bash_profile
    

    then use the absolute path of the mysqldump command

    /usr/local/mysql/bin/mysqldump -u root -pHERE THERE IS MY PASSWORD --all-databases | gzip > /var/db_backups/database_`date +%d%m%y`.sql.gz
    
    0 讨论(0)
  • 2020-12-08 21:56

    Alternatively you can create a custom command mycommand. To which you can add more options. You must give execute permissions.

    It is preferable to have a folder where they store all your backups, in this case using a writable folder "backup" which first create in "your home" for example.

    My command in "usr/local/bin/mycommand":

    #!/bin/bash
    MY_USER="your_user"
    MY_PASSWORD="your_pass"
    MY_HOME="your_home"
    case $1 in 
    "backupall")
        cd $MY_HOME/backup
        mysqldump --opt --password=$MY_PASSWORD --user=$MY_USER  --all-databases > bckp_all_$(date +%d%m%y).sql
        tar -zcvf bckp_all_$(date +%d%m%y).tgz bckp_all_$(date +%d%m%y).sql
        rm bckp_all_$(date +%d%m%y).sql;;
    *)  echo "Others";;
    esac
    

    Cron: Runs the 1st day of each month.

    0 0 1 * * /usr/local/bin/mycommand backupall
    

    I hope it helps somewhat.

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