shell脚本实现GrayLog配置的定期备份
一、需求
需要定期备份GrayLog5.1版本的配置
二、实现步骤或者思路
众所周知GrayLog的配置都保存在MongoDB数据库,这时我们只需备份GrayLog的MongoDB即可
https://kb.synology.cn/zh-cn/DSM/tutorial/How_to_log_in_to_DSM_with_key_pairs_as_admin_or_root_permission_via_SSH_on_computers
- 3、备份完成后可以钉钉发送通知
- 4、结合crontab进行定期备份
0 1 * * * /opt/graylog_mongodb_backup.sh
- 5、最后测试利用数据库备份文件进行恢复
mongorestore --drop -h 127.0.0.1 -d graylog /root/restore_mongodb/graylog/ -u graylog
三、实现效果
1、最终实现的备份脚本如下
graylog_mongodb_backup.sh
说明:脚本中的一些变量请根据自己的实际情况进行修改,这里只做演示
#!/bin/bash
# LOCK_FILE文件路径
LOCK_FILE=/var/log/mongodb_backup_record.log
# 钉钉机器人 Webhook URL
WEBHOOK_URL="https://oapi.dingtalk.com/robot/send?access_token=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
# 获取当前日期作为变量
current_datetime=$(date +"%Y-%m-%d_%H_%M_%S")
# 定义备份目录和文件名
backup_dir="/home/graylog_mongodb_backup"
backup_file="graylog_mongodb_backup$current_datetime"
# MongoDB 连接参数
mongodb_host="localhost"
mongodb_user="graylog"
mongodb_password="Password请自己修改为实现的账号密码"
mongodb_database="graylog"
# NAS 目标路径
nas_ip="192.168.31.200"
nas_username="yuanfan"
nas_target_dir="/volume1/ShareFolder"
nas_ssh_port="8122"
nas_ssh_keyfile="/opt/nas_id_rsa.pem"
# 创建备份目录
mkdir -p "$backup_dir"
# 备份 MongoDB 数据库
mongodump -h "$mongodb_host" -u "$mongodb_user" -p "$mongodb_password" -d "$mongodb_database" -o "$backup_dir" >> ${LOCK_FILE} 2>&1
# 检查 mongodump 命令是否执行成功
if [ $? -eq 0 ]; then
echo `date +"%Y-%m-%d %H:%M:%S"` >> ${LOCK_FILE} 2>&1
echo "MongoDB backup completed successfully." >> ${LOCK_FILE} 2>&1
else
echo `date +"%Y-%m-%d %H:%M:%S"` >> ${LOCK_FILE} 2>&1
echo "Error occurred while performing MongoDB backup." >> ${LOCK_FILE} 2>&1
exit 1
fi
# 打包备份文件为 tar.gz 格式
cd $backup_dir
tar -zcvf /tmp/"$backup_file.tar.gz" graylog >> ${LOCK_FILE} 2>&1
# 检查打包命令是否执行成功
if [ $? -eq 0 ]; then
echo `date +"%Y-%m-%d %H:%M:%S"` >> ${LOCK_FILE} 2>&1
echo "Backup files compressed successfully." >> ${LOCK_FILE} 2>&1
else
echo `date +"%Y-%m-%d %H:%M:%S"` >> ${LOCK_FILE} 2>&1
echo "Error occurred while compressing backup files." >> ${LOCK_FILE} 2>&1
exit 1
fi
# 上传备份文件到 NAS
current_time=$(date +"%Y-%m-%d %H:%M:%S")
scp -i $nas_ssh_keyfile -P "$nas_ssh_port" /tmp/"$backup_file.tar.gz" "$nas_username@$nas_ip:$nas_target_dir" >> ${LOCK_FILE} 2>&1
# 检查上传命令是否执行成功
if [ $? -eq 0 ]; then
echo `date +"%Y-%m-%d %H:%M:%S"` >> ${LOCK_FILE} 2>&1
echo "Backup files uploaded to NAS successfully." >> ${LOCK_FILE} 2>&1
echo "备份文件上传成功,发送dingding通知" >> ${LOCK_FILE} 2>&1
notify_message="【通知】:Graylog服务器<font color=#FF0000> IP:($(hostname -I))</font> 的MongoDB数据库备份文件已上传至NAS <font color=#FF0000>IP:($nas_ip) </font>。\n\n【备份文件上传时间】:<font color=#FF0000> $current_time </font>\n\n【备份文件上传路径及文件名称】:<font color=#FF0000>$nas_target_dir/$backup_file.tar.gz</font>"
echo $notify_message >> ${LOCK_FILE} 2>&1
curl -s -H "Content-Type: application/json" -d "{\"msgtype\":\"markdown\",\"markdown\":{\"title\":\"通知\",\"text\":\"$notify_message\"}}" "$WEBHOOK_URL" >> ${LOCK_FILE} 2>&1
# 删除临时备份文件和目录
rm -rf "$backup_dir" >> ${LOCK_FILE} 2>&1
rm /tmp/"$backup_file.tar.gz" >> ${LOCK_FILE} 2>&1
else
echo `date +"%Y-%m-%d %H:%M:%S"` >> ${LOCK_FILE} 2>&1
echo "Error occurred while uploading backup files to NAS.">> ${LOCK_FILE} 2>&1
# 删除临时备份目录
rm -rf "$backup_dir" >> ${LOCK_FILE} 2>&1
exit 1
fi
2、执行脚本
(图片点击放大查看)
3、钉钉通知效果及确认NAS上的文件
(图片点击放大查看)
(图片点击放大查看)
4、还原测试
先删除一些配置
(图片点击放大查看)
(图片点击放大查看)
(图片点击放大查看)
下载NAS的备份并上传到GrayLog上
graylog_mongodb_backup2023-08-19_22_10_50.tar.gz
mkdir restore_mongodb
tar -zxf graylog_mongodb_backup2023-08-19_22_10_50.tar.gz -C restore_mongodb/
还原命令
mongorestore --drop -h 127.0.0.1 -d graylog /root/restore_mongodb/graylog/ -u graylog
输入MongoDB数据库账号graylog的密码
(图片点击放大查看)
还原完成,无报错说明还原成功
(图片点击放大查看)
刷新GrayLog的界面上,看到之前删除的配置已经还原回来了
(图片点击放大查看)
Tips
结合crontab进行每天定时备份
0 1 * * * /opt/graylog_mongodb_backup.sh