首页 > 其他 > 详细

rsync备份服务

时间:2019-07-12 23:07:10      阅读:104      评论:0      收藏:0      [点我收藏+]
[root@backup ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

172.16.1.5   lb01
172.16.1.6   lb02
172.16.1.7   web01
172.16.1.8   web02
172.16.1.9   web03
172.16.1.31  nfs01
172.16.1.41  backup
172.16.1.51  db01
172.16.1.61  m01

[root@backup ~]# getenforce 
Disabled
[root@backup ~]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)


[root@backup ~]# rpm -qa | grep rsync
rsync-3.1.2-4.el7.x86_64

[root@backup ~]# yum -y install rsync

[root@backup ~]# rsync --version
rsync  version 3.1.2  protocol version 31
Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
    64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
    socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
    append, ACLs, xattrs, iconv, symtimes, prealloc

rsync comes with ABSOLUTELY NO WARRANTY.  This is free software, and you
are welcome to redistribute it under certain conditions.  See the GNU
General Public Licence for details.

rsync相当于cp/scp/rm命令
[root@backup ~]# cp /etc/hosts /tmp/hosts_bak01
[root@backup ~]# ls -l /tmp/
total 4
-rw-r--r-- 1 root root 327 Jul 13 05:49 hosts_bak01

[root@backup ~]# rsync /etc/hosts /tmp/hosts_bak02
[root@backup ~]# ls -l /tmp/
total 8
-rw-r--r-- 1 root root 327 Jul 13 05:49 hosts_bak01
-rw-r--r-- 1 root root 327 Jul 13 05:50 hosts_bak02

[root@backup ~]# scp /etc/hosts root@nfs01:/tmp/hosts_bak01
root@nfs01s password: 
hosts                     100%  327   128.4KB/s   00:00

[root@nfs01 ~]# ls -l /tmp/
total 4
-rw-r--r-- 1 root root 327 Jul 13 05:47 hosts_bak01

[root@backup ~]# rsync /etc/hosts root@nfs01:/tmp/hosts_bak02
root@nfs01s password:

[root@nfs01 ~]# ls -l /tmp/
total 8
-rw-r--r-- 1 root root 327 Jul 13 05:47 hosts_bak01
-rw-r--r-- 1 root root 327 Jul 13 05:48 hosts_bak02

[root@backup ~]# mkdir -p /root/data/mysql/3306/{data,logs,tmp}

[root@backup ~]# scp -rp /root/data root@nfs01:/tmp
root@nfs01s password:
[root@nfs01 ~]# tree /tmp/
/tmp/
├── data
│   └── mysql
│       └── 3306
│           ├── data
│           ├── logs
│           └── tmp
├── hosts_bak01
└── hosts_bak02

6 directories, 2 files

[root@nfs01 ~]# rm -rf /tmp/*
[root@backup ~]# rsync -rp /root/data root@nfs01:/tmp
root@nfs01‘s password:
[root@nfs01 ~]# tree /tmp/
/tmp/
└── data
    └── mysql
        └── 3306
            ├── data
            ├── logs
            └── tmp

6 directories, 0 files


利用rsync传输目录数据
目录数据后面有/  /data/   将目录下面的数据内容进行传输
目录数据后面没/  /data    将目录本身和目录下面的数据内容进行传输

[root@backup ~]# mkdir /test
[root@backup ~]# touch /test/test{01..10}.txt
[root@backup ~]# ls -l /test/
total 0
-rw-r--r-- 1 root root 0 Jul 13 06:08 test01.txt
-rw-r--r-- 1 root root 0 Jul 13 06:08 test02.txt
-rw-r--r-- 1 root root 0 Jul 13 06:08 test03.txt
-rw-r--r-- 1 root root 0 Jul 13 06:08 test04.txt
-rw-r--r-- 1 root root 0 Jul 13 06:08 test05.txt
-rw-r--r-- 1 root root 0 Jul 13 06:08 test06.txt
-rw-r--r-- 1 root root 0 Jul 13 06:08 test07.txt
-rw-r--r-- 1 root root 0 Jul 13 06:08 test08.txt
-rw-r--r-- 1 root root 0 Jul 13 06:08 test09.txt
-rw-r--r-- 1 root root 0 Jul 13 06:08 test10.txt

