三、數(shù)據(jù)庫自動備份腳本
1、自動備份腳本
#vim db_backup.sh
#!/bin/bash
#Auto backup for mariadb
#Date: first write on 2014.7.22
#Vertion: v1.1
date=`date +%Y-%m-%d`
datadir="tar -jcf /opt/backup/datadir-$date.tar.bz2 /data"
#alldb="mysqldump --all-databases > /opt/backup/alldb-$date.sql"
#zentaodb="mysqldump --databases zentao > /opt/backup/zentao-$date.sql"
user1=13910892224@139.com
user2=15048088251@139.com
if [ ! -d "/opt/backup/" ]; then
mkdir -p /opt/backup/
fi
mysqldump --all-databases > /opt/backup/alldb-$date.sql && mysqldump --databases zentao > /opt/backup/zentao-$date.sql
judge=`echo $?`
if [ $judge == 0 ];then
cd /opt/backup/ && tar -jcf ALLDB-$date.tar.bz2 alldb-$date.sql zentao-$date.sql --remove-files
$datadir;
judge1=`echo $?`
if [ $judge1 == 0 ];then
echo "Backup is ok!" | mail -s "Auto backup for db" $user1 $user2;
else
echo "Db is ok but datadir is failure!" | mail -s "Auto backup for db" $user1 $user2;
fi
else
echo "Backup is failure,because of db backup is faild!" | mail -s "Auto backup for db" $user1 $user2;
fi
find /opt/backup/ -type f -mtime +30 -name "*.bz2" -exec rm -rf {} ;
2、賦予腳本執(zhí)行權(quán)限
chmod +x db_backup.sh
3、任務(wù)計劃自動執(zhí)行
#crontab -e
0 0 * * * /bin/sh /opt/scripts/db_backup.sh > /dev/null 2>&1
四、更新腳本版本:
數(shù)據(jù)庫備份腳本v1.2
#!/bin/bash
#Auto backup for mariadb
#Date: first write on 2014.7.22
#Vertion: v1.2
date=`date +%Y-%m-%d`
user1=13910892224@139.com
user2=15048088251@139.com
main(){
tar -jcf /opt/backup/datadir-$date.tar.bz2 /data;
if [ $? -eq 0 ];then
/usr/local/mysql/bin/mysqldump --all-databases > /opt/backup/alldb-$date.sql;
if [ $? -eq 0 ];then
sleep 2
/usr/local/mysql/bin/mysqldump --databases zentao > /opt/backup/zentao-$date.sql;
if [ $? -eq 0 ];then
cd /opt/backup/
tar -jcf ALLDB-$date.tar.bz2 alldb-$date.sql zentao-$date.sql --remove-files
&& echo "Backup is ok!" | mail -s "Auto backup for db" $user2;
else
echo "Zentao db backup failure" | mail -s "Auto backup for db" $user2;
fi
else
echo "All databases backup failure" | mail -s "Auto backup for db" $user2;
fi
else
echo "Datadir backup failure,it's first step" | mail -s "Auto backup for db" $user2;
fi
find /opt/backup/ -type f -mtime +30 -name "*.bz2" -exec rm -rf {} ;
}
if [ ! -d "/opt/backup/" ]; then
mkdir -p /opt/backup/;
main;
else
main;
fi