本文目标
在Ubuntu 18.04.6 LTS上,使用lsyncd+rsyncd 实现gitlab的备份,主服务器宕机后,可以使用备份服务器的gitlab
前置步骤
主服务器,IP:10.100.95.19,已安装gitlab,并且gitlab中有代码。
备份服务器,IP:10.100.95.51,刚安装gitlab
基础知识
1.rsyncd是 rsync 的守护进程模式,可以在服务器上运行,允许客户端通过网络连接到服务器进行文件同步。安装了rsyncd服务器属于是接收端,接收其他服务器上传过来的文件
2.lsyncd 是一个简单高效的文件同步工具,通过lua语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的 inotify 触发机制,然后通过rsync去差异同步,达到实时的效果。
3.所以rsyncd应该装在备份服务器上,lsyncd应该装在主服务器上(这两个ubuntu已经默认安装)
操作步骤
一、备份服务器10.100.95.51
1.修改配置文件
vi /etc/rsyncd.conf
配置文件如下: 注意修改hosts allow 和 path
#配置参考:https://blog.csdn.net/Tassel_YUE/article/details/139005540
# sample rsyncd.conf configuration file
# GLOBAL OPTIONS
#motd file=/etc/motd
#log file=/var/log/rsyncd
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
# The init.d script does its own pid file handling,
# so omit the "pid file" line completely in that case.
# pid file=/var/run/rsyncd.pid
#syslog facility=daemon
#socket options=
#指定守护进程运行的用户ID
uid = root
#指定守护进程运行的组ID
gid = root
#是否使用 chroot 将守护进程限制在某个目录内。默认值是 yes,提高安全性
use chroot = yes
#并发数
max connections = 4
#日志、进程、锁等文件
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
#忽略输出的报错
ignor errors
#允许访问的网段或主机
hosts allow = 10.100.95.19
[opt] #客户端访问的名字,可以配置多个
#服务端的存储路径
path = /var/opt/gitlab/
#备注信息
comment = Backup Directory
#是否以只读模式运行该模块。yes 表示只读,no 表示可读写
read only = no
#是否列出该模块,yes 表示列出,no 表示隐藏
list = yes
#不需要真实在服务端上的用户,仅存于密码文件以`k:v`键值对的形式存放
auth users = user
#指定存放用户认证信息的文件位置
secrets file = /etc/rsyncd.secrets
[etc] #客户端访问的名字,可以配置多个
#服务端的存储路径
path = /etc/gitlab
#备注信息
comment = Backup Directory
#是否以只读模式运行该模块。yes 表示只读,no 表示可读写
read only = no
#是否列出该模块,yes 表示列出,no 表示隐藏
list = yes
#不需要真实在服务端上的用户,仅存于密码文件以`k:v`键值对的形式存放
auth users = user
#指定存放用户认证信息的文件位置
secrets file = /etc/rsyncd.secrets
[mnt] #客户端访问的名字,可以配置多个
#服务端的存储路径
path = /mnt
#备注信息
comment = Backup Directory
#是否以只读模式运行该模块。yes 表示只读,no 表示可读写
read only = no
#是否列出该模块,yes 表示列出,no 表示隐藏
list = yes
#不需要真实在服务端上的用户,仅存于密码文件以`k:v`键值对的形式存放
auth users = user
#指定存放用户认证信息的文件位置
secrets file = /etc/rsyncd.secrets
2.设置账号密码
vi /etc/rsyncd.secrets
# 写入账号和密码,格式是 账号:密码
# 如 user:pwd
3.设置文件权限
chmod 600 /etc/rsyncd.secrets
4.启动
sudo rsync --daemon
5.其它命令
#停止
sudo kill $(cat /var/run/rsyncd.pid)
#上传文件(其他服务器执行)
rsync -avz /local/path/ backupuser@remotehost::backup
#上传文件(带密码)
rsync -avz /local/path/ user@remotehost::backup --password-file=/path/to/passwordfile
#/path/to/passwordfile 文件中只需要填写密码就可以了
二、主服务器10.100.95.19
1.修改配置文件
vi /etc/lsyncd.conf
配置文件如下
---- 配置参考: https://blog.csdn.net/lishuoboy/article/details/89874479
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
--
-- sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
-- 指定要监控的事件,如,CloseWrite,Modify,CloseWrite or Modify, 默认是CloseWrite
inotifyMode = "CloseWrite or Modify",
nodaemon = false,
-- 继续运行,即使有失败的目标
insist = true,
maxProcesses = 6,
maxDelays = 1000, -- 累计到多少所监控的事件激活一次同步,即使后面的delay延迟时间还未到
statusInterval = 5,
}
sync {
default.rsync,
source = "/var/opt/gitlab/", -- 需要同步的目录
target = "user@10.100.95.51::opt", -- 同步的目的地址配置
exclude = { "backups" ,"gitlab-ci", "sockets", "gitlab.yml", "redis", "postmaster.pid","recovery.conf","postgresql.conf","pg_hba.conf","nginx"},
delete = true,
delay = 15,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
-- 不改变文件拥有者(重要)
owner = true,
-- 不改变文件权限(重要)
perms = true,
password_file = "/usr/rsyncd/passwordfile",
-- 网速限制,单位kb/s
_extra = {"--bwlimit=2000"}
}
}
sync {
default.rsync,
source = "/etc/gitlab/", -- 需要同步的目录
target = "user@10.100.95.51::etc", -- 同步的目的地址配置
delete = true,
delay = 15,
exclude = { "initial_root_passwordi","gitlab.rb"},
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
owner = true,
perms = true,
password_file = "/usr/rsyncd/passwordfile",
-- 网速限制,单位kb/s
_extra = {"--bwlimit=2000"}
}
}
sync {
default.rsync,
source = "/mnt", -- 需要同步的目录
target = "user@10.100.95.51::mnt", -- 同步的目的地址配置
delete = true,
delay = 15,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
owner = true,
perms = true,
password_file = "/usr/rsyncd/passwordfile",
-- 网速限制,单位kb/s
_extra = {"--bwlimit=2000"}
}
}
2.设置账号密码
vi /usr/rsyncd/passwordfile
#输入user的密码,上一步中我设置为了pwd
#设置文件权限
chmod 600 /usr/rsyncd/passwordfile
3.创建日志文件夹
mkdir -p /var/log/lsyncd
4.启动
lsyncd -log Exec /etc/lsyncd.conf
systemctl start lsyncd
5.日志 启动后可以到日志文件查看是否开始同步,日志文件在配置文件中指定了存在/var/log/lsyncd/lsyncd.log
不足之处
目前主服务器上的项目变更(新增代码、新增分支等等)是会同步到备份服务器的,但是新建项目无法自动同步过去,原因已经定位到是postgresql的原因,需要在备份服务器上执行
gitlab-ctl stop postgresql
gitlab-ctl start postgresql
以重启postgresql才可以同步到新的项目,并且重启需要几分钟才完成,可以使用命令查看postgresql日志
gitlab-ctl tail postgresql
原因应该是直接同步postgresql的文件,没法直接显示到表里。重启的话,因为某些状态不一致,需要重试多次后postgresql自动执行恢复,才能从文件中恢复过来。
如果有更好的方法,可以在评论区附一下博客链接