执行 SQL DUMP 的脚本

执行 SQL DUMP 的脚本

我正在编写用于获取 SQL 转储并压缩它的脚本。

#!/bin/sh
DBUSERID="root"
DBPASSWORD="XXXXXXXX"
BACKUP_DIR="$HOME/daily_sql_backups"
DATE=`date +%Y_%m_%d_%H_%M_%S`
cd $BACKUP_DIR
mysql -u$DBUSERID -p$DBPASSWORD  --socket=/data/mysql/mysql.sock -e 'show databases' | grep abcd >MICRO_SERVICES.txt

sed -e '/^abcd_prod$/d' MICRO_SERVICES.txt > MICRO_SERVICES_DB.txt 
cat MICRO_SERVICES_DB.txt | while read line
do
    DB=$line
    mysqldump --opt --user=${DBUSERID} --password=${DBPASSWORD} --socket=/data/mysql/mysql.sock ${DB} > $DB_$DATE.sql 2> "$DB"_"$DATE".err
    gzip $DB_$DATE.sql
    status=$?
    err=`cat "$DB"_"$DATE".err |wc -l`
    DATE_COMPLETED=$(date "+%F # %T")
    if [ $err -eq 0 -a $status -eq 0 ]
    then
       echo "$DATE_COMPLETED : DB backup success" >>/backup_logs/"$DB"_backup-$(date +%b-%Y).txt
       echo "$DATE_COMPLETED : DB backup success"
    else
       echo "$DATE_COMPLETED : DB backup FAILURE" >> /backup_logs/"$DB"_backup-error-$(date +%b-%Y).txt
       cat "$DB"_"$DATE".err >>/tmp/"$DB"_backup-error-$(date +%b-%Y).txt
       cat "$DB"_"$DATE".err |mail -s "$DB: dump failure" [email protected]
    fi
   sleep 1m
done

我收到错误“gzip:/home/XXXXXX/daily_sql_backups/2017_12_07_14_28_02.sql.gz 已存在;未覆盖”并且我收到错误转储失败。有人能告诉我我哪里做错了吗

答案1

实际上当我使用时它起作用了

"$DB"_"$DATE".sql 

代替

$DB_$DATE.sql. 

有人能解释一下为什么吗?

相关内容