Background: Full Library backup
Backup process:
1. Generate DB list, write DB name to file
2. Define the backup function and write the result to the SQL file
3. Compress files to reduce disk usage
4. Set the number of days to retain, periodically delete the n days before
5. Read the DB list file through the For loop, mysqldump
1#!/bin/Bash2 3port='5001'4Passwd='Admin'5Socked="/data/socket/mysql${port}.sock"6backup_file='/data/mysqldata/backup'7backup_save_days='2'8 9cmd1='show databases;'TenTime= 'Date+%y%m%d_%h%m%S ' OneEvery_day_backup_dir="${backup_file}/${time}_backup" Adb_list="${every_day_backup_dir}/db_list.txt" - -#function: check_db ===>Db_list.txt the check_db () - { - mkdir-PV ${every_day_backup_dir} >>/dev/NULL - Touch${db_list} &&chmod 755${db_list} &&Chown-R mysql.mysql ${db_list} +/USR/LOCAL/MYSQL/BIN/MYSQL-UADMIN-P${PASSWD}-S ${socked}-nbe"${CMD1}"|grep-V'Information_schema\|performance_schema\|mysql'>${db_list} - + } A at#function: backup_db ===>Xx.sql - backup_db () - { -/USR/LOCAL/MYSQL/BIN/MYSQLDUMP-UADMIN-P${PASSWD}-S ${socked} $1>> ${every_day_backup_dir}/$1. SQL - } - in#function: Tar_file ===>xx.Tar. GZ - Tar_file () to { + CD ${backup_file} - Tar-CF 'Date+%y%m%d_%h%m%s '.Tar. GZ ${time}_backup the RM-FR ${time}_backup * } $ Panax Notoginseng#function: Delete xx.Tar. GZ: <before n days> - Delete_file () the { + CD ${backup_file} A/usr/bin/Find-name"*.tar.gz"-mtime +${backup_save_days}-execRM {} \; the } + - # main_process $#1 $ check_db - -#2 the Cat${db_list}| whileRead Line - Do Wuyioldifs=${ifs} the backup_db ${line} -ifs=${oldifs} Wu Sleep - - Done About $#3 - Tar_file - -#4Delete XX.Tar. Gz:before N Days A +Delete_file ${backup_save_days}
MySQL Backup script-mysqldump