[root@backup ~]# mkdir /null

[root@backup ~]# rsync -avz --delete /null/ /test/
sending incremental file list
deleting test10.txt
deleting test09.txt
deleting test08.txt
deleting test07.txt
deleting test06.txt
deleting test05.txt
deleting test04.txt
deleting test03.txt
deleting test02.txt
deleting test01.txt

sent 39 bytes  received 152 bytes  382.00 bytes/sec
total size is 0  speedup is 0.00
[root@backup ~]# ls -l /null/
total 0
[root@backup ~]# ls -l /test/
total 0

--delete 无差异同步参数  将两个目录中的数据保持高度一致
我的东西     /null  你也有
我没有的东西 /null  你也不能有

[root@backup ~]# ls /etc/hosts
/etc/hosts
[root@backup ~]# rsync /etc/hosts
-rw-r--r--            327 2019/07/12 06:04:32 hosts

[root@backup ~]# rsync /root
drwxr-xr-x            128 2019/07/13 06:07:36 root
[root@backup ~]# rsync /root/
drwxr-xr-x            128 2019/07/13 06:07:36 .
-rw-r--r--             96 2019/07/13 06:06:17 .bash_history
-rw-r--r--             18 2019/07/13 06:06:31 .bash_logout
-rw-r--r--            176 2019/07/13 06:07:03 .bash_profile
-rw-r--r--            271 2019/07/13 06:06:42 .bashrc
-rw-r--r--            100 2019/07/13 06:07:14 .cshrc
-rw-r--r--            129 2019/07/13 06:07:26 .tcshrc
-rw-------          3,863 2019/07/13 06:07:36 .viminfo


rsync命令详细用法说明
Local:  rsync [OPTION...] SRC... [DEST]
本地复制备份文件 == cp 
SRC  要备份的数据
DEST 要备份到什么路径中


    Access via remote shell:
    远程复制备份文件
    Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]  远端数据  拉取  本地
    [USER@]: 指定以什么用户身份登录到远程主机(默认是当前系统用户)
    HOST:    远程主机IP地址或者主机名称
    SRC:      远程要拉取的数据信息
    DEST:     保存到本地路径信息

    Push: rsync [OPTION...] SRC... [USER@]HOST:DEST    本地数据  推送  远端
    [USER@]: 指定以什么用户身份登录到远程主机(默认是当前系统用户)
    HOST:    远程主机IP地址或者主机名称
    SRC:     本地要推送的数据信息
    DEST:     保存到远程路径信息


    Access via rsync daemon:
    利用守护进程方式
    Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
          rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
    Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
          src:    本地要推送的数据信息
          [USER@]  认证用户名称信息
          HOST:   备份服务器IP地址或者主机名称  host后面需要有两个冒号
          DEST:   指定模块信息  backup
          rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

备份服务部署
服务端部署
[root@backup ~]# yum -y install rsync
[root@backup ~]# rpm -qa | grep rsync
rsync-3.1.2-4.el7.x86_64

[root@backup ~]# vi /etc/rsyncd.conf
#rsync_config
#created by HQ at 2017
##rsyncd.conf start##

uid = rsync
gid = rsync
port = 873
fake super = yes
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
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = "backup dir by oldboy"
path = /backup

[root@backup ~]# useradd rsync -M -s /sbin/nologin
[root@backup ~]# id rsync
uid=1000(rsync) gid=1000(rsync) groups=1000(rsync)

