天天看点

Zhong__Linux下使用rsync实现两台主机远程备份文件服务端:客户端: 

主题:实现服务端文件定时增量备份到客户端

环境:服务端(CentOStart7.6) 客户端(CentOStart7.6)  两台皆为云服务器

目的:实现服务端文件每天自动备份到客户端,并删除超过30天的文件!

时间:2019.08.12

工具:rsync

作者:Zhong

  对于MySQL远程备份前面的文章有写过,有需要的请前去查看!

目前CentOS7.6系统自带rsync工具,无需再安装!需要配置的文件建议做好备份再修改!

服务端:

配置rsyncd.conf:

vim rsyncd.conf
           

初始文件:

# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
           

修改为如下:

# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = root
gid = root
use chroot = no
#最大连接
max connections = 4
# pid file = /var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
#日志文件路径
log file = /var/log/rsyncd.log     
transfer logging = yes
# exclude = lost+found/
# transfer logging = yes
#端口  默认为873
#port=873
#连接超时时间
timeout = 900
#忽略不可读文件
ignore nonreadable = yes
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

#[]中为名字,可随意
[rsync_file]
path = /home/test
#comment 为描述说明
comment = test file
list=yes
ignore errors
list=yes
ignore errors
#认证用户  和系统用户无关 作为rsync工具认证使用
auth users = test
#密码文件
secrets file = /etc/rsync/screts.pas
           

 以上配置主要指定服务端要备份的文件路径,客户端备份时只需指定名为rsync_file的配置即可找到path对应的文件!并配置了用户及密码文件!

配置密码文件screts.pas(对应rsyncd.conf中指定的路径、文件名):

vim /etc/rsync/screts.pas  #如果没有rsync目录则创建
           

添加用户名:密码并保存: 

Zhong__Linux下使用rsync实现两台主机远程备份文件服务端:客户端: 

此文件权限必须设置其它组用户不可访问(服务端和客户端都要设置)

chmod 600 /etc/rsync/screts.pas
           

如果不做上面的配置会出现这种提示:

ERROR: password file must not be other-accessible

rsync error: syntax or usage error (code 1) at authenticate.c(196) [Receiver=3.1.2]

启动服务:

rsync --daemon --config=/etc/rsyncd.conf
           

客户端: 

创建密码文件(密码和服务端定义的对应):

echo  "admin123" > /root/Desktop/test/passwd 
           

 修改权限:

chmod 600 passwd
           
  •  即时备份(192.192.192.192为举例IP,实际IP对应服务端的IP):
rsync -avz --password-file=/root/Desktop/test/passwd  [email protected]::rsync_file
           

参数可根据自己目的调整,自行搜索!这儿-a代表归档,保持源文件属性![email protected] 192.192.192.192::rsync_file  test为服务端定义的用户,rsync_file是服务端定义的要备份的文件的名字!

此时执行命令后,文件就开始备份了!

  • 定时备份(本实验的目的)

创建rsync.sh文件: 

vim rsync.sh
           

添加上面的命令:

rsync -avz --password-file=/root/Desktop/test/passwd  [email protected]::rsync_file
           

 给rsync.sh文件增加可执行的权限:

chmod +x rsync.sh
           

配置crontab定时任务:

crontab -e
           

添加如下配置(路径对应rsybc.sh文件路径):

40  10 * * * /root/Desktop/test_backup/rsync.sh
           

 上面的配置为每天10:40执行定时任务,执行rsync.sh文件。

备注:

如果为云服务器的话,请在后台配置相关策略,开放要用到的端口如873或者12000

如果服务端指定port,那么客户端也要指定:

例如:

服务端:

port=12000

客户端:

rsync -avz --password-file=/root/Desktop/test/passwd --port=12000 [email protected]::rsync_file /root/Desktop/test/ 

可以增加定时删除超过规定时间的文件:

crontab -e
           

添加一行命令:

30 22 * * * find /root/Desktop/backup_test/ -mtime +30 -name "*tar.gz" -exec rm -rf {} \;
           

上面的命令为每天22:30查找并删除/root/Desktop/backup_test/目录中创建超过30天的以tar.gz结尾的文件 !

 关注微信公众号:

Zhong__Linux下使用rsync实现两台主机远程备份文件服务端:客户端: 

qq交流群:

 121160124

继续阅读