MeterSphere 数据备份

主要是 MySQL 数据库的数据备份和 /opt/metersphere/data 路径下的目录备份。

注意

数据库主要有 mysqldump 和 手动备份 /opt/metersphere/data/mysql 目录两种方式,可根据企业实际情况和已有备份工具制定备份策略和备份手段

1 手动备份

  1. #数据库备份:
  2. docker exec -i mysql mysqldump -uroot -pPassword123@mysql metersphere > metersphere.sql
  3. #data 目录备份
  4. zip -r XXX.zip /opt/metersphere/data

若备份数据库时出现mysqldump: Error 2020: Got packet bigger than ‘max_allowed_packet’ bytes when dumping tableapi_scenario_report_detailat row: 94,则添加max_allowed_packet参数,如下:

  1. docker exec -i mysql mysqldump -uroot -pPassword123@mysql metersphere --max_allowed_packet=2G > metersphere.sql

2 定时任务自动备份

ms_backup.sh

  1. #!/bin/bash
  2. backupDir=/opt/db_bak
  3. data=/opt/metersphere/data
  4. currentTime=`date "+%Y-%m-%d-%H-%M-%S"`
  5. backupZipFileName=ms_db_$currentTime.zip
  6. dumpSqlFilePath=$backupDir/ms_db_$currentTime.sql
  7. echo dumpSqlFilePath=$dumpSqlFilePath
  8. docker exec -i mysql mysqldump -uroot -pPassword123@mysql metersphere --max_allowed_packet=2G > $dumpSqlFilePath
  9. cd $backupDir
  10. zip -r $backupZipFileName $dumpSqlFilePath $data
  11. echo rm -rf dumpSqlFilePath
  12. rm -rf $backupDir/ms_db_$currentTime.sql
  13. #remove outdated backup files
  14. keepBackupNum=3
  15. output=`ls -lt $backupDir/*.zip | awk '{print $9}'`
  16. step=0
  17. for backupFile in $output ;do
  18. step=$((step+1))
  19. echo step=$step
  20. echo $backupFile
  21. if [ $step -gt $keepBackupNum ];then
  22. echo Remove outdated backup $backupFile
  23. rm -rf $backupFile
  24. fi
  25. done

install_ms_backup.sh

  1. #!/bin/bash
  2. timedate_fields="0 1 * * *" #每天凌晨1:00执行备份程序
  3. cmd="bash /opt/db_bak/ms_backup.sh"
  4. crontab -l | grep "$cmd " > /dev/null 2>&1
  5. if test $? -ne 0; then
  6. crontab -l > crontab.tmp
  7. echo "$timedate_fields $cmd" >> crontab.tmp
  8. crontab crontab.tmp
  9. fi

执行 crontab -l 即可

3 数据恢复

进入备份 sql 目录,将 sql 复制到 mysql 容器的挂载目录 /opt/metersphere/data/mysql 下

  1. cp metersphere.sql /opt/metersphere/data/mysql

进入 mysql 容器,进入数据库

  1. docker exec -it mysql sh
  2. mysql -uroot -pPassword123@mysql

使用 metersphere 库,并将数据导入到库里

  1. use metersphere;
  2. source /var/lib/mysql/metersphere.sql