一、mysql数据库备份脚本
备份脚本1:
[root@szxelab-a-back-12 scripts]# vim elab_fenku_backup.sh #!/bin/sh #create at 2018-01-08 #author oldboy #qq 88888 echo "--------------------------ychfq:`date +%F-%H`--------------------------" DB_USER="oldboy_bak" DB_PWD="123456" DB_PROT="3306" DB_HOST="ro-oldboy.mysql.rds.aliyuncs.com" BACKUP_DIR="/data/backup/mysql" if [ ! -d $BACKUP_DIR ];then echo "$BACKUP_DIR:No such file or directory" && exit 2 else for dbname in `mysql -u"$DB_USER" -p"$DB_PWD" -h"$DB_HOST" -P"$DB_PROT" -e "show databases;" \ |egrep -v "Database|information_schema|performance_schema|mysql|mysq"` do #备份存储过程、函数等 mysqldump -u"$DB_USER" -p"$DB_PWD" -h"$DB_HOST" -P"$DB_PROT" --opt --default-character-set=utf8 --hex-blob --triggers -d -t -R $dbname \ |sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/'|gzip>$BACKUP_DIR/"$dbname"_func_$(date +%Y%m%d%H).sql.gz #copy存储过程、函数到公司内网服务器 scp -r -p -P 52119 $BACKUP_DIR/"$dbname"_func_$(date +%Y%m%d%H).sql.gz root@116.204.67.158:/data/mysqlbak/ & #备份视图、数据 mysqldump -u"$DB_USER" -p"$DB_PWD" -h"$DB_HOST" -P"$DB_PROT" --opt --default-character-set=utf8 --hex-blob --skip-triggers $dbname \ |sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/'|gzip>$BACKUP_DIR/"$dbname"_data_$(date +%Y%m%d%H).sql.gz #copy视图、数据到公司内网服务器 scp -r -p -P 52119 $BACKUP_DIR/"$dbname"_data_$(date +%Y%m%d%H).sql.gz root@223.5.5.158:/data/mysqlbak/ & done fi wait find $BACKUP_DIR -mtime +3 -type f -name "*.sql.gz" |xargs rm -f
备份脚本2:
[root@szxelab-a-back-12 scripts]# cat elab_fenku_backup.sh #!/bin/sh #create at 2018-01-08 #author oldboy #qq 88888 echo "--------------------------ychfq:`date +%F-%H`--------------------------" DB_USER="elab_bak" DB_PWD="123456" DB_PROT="53306" DB_HOST="oldboycom.mysql.rds.aliyuncs.com" BACKUP_DIR="/data/backup/mysql" if [ ! -d $BACKUP_DIR ];then echo "$BACKUP_DIR:No such file or directory" && exit 2 else for dbname in `mysql -u"$DB_USER" -p"$DB_PWD" -h"$DB_HOST" -P"$DB_PROT" -e "show databases;" \ |egrep -v "Database|information_schema|performance_schema|mysql|mysq"` do #备份存储过程、函数等 mysqldump -u"$DB_USER" -p"$DB_PWD" -h"$DB_HOST" -P"$DB_PROT" --opt --default-character-set=utf8 --hex-blob --triggers -d -t -R $dbname \ |sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/'|gzip>$BACKUP_DIR/"$dbname"_func_$(date +%Y%m%d%H).sql.gz #copy存储过程、函数到公司内网服务器 scp -r -p -P 52119 $BACKUP_DIR/"$dbname"_func_$(date +%Y%m%d%H).sql.gz root@116.204.67.158:/data/mysqlbak/ & #备份视图、数据 mysqldump -u"$DB_USER" -p"$DB_PWD" -h"$DB_HOST" -P"$DB_PROT" --opt --default-character-set=utf8 --hex-blob --skip-triggers $dbname \ |sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/'|gzip>$BACKUP_DIR/"$dbname"_data_$(date +%Y%m%d%H).sql.gz #copy视图、数据到公司内网服务器 scp -r -p -P 52119 $BACKUP_DIR/"$dbname"_data_$(date +%Y%m%d%H).sql.gz root@223.5.5.158:/data/mysqlbak/ & done fi wait find $BACKUP_DIR -mtime +3 -type f -name "*.sql.gz" |xargs rm -f
二、恢复脚本
[root@analysis ~]# cat /scripts/mysql_import.sh #!/bin/sh #author billy #time 2017-01-10 #version 1.0 MYSQL_PATH=/application/mysql/bin cd /scripts echo "---------------------------------$(date +%F)-------------------------------------" /usr/bin/scp -r -p -P53306 root@10.68.8.23:/data/mysqlbak/*_$(date +%Y%m%d)01.sql.gz /scripts #/usr/bin/scp -r -p root@10.68.8.23:/data/mysqlbak/youyadai_$(date +%Y%m%d)01.sql.gz /scripts /bin/gzip -d *_$(date +%Y%m%d)01.sql.gz #/bin/gzip -d yyd_$(date +%Y%m%d)01.sql.gz $MYSQL_PATH/mysql -uroot -p'123456' yyd <youyadai_data_$(date +%Y%m%d)01.sql $MYSQL_PATH/mysql -uroot -p'123456' yyd <youyadai_func_$(date +%Y%m%d)01.sql #$MYSQL_PATH/mysql -uroot -p'123456' ych <ychfq_data_$(date +%Y%m%d)01.sql $MYSQL_PATH/mysql -uroot -p'123456' ych <ychfq_func_$(date +%Y%m%d)01.sql $MYSQL_PATH/mysql -uroot -p'123456' lending_account <lending_account_func_$(date +%Y%m%d)01.sql $MYSQL_PATH/mysql -uroot -p'123456' lending_account <lending_account_data_$(date +%Y%m%d)01.sql /bin/rm -f /scripts/*_$(date +%Y%m%d)01.sql echo "---------------------------------end-------------------------------------"