[root@backup ~]# echo "rsync_backup:123" >/etc/rsync.password
[root@backup ~]# chmod 600 /etc/rsync.password
[root@backup ~]# ls -l /etc/rsync.password
-rw------- 1 root root 17 Jul 13 06:20 /etc/rsync.password

[root@backup ~]# mkdir /backup -p
[root@backup ~]# chown rsync:rsync /backup
[root@backup ~]# ls -ld /backup
drwxr-xr-x 5 rsync rsync 39 Jul 12 03:52 /backup

[root@backup ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
   Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-07-13 06:22:21 CST; 15s ago
 Main PID: 7343 (rsync)
   CGroup: /system.slice/rsyncd.service
           └─7343 /usr/bin/rsync --daemon --no-detach

Jul 13 06:22:21 backup systemd[1]: Started fast remote fi...
Hint: Some lines were ellipsized, use -l to show in full.

客户端备份存储测试
[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@backup::backup
Password: 
sending incremental file list
hosts

sent 214 bytes  received 43 bytes  102.80 bytes/sec
total size is 327  speedup is 1.27

[root@backup ~]# ls -l /backup/
total 4
drwxr-xr-x 2 root  root   29 Jul 12 03:50 conf
-rw-r--r-- 1 rsync rsync 327 Jul 12 23:15 hosts
drwxr-xr-x 2 root  root   54 Jul 12 05:39 sh
drwxr-xr-x 2 root  root   49 Jul 12 03:52 sql
[root@backup ~]# rm -rf /backup/hosts

客户端部署
创建密码文件
[root@nfs01 ~]# echo "123" > /etc/rsync.password
[root@nfs01 ~]# chmod 600 /etc/rsync.password
[root@nfs01 ~]# ls -l /etc/rsync.password
-rw------- 1 root root 4 Jul 13 06:25 /etc/rsync.password

免交互传输数据测试
[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@backup::backup --password-file=/etc/rsync.password
sending incremental file list
hosts

sent 214 bytes  received 43 bytes  514.00 bytes/sec
total size is 327  speedup is 1.27

[root@backup ~]# ls -l /backup/
total 4
drwxr-xr-x 2 root  root   29 Jul 12 03:50 conf
-rw-r--r-- 1 rsync rsync 327 Jul 12 23:15 hosts
drwxr-xr-x 2 root  root   54 Jul 12 05:39 sh
drwxr-xr-x 2 root  root   49 Jul 12 03:52 sql



操作常见问题:
问题一:
# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup
Password: 
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]
传输失败原因:
auth failed on module backup  --- 认证失败
1)用户名不正确/密码不正确
2)密码文件编写格式不正确/密码文件名称不正确    rsync_backup:oldboy123  
3)密码文件权限不正确                           600

问题二:
# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup
@ERROR: Unknown module ‘backup‘
rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]
传输失败原因:
Unknown module ‘backup‘   backup模块未知 

问题三:复制配置文件有注释信息


rsync命令参数说明
    -v   increase verbosity
         显示数据传输过程信息
    -z   compress file data during the transfer
         将传输的数据进行压缩处理
    -a   archive mode
         归档参数:等价于输入了 -rlptgoD
    -r   递归传输数据信息(传输目录)
        -l   copy symlinks as symlinks  最没有意义参数
         保持链接文件属性
    -L   将链接文件指向的源文件进行备份传输
    -p   保持文件权限不变
    -t   保持文件时间信息不变 (修改时间不变)
    -g   保持文件属组信息不变  
    -o   保持文件属主信息不变
    -D   保持设备文件属性信息不变
    --delete   实现无差异同步参数  
    让服务器目录中数据信息  和  备份目录中的数据信息   保持高度一致
    --exclude=PATTERN       exclude files matching PATTERN     tar
    --exclude-from=FILE     read exclude patterns from FILE
                            排除指定数据信息不做备份传输

 

rsync备份服务

原文:https://www.cnblogs.com/zhouwanchun/p/11178796.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!