Eurusan

Mongo数据备份

Mongo数据备份

mongodb+rsync+crontab

获取需要备份文件 mongodb

项目中需要备份的文件有两种,

  1. mongo的数据
  2. docs下的excel和word文件

mongo原生的三个命令可以从数据库中导出数据文件,导入数据文件,以及通过文件对数据库进行表级字段级操作。

(以下只涉及到备份预案)

常用的mongo数据库导出
mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径

不需要展开的文件目录,mongo提供了导出压缩文件的命令,每天备份一次,文件名使用日期-时间戳-数据库.archive的形式,–archive和-o不兼容,只能同时使用一个命令。
获取时间戳
HOD=$(date +%s)
获取yyyyMMdd格式日期
TODAY=$(date +%Y%m%d)
导出存档文件
mongodump -h "$DB_HOST" --port "$DB_PORT" -d "$DB_NAME" --gzip --archive="$ARCHIVE"

推送到备份服务器 rsync

rsync具有ssh和daemon两种服务模式,两种又可以进行推||拉两种同步方式。(我个人直觉上比较喜欢资源服务器搭建好后,自由拉取。或者由一个中心服务器控制文件流动的,而不是资源服务器自动推。所以使用了daemon和pull的模式,因为拉取在备份服务器上运行脚本,是否成功获取也很容易通过一些脚本处理。)
daemon的主要配置在rsyncd.conf文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
pid file = /var/run/rsyncd.pid # The process id file the daemon uses.
lock file = /var/run/rsync.lock # The daemon lock file.
log file = /var/log/rsync.log # The location of the log file.
port = 12000

# [files] This is the module name.
# The name used here is what you’ll be putting in the rsync pull command as the first part of the source (/files/../..).
# You can name it what you’d like and can have as many as you’d like.
[files]
path = /home/data/files # The file path for files associated with this module.
comment = RSYNC FILES # Descriptive comment for this module.
read only = no # This tells the daemon the directory for this module is read-only. You cannot upload to it. For upload only, use upload only = true.
write only = no
list = yes
# 24标志c类网段
hosts allow = ipaddr/24
timeout = 300 # Time, in seconds, the rsync daemon will wait before terminating a dead conenction.

[mongo]
path = /home/data/mongo
comment = MONGODB BACKUP
read only = no
write only = no
list = yes
# ipaddr like 127.0.0.1
hosts allow = ipaddr/24 ipaddr/24
timeout = 300

[docs]
path = /home/static/docs
comment = DOCS BACKUP
read only = no
write only = no
list = yes
hosts allow = ipaddr/24 ipaddr/24
timeout = 300

启动

1
rsync --daemon

检查启动情况

1
ps x | grep rsync

停止daemon

1
kill `cat /var/run/rsyncd.pid`

通过xinetd守护进程

备份服务器拉取数据
查看rsync服务器中可以同步的目录列表

1
rsync -rdt rsync://IPADDR:RsyncPort/

可以反复执行拿到下级目录

1
rsync -rdt rsync://IPADDR:RsyncPort/DirectoryName

找到资源后 同步到目标路径

1
rsync -rdt rsync://IPADDR:RsyncPort/DirectoryName/File /DestinationDirectory/

MacOS因为是NFS的数据格式,同步的时候需要使用 -O参数,SRC为资源路径 DEST为目标路径,-avu, -rdt同步中的细节调整,详看文档

1
2
3
4
5
6
#!/usr/bin/env bash
RSYNC_SERVER=rsync://127.0.0.1:12000
RSYNC_DIR=files
RSYNC_SRC="$RSYNC_SERVER"/"$RSYNC_DIR"
RSYNC_DEST="/data/server_back"
sudo rsync -avu -O --progress "$RSYNC_SRC" "$RSYNC_DEST"

定时执行获取&&推送操作 crontab

crontab是系统级的定时任务
crontab -e 编辑定时任务
crontab -l 查看定时任务列表
保存后系统就会按照crontab执行计划任务
crontab 除了执行shell外还行执行其他脚本
*/1 * * * * /usr/bin/python /root/images/color.py 如可以执行python解释器来执行python脚本,改脚本为执行color.py/1 minute

遇到的问题

Destination host unreachable https://blog.csdn.net/yzit0905/article/details/52748122

ref

知乎上关于mongodb backup的文章
https://zhuanlan.zhihu.com/p/29809036

github上自动备份mongo的脚本
https://github.com/micahwedemeyer/automongobackup/blob/master/src/automongobackup.sh

mongodb压缩,过去需要额外执行压缩命令,3.2引入了–gzip
https://www.cnblogs.com/xuliuzai/p/9594138.html

mongo官方文章,存档与压缩
https://www.mongodb.com/blog/post/archiving-and-compression-in-mongodb-tools

来自linux公社,rsync实现文件同步配置与使用
https://www.linuxidc.com/Linux/2012-10/71704.htm

来自linux公社,rsync实现文件同步配置排除故障
https://www.linuxidc.com/Linux/2012-10/71705.htm

! rsync 使用详解
https://www.atlantic.net/hipaa-compliant-cloud-storage/how-to-setup-rsync-daemon-linux-server/

rsync 使用详解 相当于上一片的中文版
https://segmentfault.com/a/1190000000444614
wiki关于xinted文章
https://en.wikipedia.org/wiki/Xinetd

20个crontab的用例,常见写法与基本命令
https://www.jianshu.com/p/d93e2b177814

crontab语法辅助工具
https://crontab.guru/#52_0-23_*_*_*