Automatically Backup MySQL database on linux server

前端 未结 7 668
無奈伤痛
無奈伤痛 2021-02-04 10:32

I need a script that automatically makes a backup of a MySql Database. I know there are a lot of posts and scripts out there on this topic already but here is where mine differs

7条回答
  •  执念已碎
    2021-02-04 11:19

    Create a shell script like the one below:

    #!/bin/bash
    mysqldump -u username -p'password' dbname > /my_dir/db_$(date+%m-%d-%Y_%H-%M-%S).sql
    find /mydir -mtime +10 -type f -delete
    

    Replace username, password and your backup directory(my_dir). Save it in a directory(shell_dir) as filename.sh

    Schedule it to run everyday using crontab -e like:

    30 8 * * * /shell_dir/filename.sh
    

    This will run everyday at 8:30 AM and backup the database. It also deletes the backup which is older than 10 days. If you don't wanna do that just delete the last line from the script.

提交回复
热议问题