数据备份¶
为了防止数据丢失,OpsAny提供了Mysql和Mongo的数据备份脚本,需要用户修改对应配置项,同时支持手动添加至Crontab任务中进行定时备份。
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
HEARTBEAT 服务拨测数据备份¶
zabbix监控平台服务拨测功能是基于heartbeat进行数据统计。 heartbeat会根据每一个拨测服务生成对应的yaml文件。
目录是在/opt/opsany/uploads/monitor/heartbeat-monitors.d/* 可以直接备份目录即可。
cp -r /opt/opsany/uploads/monitor/heartbeat-monitors.d/ /backup/