通过shell脚本实现MySQL备份并恢复,for in流程控制

从3306备份数据库,还原到3307数据库上,通过shell脚本的for in流程控制实现
 
#!/bin/bash
 
my_date=`date +%Y%m%d`
my_time=`date +%H%M`
my_date_yesterday=`date +%Y%m%d --date='1 days ago'`
 
echo "----------------------------------------------------"
echo "$my_date $my_time"
 
backup_dir="/data/backup3307/${my_date}"
mkdir -p ${backup_dir}
echo "db backup dir: ${backup_dir}"
 
DBS="data1 data2 data3 data4 "
for db in ${DBS}
do
echo "${db} backup start..."
echo "$my_date $my_time"
file="${backup_dir}/${db}_${my_date}.sql"
mysqldump --login-path=backup --single-transaction --set-gtid-purged=OFF --opt --triggers --routines --events ${db} > ${file}
size=`ls -lh ${file}|awk '{print $5}'`
echo "size: ${size}"
echo "${db} backup end. "
echo "$my_date $my_time"
 
echo "${db} restore start..."
mysql --login-path=restore -e "CREATE DATABASE ${db}_$my_date_yesterday DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;"
mysql --login-path=restore "${db}_$my_date_yesterday" < ${file}
echo "${db} restore end."
echo "$my_date $my_time"
echo "gzip ${file}"
gzip ${file}
done
 
# delete old backup data
old_date=`date +%Y%m%d -d '5 days ago'`
rm -rf /data/backup3307/${old_date}
 
for in循环举例:
 
#!/bin/sh
 
s="a b c "
 
for i in $s
do
echo "i is $i"
done
 
执行结果:
 
# ./for_in.sh
i is a
i is b
i is c

转载于:https://www.cnblogs.com/qingchen-24K/p/7281093.html

你可能感兴趣的:(数据库,shell,awk)