rsync守护进程+xinetd管理rsync服务 - rsync传输文件
###
1.语法格式
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
2.服务端部署--rsync(备份服务器)
###########################################################查看是否安装rsync服务 rpm -qa rsync ###########################################################安装rsync服务 yum install -y rsync ###########################################################rsync的配置文件 vim /etc/rsyncd.conf #created by YH at 2017 ##rsyncd.conf start
# 指定rsync服务运行时,向磁盘进行读写操作的操作者 #uid = root #gid = root uid = rsync gid = rsync use chroot = no
# 最大连接数 max connections = 200 # 超时时间
timeout = 300 # 进行对应的进程号文件
pid file = /var/run/rsyncd.pid
# 锁文件 lock file = /var/run/rsync.lock # 日志文件
log file = /var/log/rsyncd.log # 服务端密码文件(设置进行连接认证的秘钥文件) secrets file = /etc/rsync.password
# 模块名称 [backup]
# 描述信息 comment = "backup dir by geyiheng" # 认证用户
auth users = rsync_backup
# 忽略错误程序 ignore errors
# 是否只读 read only = false # 是否可以列表
list = false # 允许访问rsync服务器的客户端ip范围
hosts allow = 172.16.1.0/24
# 禁止访问rsync服务器的客户端ip范围 #hosts deny = 0.0.0.0/32 # 模块对应的位置(路径)
path = /backup ##########################################################创建rsync用户(如果配置文件中uid.gid=root则使用root用户传输不需要创建rsync用户) useradd -s /sbin/nologin -M rsync ##########################################################创建密码文件(必须授权600) echo "rsync_backup:testpasswd123" >/etc/rsync.password chmod 600 /etc/rsync.password ##########################################################创建备份目录 mkdir -p /backup/test ##########################################################授权备份目录 chown -R rsync.rsync /backup ##########################################################非xinetd管理rsync服务是启动rsync命令 #rsync --daemon
3.服务端部署 - xinetd,配置详解,启动方式
#################安装xinetd yum install xinetd -y ###################################################################xinetd服务配置文件 [root@alpha etc]# cat xinetd.conf|egrep -v "^$|#" defaults { # 表示使用syslog进行服务登记 log_type = SYSLOG daemon info # 表示设置失败后记录客户机的IP地址 log_on_failure = HOST # 表示设置成功后记录客户机的IP地址的进程ID log_on_success = PID HOST DURATION EXIT # 表示每秒25个入站连接,如果超过限制,则等待30秒。主要用于对付拒绝服务攻击 cps = 50 10 # 表示最大连接进程数为50个。 instances = 50 per_source = 10 v6only = no groups = yes umask = 002 } # 表示告诉xinetd要包含的文件或目录是/etc/xinetd.d includedir /etc/xinetd.d ####################################################################xinetd生成管理rsync服务配置文件(此文件需要手动生成) vim /etc/xinetd.d/rsync # default: off # description: The rsync server is a good addition to an ftp server, as it \ # allows crc checksumming etc. service rsync { # disable = yes # 将disable参数对应值改为no,表示开启rsync服务服务,否则启动xinetd此rsync service不生效 disable = no flags = IPv6 # 表示服务的数据包类型为stream socket_type = stream # 表示不需等待,即服务将以多线程的方式运行 wait = no # 表示执行此服务进程的用户是root user = root # 启动脚本的位置 server = /usr/bin/rsync # 启动服务参数 server_args = --daemon # 表示设置失败时,UID添加到系统登记表 log_on_failure += USERID } ####################################################################启动xinetd netstat -lntup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1171/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1274/master tcp6 0 0 :::22 :::* LISTEN 1171/sshd tcp6 0 0 ::1:25 :::* LISTEN 1274/master systemctl start xinetd netstat -lntup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1171/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1274/master tcp6 0 0 :::22 :::* LISTEN 1171/sshd tcp6 0 0 ::1:25 :::* LISTEN 1274/master tcp6 0 0 :::873 :::* LISTEN 1413/xinetd
4.客户端的部署--web服务(推)
###########################################################查看是否安装rsync服务 rpm -qa rsync ###########################################################安装rsync服务 yum install -y rsync ###########################################################rsync的密码文件(/etc/rsync.password此文件随意创建,但必须授权600,传输文件时引用) echo "testpasswd123" >/etc/rsync.password chmod 600 /etc/rsync.password ###########################################################测试文件传输(-n参数:测试传输,并不会真正传输文件) rsync -avPn aliyunlogcli.log rsync_backup@172.16.1.44::backup/test --password-file=/etc/rsync.password ###########################################################文件传输 rsync -avP aliyunlogcli.log rsync_backup@172.16.1.44::backup/test --password-file=/etc/rsync.password ###########################################################目录/文件无差异传输(--delete:无差异传输,相同文件名覆盖,差异文件删除) # dir01目录下文件
[root@v01 dir01]# ls file1A.txt fileA2.txt fileA3.txt test
# dir02目录下文件 [root@v01 dir02]# ls file1A.txt fileA2.txt fileA3.txt fileB4.txt # 将dir01目录内的test目录不同步到dir02目录内 [root@v01 ~]# rsync -arvz --exclude="test" --delete dir01/ dir02/ sending incremental file list ./ deleting fileB4.txt sent 75 bytes received 15 bytes 180.00 bytes/sec total size is 0 speedup is 0.00
# 同步完成后dir02目录文件 [root@v01 ~]# ls dir02/ file1A.txt fileA2.txt fileA3.txt ###########################################################排除目录/文件无差异传输(-n:测试传输,未传输|--delete:无差异传输) rsync -avPcn --exclude=/incomming --exclude=init.txt --exclude=redis.properties --delete /home/jenkins/.jenkins/workspace/ rsync_backup@172.16.1.44::backup/test --password-file=/etc/rsync.password
###