数据库备份与恢复

一、备份脚本

#!/usr/bin/bash
path_current=$(cd "$(dirname "$0")"; pwd)
name_current=$(basename "$0" .sh)
echo $path_current
echo $name_current

if [ ! -d $path_current/backup/ ];then
   echo "create backup path: $path_current/backup"
   mkdir -p $path_current/backup
fi

path_backup=$path_current/backup/
echo "backup path: ${path_backup}"

dball=(commams-bigdata commams-bos commams-coupon commams-device commams-face commams-goods commams-log commams-logistics commams-marketing commams-member commams-oa commams-oauth commams-order commams-partner commams-pay commams-payaccount commams-peripherals commams-sms commams-wallet commams-wms)
for db in ${dball[@]};do
   if [ ! -f ${path_backup}/"$db".sql ];then
      echo "backup next database:$db"
      mysqldump --host xxxxx --port 196227  -u abc -p'yyy3' --set-gtid-purged=off --databases "$db" > ${path_backup}/"$db".sql
      if [ $? -ne 0 ]; then
         rm -f ${path_backup}/"$db".sql
         echo "failed to backup the database."
         exit 1
      fi
   fi
done
echo "success to backup database from production"

二、restore.sh恢复脚本

#!/usr/bin/bash
path_current=$(cd "$(dirname "$0")"; pwd)
name_current=$(basename "$0" .sh)
echo $path_current
echo $name_current

if [ ! -d $path_current/backup/ ];then
   echo "should first backup database."
   exit 1
fi

path_backup=$path_current/backup/
echo "backup path: ${path_backup}"

dball=(commams-bigdata commams-bos commams-coupon commams-device commams-face commams-goods commams-log commams-logistics commams-marketing commams-member commams-oa commams-oauth commams-order commams-partner commams-pay commams-payaccount commams-peripherals commams-sms commams-wallet commams-wms)

for db in ${dball[@]};do 
   if [ -f ${path_backup}/"$db".sql ];then
      echo "restore database:$db"
      mysql --host sxxxxx --port 3307  -uroot -p'123456' < ${path_backup}/"$db".sql && echo "$db is ok"
   fi
done
echo "success to restore database from production"