mysql数据库备份脚本

#!/bin/bash

source /etc/profile

USER=ops
PASSWORD="aaaaaaaa"
#DATABASE="test tes1t"
HOSTNAME="192.168.11.11"

BACKUP_DIR=/opt/backup/mysql_back/
#LOGFILE=/opt/backup/mysql_back/data_backup.log
LOGFILE=/root/shell/logs/mysql_backup.log
DATE=`date '+%Y%m%d-%H%M'`
#DUMPFILE=$DATE.$i.sql
#ARCHIVE=$DATE.$i.sql.tgz
OPTIONS="-h$HOSTNAME -u$USER -p$PASSWORD --single-transaction --databases $DATABASE"


if [ ! -d $BACKUP_DIR ] ;
then
mkdir -p "$BACKUP_DIR"
fi

echo " " >> $LOGFILE
echo " " >> $LOGFILE
echo "———————————————–" >> $LOGFILE
echo "BACKUP DATE:" $(date +"%y-%m-%d %H:%M:%S") >> $LOGFILE
echo "———————————————– " >> $LOGFILE

cd $BACKUP_DIR
#mysqldump $OPTIONS > $DUMPFILE

#for i in aaa bbb
for i in aaa
do
DUMPFILE=$DATE.$i.sql
ARCHIVE=$DATE.$i.sql.tgz

echo "begin backup ${i}"
mysqldump -h${HOSTNAME} -u${USER} -p${PASSWORD} --single-transaction --databases $i > $DATE.$i.sql

if [[ $? == 0 ]]; then
tar czvf $ARCHIVE $DUMPFILE >> $LOGFILE 2>&1
echo “[$ARCHIVE] Backup Successful!” >> $LOGFILE
rsync -auv $ARCHIVE [email protected]:/data2/senses/$HOSTNAME/mysql_back/ 2>&1 > /dev/null
echo “[$ARCHIVE] rsync 2.202 Successful!” >> $LOGFILE
#rm -rf $DUMPFILE
#rm -fr $ARCHIVE
find /opt/backup/mysql_back/ -mtime +7 -exec rm -fr {} \;
else
echo “Database Backup Fail!” >> $LOGFILE
fi
echo “${i} Backup Process Done”


done

你可能感兴趣的:(mysql数据库备份脚本)