mysqldump doesn't work in crontab

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

提交回复
热议问题