Automatically Backup MySQL database on linux server

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

    After a brief reading the question and the good answers i would add few more points. Some of them are mentioned already.

    The backup process can involve next steps:

    1. Create a backup
    2. Compress the backup file
    3. Encrypt the compressed backup
    4. Send the backup to a cloud (DropBox, OneDrive, GoogleDrive, AmazonS3,...)
    5. Get a notification about results
    6. Setup a schedule to run the backup process periodically
    7. Delete the old backup files

    To compound a script to cover all the backup steps you need an effort and knowledge.

    I would like to share a link to an article (i'm one of the writers) which describes the most used ways to backup MySQL databases with some details:

    1. Bash script

      # Backup storage directory  
      backup_folder=/var/backups
      
      # Notification email address 
      recipient_email=
      
      # MySQL user
      user=
      
      # MySQL password
      password=
      
      # Number of days to store the backup 
      keep_day=30 
      
      sqlfile=$backup_folder/all-database-$(date +%d-%m-%Y_%H-%M-%S).sql
      zipfile=$backup_folder/all-database-$(date +%d-%m-%Y_%H-%M-%S).zip 
      
      # Create a backup 
      sudo mysqldump -u $user -p$password --all-databases > $sqlfile 
      
      if [ $? == 0 ]; then
         echo 'Sql dump created' 
      else
         echo 'mysqldump return non-zero code' | mailx -s 'No backup was created!' $recipient_email  
         exit 
      fi 
      
      # Compress backup 
      zip $zipfile $sqlfile 
      
      if [ $? == 0 ]; then
         echo 'The backup was successfully compressed' 
      else
         echo 'Error compressing backup' | mailx -s 'Backup was not created!' $recipient_email 
         exit 
      fi 
      
      rm $sqlfile 
      
      echo $zipfile | mailx -s 'Backup was successfully created' $recipient_email 
      
      # Delete old backups 
      find $backupfolder -mtime +$keep_day -delete
      
    2. Automysqlbackup

      sudo apt-get install automysqlbackup
      wget https://github.com/sixhop/AutoMySQLBackup/archive/master.zip
      
      mkdir /opt/automysqlbackup
      mv AutoMySQLBackup-master.zip 
      cd /opt/automysqlbackup
      tar -zxvf AutoMySQLBackup-master.zip
      
      ./install.sh
      
      sudo nano /etc/automysqlbackup/automysqlbackup.conf
      
      CONFIG_configfile="/etc/automysqlbackup/automysqlbackup.conf"
      CONFIG_backup_dir='/var/backup/db'
      CONFIG_mysql_dump_username='root'
      CONFIG_mysql_dump_password='my_password'
      CONFIG_mysql_dump_host='localhost'
      CONFIG_db_names=('my_db')
      CONFIG_db_exclude=('information_schema')
      CONFIG_mail_address='mail@google.com'
      CONFIG_rotation_daily=6
      CONFIG_rotation_weekly=35
      CONFIG_rotation_monthly=150
      
      automysqlbackup /etc/automysqlbackup/automysqlbackup.conf
      
    3. Third party tools

    Hope it would be helpful!

提交回复
热议问题