backup mysql database

#DataBase vars
DB_NAME_LIST=(xxxxx xxxxx xxxxx xxxx)  
DB_USER=""  
DB_PASS=""  
DB_HOST=""

#FTP vars
FTP_UPLOAD_DIR="/QA/mysql_backup"
FTP_HOST="172.16.10.241"
FTP_USER="xxxxxxx"
FTP_PASS="xxxxxxxx"

# Others vars  
# whereis mysqldump  
# IS ` but not '  
BIN_DIR="/usr/bin"  
BCK_DIR="/home/robot/mysql_backup"  
DATE=`date +%Y%m%d_%H%M%S`  

echo "Make a folder:" $DATE

mkdir $BCK_DIR/$DATE


for DB_NAME in ${DB_NAME_LIST[@]}
do

    echo "Backup DataBase:" $DB_NAME
    # TODO Backup MySQL  
    #$BIN_DIR/mysqldump --opt -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME  > $BCK_DIR/$DATE/$DB_HOST.$DB_NAME.dump.sql
    $BIN_DIR/mysqldump --defaults-extra-file=/etc/mysql/backup.cnf $DB_NAME > $BCK_DIR/$DATE/$DB_HOST.$DB_NAME.dump.sql
    #$BIN_DIR/mysqldump    --login-path=local -e $DB_NAME > $BCK_DIR/$DATE/$DB_HOST.$DB_NAME.dump.sql
    echo "Finish Backup DataBase:" $DB_NAME

done
    
# TODO tar .sql
echo "#################################################################"
echo "TAR the bakup folder"
echo "#################################################################"
tar -czvf $BCK_DIR/$DATE.tar.gz $BCK_DIR/$DATE

# TODO Upgrade MySQL Backup to FTP

echo "#################################################################"
echo "Upgrade MySQL Backup to FTP"
echo "#################################################################"

ftp -v -n $FTP_HOST << EOF
user $FTP_USER $FTP_PASS
binary
hash
cd $FTP_UPLOAD_DIR
lcd $BCK_DIR
prompt
put $DATE.tar.gz
bye
EOF

# TOD remove Backup file and folder

echo "Remove the backup folder and file " $DATE "and" $DATE.tar.gz

rm -rf *$DATE*



你可能感兴趣的:(backup mysql database)