数据备份和迁移
如果想迁移OpsAny到新的环境,或者日常进行数据备份,需要了解OpsAny的数据存储,主要有以下三方面内容:
- MySQL数据库:所有的OpsAny SaaS平台均会创建一个和APPID同名的MySQL数据库,用户名也为APPID,密码为安装时自定义或者自动生成的密码。
- MongoDB数据库: 部署OpsAny SaaS平台会使用MongoDB进行数据存储,例如工作台、资源平台等。会创建一个和APPID同名的数据库,用户名也为APPID。
- uploads目录: 所有用户上传或者编写的脚本等内容均存储在INSTALL_PATH/uploads目录下。
OpsAny平台迁移
方案1: 新安装OpsAny后,进行数据迁移。(推荐)
- 1.安装全新OpsAny平台:在新的环境,根据官方文档安装和迁移前相同版本的OpsAny,迁移时不支持升级,所以没有升级到最新版本的,请先升级到最新版本。
- 2.将老平台的MySQL、MongoDB、uploads进行备份,并复制到新主机。
- 3.将老平台的数据导入,并进行验证。
方案2: 镜像迁移。
- 1.将原平台主机做虚拟机快照。
- 2.在新的环境,启动新的主机实例。
- 3.修改所有的配置文件,为最新的IP地址(¥INSTALL_PATH/conf下面所有的配置文件,大约20个左右。)
MySQL数据库备份
参数 |
说明 |
行数 |
MyUSER |
数据库用户名 |
Line 5 |
MyPASS |
数据库密码 |
Line 6 |
MyHOST |
数据库主机地址 |
Line 7 |
DEST |
备份存放目录 |
Line 10 |
#!/bin/bash
# Shell script to backup MySQL database
# Set these variables
MyUSER="" # DB_USERNAME
MyPASS="" # DB_PASSWORD
MyHOST="" # DB_HOSTNAME
# Backup Dest directory
DEST=""
# Email for notifications
# EMAIL=""
# How many days old files must be to be removed
DAYS=5
# Linux bin paths
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
# Get date in dd-mm-yyyy format
NOW="$(date +"%Y-%m-%d_%s")"
# Create Backup sub-directories
MBD="$DEST/$NOW"
install -d $MBD
# DB skip list
SKIP="information_schema
another_one_db"
# Get all databases
DBS="$($MYSQL -h $MyHOST -u $MyUSER -p$MyPASS -Bse 'show databases')"
# Archive database dumps
for db in $DBS
do
skipdb=-1
if [ "$SKIP" != "" ];
then
for i in $SKIP
do
[ "$db" == "$i" ] && skipdb=1 || :
done
fi
if [ "$skipdb" == "-1" ] ; then
FILE="$MBD/$db.sql"
$MYSQLDUMP -h $MyHOST -u $MyUSER -p$MyPASS --single-transaction $db > $FILE
fi
done
# Archive the directory, send mail and cleanup
cd $DEST
tar -cf $NOW.tar $NOW
$GZIP -9 $NOW.tar
# echo "MySQL backup is completed! Backup name is $NOW.tar.gz" | mail -s "MySQL backup" $EMAIL
rm -rf $NOW
# Remove old files
find $DEST -mtime +$DAYS -exec rm -f {} \;
MongoDB 数据库备份
参数 |
说明 |
行数 |
DB_BACKUP_PATH |
备份存放目录 |
Line 10 |
MONGO_HOST |
数据库主机地址 |
Line 11 |
MONGO_PORT |
数据库端口 |
Line 12 |
AUTH_ENABLED |
是否开启验证 |
Line 18 |
MONGO_USER |
数据库用户名 |
Line 19 |
MONGO_PASSWD |
数据库密码 |
Line 20 |
DATABASE_NAMES |
扫描数据库名 |
Link 26 |
#!/bin/bash
##
## MongoDB Database Backup Script
##
export PATH=/bin:/usr/bin:/usr/local/bin
TODAY=`date +"%Y_%m_%d"`
DB_BACKUP_PATH=''
MONGO_HOST=''
MONGO_PORT=''
# If mongodb is protected with username password.
# Set AUTH_ENABLED to 1
# and add MONGO_USER and MONGO_PASSWD values correctly
AUTH_ENABLED=1
MONGO_USER=''
MONGO_PASSWD=''
# Set DATABASE_NAMES to "ALL" to backup all databases.
# or specify databases names seprated with space to backup
# specific databases only.
DATABASE_NAMES='ALL'
#DATABASE_NAMES='mydb db2 newdb'
## Number of days to keep local backup copy
BACKUP_RETAIN_DAYS=30
mkdir -p ${DB_BACKUP_PATH}/${TODAY}
AUTH_PARAM=""
if [ ${AUTH_ENABLED} -eq 1 ]; then
AUTH_PARAM=" --username ${MONGO_USER} --password ${MONGO_PASSWD} "
fi
if [ ${DATABASE_NAMES} = "ALL" ]; then
echo "You have choose to backup all databases"
mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} ${AUTH_PARAM} --out ${DB_BACKUP_PATH}/${TODAY}/
else
echo "Running backup for selected databases"
for DB_NAME in ${DATABASE_NAMES}
do
mongodump --host ${MONGO_HOST} --port ${MONGO_PORT} --db ${DB_NAME} ${AUTH_PARAM} --out ${DB_BACKUP_PATH}/${TODAY}/
done
fi
## Remove backups older than {BACKUP_RETAIN_DAYS} days ##
DBDELDATE=`date +"%Y_%m_%d" --date="${BACKUP_RETAIN_DAYS} days ago"`
if [ ! -z ${DB_BACKUP_PATH} ];then
cd ${DB_BACKUP_PATH}
if [ ! -z ${DBDELDATE} ] && [ -d ${DBDELDATE} ];then
rm -rf ${DBDELDATE}
fi
fi