rsyncd服务及搭建备份服务器
1、备份服务器backup操作
##1)安装 [root@backup ~]# rsync --version <=======查看版本rsync version3.1.2protocol version31[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 Linux#site: http://www.linux.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 <==========锁文件
logfile = /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 linux backup! <==说明注释
path = /backup/ <==============服务端用户备份的目录,用户和组。
rsync.rsync fake super = yes <==============不用root用户也可以存储文件的完整属性。
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-x2rsync rsync64月1512:12/backup/
4)启动和检查rsyncd服务和服务状态
rsync --daemon(c6及以前) systemctl start rsyncd systemctlenablersyncd systemctl status rsyncd
检查进程
[root@backup ~]# ps -ef|grep sync|grep -v grep #检查进程root75211011:39?00:00:00/usr/bin/rsync --daemon --no-detach
检查端口
[root@backup ~]# netstat -lntup|grep 873 #检查端口tcp000.0.0.0:8730.0.0.0:*LISTEN7521/rsync tcp600:::873:::*LISTEN7521/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-------1root root204月1511: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-------1root root74月1511: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.passwordsending incremental file list hosts sent89bytes received49bytes276.00bytes/sec total sizeis332speedupis2.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::backupsending incremental file list sent52,071bytes received644bytes105,430.00bytes/sec total sizeis31,244,350speedupis592.70
6、测试增量 客户端操作
[root@nfs01 ~]# touch /etc/oldboy.txt[root@nfs01 ~]# rsync -avz /etc rsync_backup@172.16.1.41::backupsending incremental file list etc/oldboy.txtsent52,098bytes received655bytes105,506.00bytes/sectotal sizeis31,244,350speedupis592.28
7、基本常见的的错误
1、服务端密码文件属性不对
[root@nfs01 opt]# rsync -avz rsync_backup@172.16.1.41::backup /opt/ --password-file=/etc/rsync.password
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1648) [Receiver=3.1.2]
2、客户端密码文件权限不对
[root@nfs01 opt]#
[root@nfs01 opt]# chmod 644 /etc/rsync.password
[root@nfs01 opt]# rsync -avz rsync_backup@172.16.1.41::backup /opt/ --password-file=/etc/rsync.password
ERROR: password file must not be other-accessible
rsync error: syntax or usage error (code 1) at authen ticate.c(196) [Receiver=3.1.2]
3、相关用户没有创建
[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/
4、/etc/rsyncd.conf配置缺少 "fake super = yes"参数
[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
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
增加如下参数到/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
5、权限被拒绝
[root@zgw /]# rsync -avz /backup/ rsync_backup@172.16.1.41::backup
Password:
sending incremental file list
rsync: failed to read xattr user.rsync.%stat for "." (in backup): Permission denied (13)
rsync: failed to read xattr user.rsync.%stat for "." (in backup): Permission denied (13)
rsync: failed to set times on "." (in backup): Permission denied (13)
rsync: failed to read xattr user.rsync.%stat for "." (in backup): Permission denied (13)
./
rsync: recv_generator: mkdir "1234" (in backup) failed: Permission denied (13)
*** Skipping any contents from this failed directory ***
rsync: failed to read xattr user.rsync.%stat for "." (in backup): Permission denied (13)
1234/
sent 89 bytes received 621 bytes 284.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
解决方法:现阶段关闭selinux,firewalld,iptables等
6、服务无法启动
相关配置文件被改了,将配置文件名称恢复