文件同步部署

论坛 期权论坛 脚本     
已经匿名di用户   2022-7-2 21:57   1940   0

环境准备

Server端: Ubuntu 4.8.2-19ubuntu1

公有ip地址:101.201.101.146

Client端: Ubuntu 5.4.0-6ubuntu1~16.04.5

公有ip地址:39.106.56.35

Server端

修改rsync服务启动的参数

root@iZ25kq9nx52Z:~#vim /etc/default/rsync

RSYNC_ENABLE=true

退出 :wq

移动配置文件rsyncd.conf

root@iZ25kq9nx52Z:~# cp /usr/share/doc/rsync/examples/rsyncd.conf /etc

修改配置文件rsyncd.conf

root@iZ25kq9nx52Z:~# vim /etc/rsyncd.conf

# 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=

# MODULE OPTIONS

[my_rsync_bk]

comment = public archive

path = /mnt/uftp/file

use chroot = yes

#max connections=10

lock file = /var/lock/rsyncd

# the default for read only is yes...

read only = yes

list = yes

uid = root

gid = root

exclude = mp4 MP4cover resources resourcesCover resourcesExcel

# exclude from =

# include =

# include from =

auth users = yanfa_rsync

secrets file = /etc/rsyncd.secrets

strict modes = yes

hosts allow =39.106.56.35

# hosts deny =

ignore errors = yes

ignore nonreadable = yes

transfer logging = yes

log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.

timeout = 600

refuse options = checksum dry-run

dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz

#新增MySQL模块

[my_rsync_mysql]

comment = public archive

path = /usr/local/mysql-bak

use chroot = yes

#max connections=10

lock file = /var/lock/rsyncd

# the default for read only is yes...

read only = yes

list = yes

uid = root

gid = root

exclude = 20170228-moors.sql 20170328-moors.sql.gz.bak backup.log backup.sh*

# exclude from =

# include =

# include from =

auth users = yanfa_rsync

secrets file = /etc/rsyncd.secrets

strict modes = yes

hosts allow =39.106.56.35

# hosts deny =

ignore errors = yes

ignore nonreadable = yes

transfer logging = yes

log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.

timeout = 600

refuse options = checksum dry-run

dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz

创建密码文件

root@iZ25kq9nx52Z:~# vim /etc/rsyncd.secrets

yanfa_rsync:fromyanfa

给密码文件赋予权限

root@iZ25kq9nx52Z:~# chmod 0600 /etc/rsyncd.secrets

查看权限:

root@iZ25kq9nx52Z:~# ll /etc/rsyncd.secrets

-rw------- 1 root root 22 Mar 13 14:49 /etc/rsyncd.secrets

启动rsync

root@iZ25kq9nx52Z:~# /etc/init.d/rsync start

[....] Starting rsync (via systemctl): rsync.service.

查看是否启动服务:

root@iZ25kq9nx52Z:~# ps -ef | grep rsync

root 2235 1 0 13:50 ? 00:00:00 /usr/bin/rsync --no-detach --daemon --config /etc/rsyncd.conf

root 2333 2017 0 14:51 pts/0 00:00:00 grep --color=auto rsync

Client端

创建密码文件

root@iZ2zejccb1trnt5m8yav32Z:~# vim /etc/rsync.pwd

fromyanfa

给密码文件赋予权限

root@iZ2zejccb1trnt5m8yav32Z:~# chmod 0600 /etc/rsync.pwd

root@iZ2zejccb1trnt5m8yav32Z:~# chown root:root /etc/rsync.pwd

(如果是非root用户,只能使用自己当前用户和自己所在的组)

查看权限:

root@iZ2zejccb1trnt5m8yav32Z:~# ll /etc/rsync.pwd

-rw------- 1 root root 10 Mar 13 14:54 /etc/rsync.pwd

添加定时任务

root@iZ2zejccb1trnt5m8yav32Z:~# crontab -e

