rsyncd服务及搭建备份服务器
1、备份服务器backup操作
##1)安装
[root@backup ~]# rsync --version <=======查看版本
rsync version 3.1.2 protocol version 31
[root@backup ~]# yum install rsync <=======安装
2)配置配置文件/etc/rsyncd.conf
[root@backup ~]# cp /etc/rsyncd.conf{,.ori} <=======备份文件
[root@backup ~]#cat>/etc/rsyncd.conf<<EOF <=======设置配置文件
#rsync_config_______________start
#created by oldboy
#site: http://www.oldboyedu.com
uid = rsync <========================管理备份目录的用户
gid = rsync <========================管理备份目录的用户组
use chroot = no <==================安全功能,数据是否锁定到备份目录
max connections = 200 <==============并发链接,同时多少客户端访问
timeout = 600 <==============超时时间
pid file = /var/run/rsyncd.pid <==========进程号所在文件
lock file = /var/run/rsync.lock <==========锁文件
log file = /var/log/rsyncd.log <==========日志文件,查看报错等
ignore errors <==============忽略错误
read only = false <==============可写
list = false <==============不允许列表
hosts allow = 172.16.1.0/24 <=========哪些主机可以访问 和下面2选一
hosts deny = 0.0.0.0/32 <=========哪些主机不允许访问
auth users = rsync_backup <=========远程虚拟链接用户
secrets file = /etc/rsync.password <=======存放密码的文件:格式:用户名:密码 权限 必须600
[backup] <==============模块名 远程访问使用模块名访问
comment = welcome to oldboyedu backup! <==说明注释
path = /backup/ <==============服务端用户备份的目录,用户和组。rsync.rsync
fake super = yes <==============不用root用户也可以存储文件的完整属性。
EOF
man rsync 查命令的参数
man rsyncd.conf 查配置参数
https://www.samba.org/ftp/rsync/rsync.html
3)创建用户和备份目录
[root@backup ~]# useradd rsync <===========创建rsync用户
[root@backup ~]# id rsync <===========查看rsync用户
uid=1001(rsync) gid=1001(rsync) 组=1001(rsync)
[root@backup ~]# mkdir -p /backup <===========创建目录/backup
[root@backup ~]# chown -R rsync.rsync /backup/ <====修改/backup目录所属用户 用户组
[root@backup ~]# ls -ld /backup/ <===========查看/backup目录属性信息
drwxr-xr-x 2 rsync rsync 6 4月 15 12:12 /backup/
4)启动和检查rsyncd服务和服务状态
rsync --daemon(c6及以前)
systemctl start rsyncd
systemctl enable rsyncd
systemctl status rsyncd
检查进程
[root@backup ~]# ps -ef|grep sync|grep -v grep #检查进程
root 7521 1 0 11:39 ? 00:00:00 /usr/bin/rsync --daemon --no-detach
检查端口
[root@backup ~]# netstat -lntup|grep 873 #检查端口
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 7521/rsync
tcp6 0 0 :::873 :::* LISTEN 7521/rsync
[root@backup ~]# lsof -i :873 #检查端口
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 7521 root 3u IPv4 41439 0t0 TCP *:rsync (LISTEN)
rsync 7521 root 5u IPv6 41440 0t0 TCP *:rsync (LISTEN)
5)配置密码文件
[root@backup ~]# echo "rsync_backup:oldboy" > /etc/rsync.password <===创建用户信息和密码文件
[root@backup ~]# chmod 600 /etc/rsync.password <===修改文件属性 必须为600
[root@backup ~]# cat /etc/rsync.password <===查看用户信息和密码文件
rsync_backup:oldboy
[root@backup ~]# ls -l /etc/rsync.password <===查看文件权限信息
-rw------- 1 root root 20 4月 15 11:51 /etc/rsync.password
rsync服务端配置完成。
2、客户端服务器操作web01或nfs01
最好使用第二种
方法1:认证密码文件
[root@nfs01 ~]# echo "oldboy" > /etc/rsync.password <===设置密码文件
[root@nfs01 ~]# chmod 600 /etc/rsync.password <===修改文件属性 必须为600
[root@nfs01 ~]# cat /etc/rsync.password <===查看用户密码文件
oldboy
[root@nfs01 ~]# ls -l /etc/rsync.password <===查看文件权限信息
-rw------- 1 root root 7 4月 15 11:55 /etc/rsync.password
方法2:
[root@nfs01 ~]# echo ' export RSYNC_PASSWORD=oldboy' >>/etc/bashrc <===设置全局变量
[root@nfs01 ~]# tail -1 /etc/bashrc <===查看变量信息
export RSYNC_PASSWORD=oldboy
[root@nfs01 ~]# . /etc/bashrc <=== 使变量生效
[root@nfs01 ~]# echo $RSYNC_PASSWORD <===输出变量 检查
oldboy
rsync客户端 nfs01 配置完成
3、守护进程模式,客户端rsync的命令语法:
配置服务器端守护进程,实现数据传输:
1)服务器端守护进程。2、客户端执行命令。
## 1)pull,拉:从远端拉取到本地。
语法1(常用):
rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync命令 参数选项 [虚拟用户]@[主机地址]::[模块名] 本地路径
语法2:
rsync [OPTION…] rsync://[USER@]HOST::SRC… [DEST]
rsync命令 参数选项 rsync://[虚拟用户]@[主机地址]/[模块名] 本地路径
2)push,推:从本地推到远端。
语法1(常用):
rsync [OPTION…] [DEST] [USER@]HOST::SRC…
rsync命令 参数选项 本地路径 [虚拟用户]@[主机地址]::[模块名]
语法2:
rsync [OPTION…] [DEST] rsync://[USER@]HOST::SRC…
rsync命令 参数选项 本地路径 rsync://[虚拟用户]@[主机地址]/[模块名]
4、测试成果 成功案例
客户端操作:把/etc/hosts推送到服务端/backup/
[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
hosts
sent 89 bytes received 49 bytes 276.00 bytes/sec
total size is 332 speedup is 2.41
客户端操作:把/etc/推送到服务端/backup/
rsync -avz /etc rsync_backup@172.16.1.41::backup –password-file=/etc/rsync.password
服务端检查:
[root@backup ~]# ls /backup/
hosts
[root@backup ~]# ls /backup/
etc hosts
5、注意:
如果客户端按照环境变量的方式配置,则可以忽略--password-file=/etc/rsync.password参数。
[root@nfs01 ~]# rsync -avz /etc rsync_backup@172.16.1.41::backup
sending incremental file list
sent 52,071 bytes received 644 bytes 105,430.00 bytes/sec
total size is 31,244,350 speedup is 592.70
6、测试增量 客户端操作
[root@nfs01 ~]# touch /etc/oldboy.txt
[root@nfs01 ~]# rsync -avz /etc rsync_backup@172.16.1.41::backup
sending incremental file list
etc/oldboy.txt
sent 52,098 bytes received 655 bytes 105,506.00 bytes/sec
total size is 31,244,350 speedup is 592.28
7、可能遇见的错误
##错误1:
[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup –password-file=/etc/rsync.password
@ERROR: invalid uid rsync
rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]
没有创建用户和目录 并修改用户 用户组
解答:
[root@backup ~]# useradd rsync
[root@backup ~]# id rsync
uid=1001(rsync) gid=1001(rsync) 组=1001(rsync)
[root@backup ~]# mkdir -p /backup
[root@backup ~]# chown -R rsync.rsync /backup/
[root@backup ~]# ls -ld /backup/
drwxr-xr-x 2 rsync rsync 6 4月 15 12:12 /backup/
错误2:
[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
hosts
rsync: chgrp ".hosts.YDuTjO" (in backup) failed: Operation not permitted (1)
sent 223 bytes received 124 bytes 694.00 bytes/sec
total size is 332 speedup is 0.96
没有添加 fake super = yes 到/etc/syncd.conf里
解答:增加如下参数到/etc/rsyncd.conf
fake super = yes #不用root用户也可以存储文件的完整属性。
This allows the full attributes of a file to be stored without having to have the daemon actually running as root.
[root@backup ~]# grep fake /etc/rsyncd.conf
fake super = yes
改完配置,要重启服务:
[root@backup ~]# systemctl restart rsyncd