This article mainly introduces a simple MySQL backup Shell script. This article provides code examples directly. For more information, see
#! /Bin/bash # this is a script of mysql backup if [! -D/mydata/data1/backup]; then mkdir/mydata/data1/backupficd/mydata/data1/backupfile = $ (find. -type f-mtime-7 | grep. * all. SQL) # Find the backup file echo $ fileif [-z $ file]; then echo "backup all databases... "backupfile = $ (date + % F-% H-% M-% S) mysqldump-uroot -- lock-all-tables -- flush-logs -- master-data = 2 -- all-databases>/mydata/data1/backup/"$ backupfile-all. SQL" if [$? -Eq 0]; then echo "Accomplish, file is $ backupfile-all. SQL! "Else echo" Failure !!! "Fielse # Find the penultimate binary file for incremental backup cd. echo" All database backups, now start doing incremental backups! "A = 1 B = 1 for file in $ (ls | grep mysql-bin | grep-v index); do num = $ (echo $ file | cut-d. -f2) if [$ num-gt $ a]; then a = $ num fi done unset num file num = 1 for file in $ (ls | grep mysql-bin | grep-v index ); do num = $ (echo $ file | cut-d. -f2) if [$ num-gt $ B-a $ num-ne $ a]; then B = $ num tmp = $ file fi done file1 = $ (echo $ tmp | cut-d. -f1) file2 = $ (echo $ tmp | cut-d. -f2) mysqlbinlog $ tmp>/mydata/data1/ba Ckup/"$ file1-$ file2. SQL" if [$? -Eq 0]; then echo "Backup completed, file is" $ file1-$ file2. SQL "" fifi
Note: This script performs a full backup every seven days, and then performs an incremental backup every day. full backup uses the mysqldump client tool, and incremental backup uses mysqlbinlog to back up its binary log files.