Automatically Backup MySQL database on linux server

前端 未结 7 678
無奈伤痛
無奈伤痛 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:02

    Answer: A cron

    Description:

    Try creating a file something.sh with this:

     #!/bin/sh
     mysqldump -u root -p pwd --opt db1.sql > /respaldosql/db1.sql
     mysqldump -u root -p pwd --opt db2.sql > /respaldosql/db2.sql
     cd /home/youuser/backupsql/
     tar -zcvf backupsql_$(date +%d%m%y).tgz *.sql
     find -name '*.tgz' -type f -mtime +2 -exec rm -f {} \;
    

    Give the adequate permission to the file

     chmod 700 mysqlrespaldo.sh
    

    or

     sudo chmod 700 something.sh
    

    and then create a cron with

     crontab -e
    

    setting it like

     **0 1 * * *** /home/youruser/coolscripts/something.sh
    

    Remember that the numbers or '*' characters have this structure:

    Minutes (range 0-59)
    Hours (0-23)
    Day of month (1-31)
    Month (1-12)
    Day of the week (0-6 being 0=Domingo)
    Absolute path to script or program to run
    

提交回复
热议问题