#!/bin/bash #初始数据库文件放在/soft目录下,备份数据库文件放在/tzxdb/backup/目录下 sif-stop echo "正在停止应用,请等待1分钟" sleep 60 dbname=$(su - db2inst1 -c "db2 list db directory"|sed -n "10p"|awk ‘{print $5}‘|awk -F "/" ‘{print $3}‘) #获取数据库名 read -p "是否要备份现有数据库(yes/no):" backup b=yes if [ "$backup" = "$b" ] then aa=backup bb=$(ls /tzxdb |grep backup) if [ "$aa" = "$bb" ] then echo "正在备份数据库到/tzxdb/backup/" else echo "正在备份数据库到/tzxdb/backup/" mkdir /tzxdb/backup chmod 777 /tzxdb/backup fi #判断备份数据目录是否存在,如不存在则新建一个并附与权限。 su - db2inst1 -c "db2 force application all" su - db2inst1 -c "db2 force application all" su - db2inst1 -c "db2stop" su - db2inst1 -c "db2start" #重启数据库 su - db2inst1 -c "db2 backup db $dbname to /tzxdb/backup" echo "备份完成,/tzxdb/backup/" ls /tzxdb/backup/ read -p "请确定要恢复数据库到初始状态(yes/no):" aaa bbb=yes if [ "$aaa" = "$bbb" ] then su - db2inst1 -c "db2 force application all" sleep 2 su - db2inst1 -c "db2 force application all" su - db2inst1 -c "db2stop" echo "数据库停止" su - db2inst1 -c "db2start" echo "数据库启动" su - db2inst1 -c "db2 drop db $dbname" echo "数据库删除" cd /tmp echo "数据库还原中......" nohup su - db2inst1 -c "/soft/dbinstall" & #在后台执行数据库还原脚本(脚本见下) else echo "已退出数据库还原" fi else read -p "请确定要恢复数据库到初始状态(yes/no):" aaa bbb=yes if [ "$aaa" = "$bbb" ] then su - db2inst1 -c "db2 force application all" sleep 2 su - db2inst1 -c "db2 force application all" su - db2inst1 -c "db2stop" echo "数据库停止" su - db2inst1 -c "db2start" echo "数据库启动" su - db2inst1 -c "db2 drop db $dbname" echo "数据库删除" cd /tmp echo "数据库恢复中......" nohup su - db2inst1 -c "/soft/dbinstall" & #在后台执行数据库还原脚本(脚本见下) else echo "已退出数据库还原" fi fi #数据库还原脚本 #!/bin/bash namedb1=testdb dbname=$( ls /soft |grep db2inst1|awk -F "." ‘{print $1}‘ ) dbtime=$( ls /soft |grep db2inst1|awk -F "." ‘{print $6}‘ ) db2 restore db $dbname from /soft taken at $dbtime on /tzxdb/$namedb1 dbpath on /tzxdb/$namedb1 into $namedb1
时间: 2024-11-14 13:38:53