Linux下Mysql定時自動備份

灰灰的輝發表於2020-11-05

一、檢視記憶體選擇系統盤防止記憶體不足備份失敗

[root@iZ7ns71hl9rkcwZ scripts]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        79G  6.2G   69G   9% /
devtmpfs         16G     0   16G   0% /dev
tmpfs            16G     0   16G   0% /dev/shm
tmpfs            16G  448K   16G   1% /run
tmpfs            16G     0   16G   0% /sys/fs/cgroup
tmpfs           3.1G     0  3.1G   0% /run/user/0

二、建立檔案儲存目錄

mkdir -p /data/mysqlbak/data
mkdir -p /data/mysqlbak/scripts
mkdir -p /data/mysqlbak/logs

三、建立自動備份指令碼執行檔案

cd /data/mysqlbak/scripts
vi backup.sh

#!/bin/bash

#備份目錄
BACKUP_ROOT=/data/mysqlbak
BACKUP_FILEDIR=$BACKUP_ROOT/data

#當前日期
DATE=$(date +%Y%m%d)

######備份######

#查詢所有資料庫
#-uroot -p123456表示使用root賬號執行命令,且root賬號的密碼為:123456
DATABASES=$(mysql -uroot -p123456 -e "show databases" | grep -Ev "Database|sys|information_schema|performance_schema|mysql")
#迴圈資料庫進行備份
for db in $DATABASES
do
echo
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz BEGIN----------
mysqldump -uroot -p123456 --default-character-set=utf8 -q --lock-all-tables --flush-logs -E -R --triggers -B ${db} | gzip > $BACKUP_FILEDIR/${db}_$DATE.sql.gz
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz COMPLETE----------
echo
done

echo "done"

四、賦予指令碼執行許可權

chmod 777 backup.sh

五、將備份操作加入到定時任務中(每天凌晨2點執行)

crontab -e

00 2 * * * /data/mysqlbak/scripts/backup.sh > data/mysqlbak/logs/backup.log 2>&1

六、建立自動刪除備份指令碼檔案(防止備份檔案過大佔用記憶體,7天刪除一次)

vi backup_clean.sh

#!/bin/bash
echo ----------CLEAN BEGIN----------
find /data/mysqlbak/data -mtime +7 -name "*.gz" -exec rm -rf {} \;
echo ----------CLEAN COMPLETE----------

七、賦予刪除指令碼執行許可權

chmod 777 backup_clean.sh

八、將刪除操作加入到定時任務中(每天凌晨1點執行)

00 1 * * * /data/mysqlbak/scripts/backup_clean.sh > /data/mysqlbak/logs/backup_full_clean.log 2>&1

九、檢視定時任務

crontab -l

檢視結果:
[root@iZ7ns71hl9rkcwZ scripts]# crontab -l

00 2 * * * /data/mysqlbak/scripts/backup.sh > data/mysqlbak/logs/backup.log 2>&1

00 1 * * * /data/mysqlbak/scripts/backup_clean.sh > /data/mysqlbak/logs/backup_full_clean.log 2>&1

十一、或者備份至另外一臺伺服器,避免資料庫和指令碼一起丟失

      1、首先伺服器需要安裝export,yum安裝:

yum  install expect

      2、修改上面的指令碼檔案

#!/bin/bash

#備份目錄
BACKUP_ROOT=/data/mysqlbak
BACKUP_FILEDIR=$BACKUP_ROOT/data

#當前日期
DATE=$(date +%Y%m%d)

######備份######

#查詢所有資料庫
#-uroot -p123456表示使用root賬號執行命令,且root賬號的密碼為:123456
DATABASES=$(mysql -uroot -p123456 -e "show databases" | grep -Ev "Database|sys|information_schema|performance_schema|mysql")
#迴圈資料庫進行備份
for db in $DATABASES
do
echo
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz BEGIN----------
mysqldump -uroot -p123456 --default-character-set=utf8 -q --lock-all-tables --flush-logs -E -R --triggers -B ${db} | gzip > $BACKUP_FILEDIR/${db}_$DATE.sql.gz
echo ----------$BACKUP_FILEDIR/${db}_$DATE.sql.gz COMPLETE----------
echo ----------scp 226  begin----------
expect -c "
    spawn scp -r /data/mysqlbak/data/${db}_$DATE.sql.gz root@xxx.xxx.xxx.226:/data/mysqlbak/data225/
    expect {
        \"*assword\" {set timeout 300; send \"此處是scp的密碼\r\"; exp_continue;}
        \"yes/no\" {send \"yes\r\";}
    }
expect eof"
echo ----------scp 226  end----------
echo
done

echo "done"

或者原始碼安裝,參考 https://www.cnblogs.com/operationhome/p/9154055.html

文獻參考:https://www.cnblogs.com/blazeZzz/p/10881297.html

相關文章