Select an editor. To change later, run 'select-editor'.

1. /bin/ed

2. /bin/nano <---- easiest

3. /usr/bin/vim.basic

4. /usr/bin/vim.tiny

Choose 1-4 [2]: 3

第一次使用crontab时,会选择编辑器,选第三个vim

1. 客户端拉取moors新增的备份文件:

5 11 * * * rsync -avz --delete --password-file=/etc/rsync.pwd

yanfa_rsync@101.201.101.146::my_rsync_bk /mnt/uftp/file > /etc/msg.log 2>&1 &

2. 客户端拉取moors数据库新增的备份文件:

40 13 * * * rsync -avz --delete --password-file=/etc/rsync.pwd yanfa_rsync@101.201.101.146::my_rsync_mysql /usr/local/mysql-bak >/etc/msg2.log 2>&1 &

查询所有的定时器:

root@iZ2zejccb1trnt5m8yav32Z:~# crontab -l

# m h dom mon dow command

5 11 * * * rsync -avz --delete --password-file=/etc/rsync.pwd yanfa_rsync@101.201.101.146::my_rsync_bk /mnt/uftp/file >/etc/msg.log 2>&1 &

40 13 * * * rsync -avz --delete --password-file=/etc/rsync.pwd yanfa_rsync@101.201.101.146::my_rsync_mysql /usr/local/mysql-bak >/etc/msg2.log 2>&1 &

附录(rsyncd.conf)

# 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.

pid file=/var/run/rsyncd.pid

#指定rsync发送日志消息给syslog时的消息级别,默认值是daemon。

#syslog facility=daemon

#自定义tcp选项,默认是关闭的

#socket options=

#以下是模块信息,我们可以创建多个模块

# MODULE OPTIONS

[ftp]

comment = public archive #模块描述

path = /var/www/pub #需要同步的路径

use chroot = yes #默认是yes,在rsync在传输文件以前首先chroot到path参数指定的目录下。这样做的原因是实现额外的安全防护,但是缺点是需要root权限,并且不能备份指向外部的符号连接指向的目录文件。

# max connections=10 #最大连接数

lock file = /var/lock/rsyncd #指定支持max connections参数的锁文件。

# the default for read only is yes...

read only = yes #只读选项,不让客户端上传文件到服务器上。

list = yes #客户请求时可用模块时是否列出该模块

uid = nobody #设定该模块传输文件时守护进程应该具有的uid

gid = nogroup #设定该模块传输文件时守护进程应具有的gid,此项与uid配合可以确定文件的访问权限

# exclude = #排除目录中不需同步的文件,多个模式列表由空格隔开

# exclude from = #可以指定一个包含exclude模式定义的文件名

# include = #与exclude相似

# include from = #可以指定一个包含include模式定义的文件名

# auth users = #该选项指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块。

# secrets file = /etc/rsyncd.secrets #该文件每行包含一个username:password对,以明文方式存储,只有在auth users被定义时,此选项才生效。同时我们需要将此文件权限设置为0600

strict modes = yes #该选项指定是否监测密码文件的权限,如果该选项值为true那么密码文件只能被rsync服务器运行身份的用户访问,其他任何用户不可以访问该文件。默认值为true

# hosts allow = #允许的主机

# hosts deny = #拒绝访问的主机

ignore errors = no #设定rsync服务器在运行delete操作时是否忽略I/O错误

ignore nonreadable = yes #设定rysnc服务器忽略那些没有访问文件权限的用户

transfer logging = no #使rsync服务器使用ftp格式的文件来记录下载和上载操作在自己单独的日志中

# log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. #设定日志格式

timeout = 600 #超时设置(秒)

refuse options = checksum dry-run #定义一些不允许客户对该模块使用的命令选项列表

dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz #告诉rysnc那些文件在传输前不用压缩,默认已设定压缩包不再进行压缩


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP