Automatic backup mysql database

You can select the desired setting backup libraries, automatic backup compression, automatically delete backups older than 7 days, you need to use crontab regular implementation.

#!/bin/bash

# Database name to be backed up, separated by a space multiple databases

databases=(db1 db2 db3) 

# To save the backup file directory

basepath='/root/backup/mysql/'

if [ ! -d "$basepath" ]; then

  mkdir -p "$basepath"

be

# Loop array of databases

for db in ${databases[*]}

  do

    # Generate SQL database backup files

    /bin/nice -n 19 /usr/bin/mysqldump -uUSER -pPASSWORD --database $db > $basepath$db-$(date +%Y%m%d).sql

    # Generated SQL file compression

    /bin/nice -n 19 tar zPcf $basepath$db-$(date +%Y%m%d).sql.tar.gz $basepath$db-$(date +%Y%m%d).sql

    # Delete backup data to 7 days before

    find $basepath -mtime +7 -name "*.sql.tar.gz" -exec rm -rf {} \;

  done

  # Delete the generated SQL file

  rm -rf $basepathroot/backup_mysql.sh

 

Guess you like

Origin www.cnblogs.com/mylover2/p/11015908.html