请先查看我的其他教程:Yapi数据备份及迁移 https://www.jianshu.com/p/de26ad0110b0
yapi的数据库是MongoDB,所以yapi的数据备份和恢复实际上就是MongoDB的数据备份与恢复,使用复制数据库文件夹,然后进行覆盖,是不可以将数据成功迁移的!
背景:
1.yapi搭建在容器里,yapi的配置文件夹及MongoDB文件夹挂载到宿主机
2.使用scp传输备份
自动备份脚本:备份yapi的配置文件、数据文件
#!/bin/bash
#yapi 本地服务器-备份存放地址
LocalBackDir=/home/dgd/yapi/backups
#服务机yapi配置文件地址
ConfigDir=/home/dgd/yapi/config/my-yapi/config.json
#备份服务器-备份存储路径
RemoteBackDir=/yapi-backup-216
#远程备份使用用户及端口
RemoteUser=root
RemotePort=22
#备份服务器IP
RemoteIP=10.1.8.50
#以当前时间戳创建备份目录
bakname=$(date -d "today" +"%Y%m%d_%H%M%S")
BakDir=$LocalBackDir/$bakname
mkdir $BakDir
#备份日志文件
LogFile=$LocalBackDir/remote_backup.log
#新建备份日志文件
touch $LogFile
#记录配置文件备份日志
echo "Yapi configure file auto backup at local server, start at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
echo "--------------------------------------------------------------------------" >> $LogFile
#拷贝配置文件
cp -r $ConfigDir $BakDir >> $LogFile 2>&1
#记录本地生成Yapi备份日志
echo "Yapi auto backup at local server, start at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
echo "--------------------------------------------------------------------------" >> $LogFile
#执行Yapi本地备份,备份数据
docker exec testyapi mongodump -d yapi -o /my-yapi/ #yapi搭建在容器testyapi里
cd /home/dgd/yapi/config/yapi
tar -cvf /home/dgd/yapi/backups/yapi-mgdb-$(date +%Y%m%d).tar *
cd /home/dgd/yapi/backups
# $?符号显示上一条命令的返回值,如果为0则代表执行成功,其他表示失败
if [ $? -eq 0 ];then
#追加日志到日志文件
echo "--------------------------------Success!-------------------------------" >> $LogFile
echo "Yapi auto backup at local server, end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
else
#追加日志到日志文件
echo "--------------------------------Failed!----------------------------------" >> $LogFile
echo "Yapi auto backup at local server failed at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile
fi
#查找本地备份目录修改时间为10分钟以内且后缀为.tar的Yapi备份文件
Backfile_Send_To_Remote=`find $LocalBackDir -type f -mmin -10 -name '*.tar'` >> $LogFile 2>&1
#移动生成的备份文件到配置文件备份地址
mv -bfu $Backfile_Send_To_Remote $BakDir
#记录备份日志
echo "$(date +"%Y-%m-%d %H:%M:%S") Yapi auto backup to remote server." >> $LogFile
echo "--------------------------------------------" >> $LogFile
#打印每次备份的档案名
echo "The files need send to remote server is: $Backfile_Send_To_Remote" >> $LogFile
# 本地传输Yapi备份档案到远程
scp -r $RemotePort $BakDir $RemoteUser@$RemoteIP:$RemoteBackDir
# 备份结果追加到备份日志
if [ $? -eq 0 ];then
echo ""
echo "$(date +"%Y-%m-%d %H:%M:%S") Yapi Remote Backup Succeed!" >> $LogFile
else
echo "$(date +"%Y-%m-%d %H:%M:%S") Yapi Remote Backup Failed!" >> $LogFile
fi
#打印每次备份的档案名
echo "The files need send to remote server is: $Backfile_Send_To_Remote" >> $LogFile
# 本地传输Yapi备份档案到远程
scp -r $RemotePort $BakDir $RemoteUser@$RemoteIP:$RemoteBackDir
# 备份结果追加到备份日志
if [ $? -eq 0 ];then
echo ""
echo "$(date +"%Y-%m-%d %H:%M:%S") Yapi Remote Backup Succeed!" >> $LogFile
else
echo "$(date +"%Y-%m-%d %H:%M:%S") Yapi Remote Backup Failed!" >> $LogFile
fi
网友评论