mkdir -p /data/mysqlbal/data
mkdir -p /data/mysqlbal/scripts
mkdir -p /data/mysqlbal/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
crontab -e 00 2 * * * /data/mysqlbak/scripts/backup.sh > data/mysqlbak/logs/backup.log 2>&1
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
00 1 * * * /data/mysqlbak/scripts/backup_clean.sh > /data/mysqlbak/logs/backup_full_clean.log 2>&1
crontab -l
首先服務器須要安裝export,yum安裝:html
yum install expect
或者源碼安裝,參考mysql
http://www.javashuo.com/article/p-nyxwblfm-bb.htmlsql
腳本修改:數據庫
#!/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"