使用rsync搭建备份服务器
1. RSYNC介绍
RSYNC是一款开源的,快速的,多功能的,可实现全量及增量的本地或远程数据同步备份的优秀工具。Rsync软件适用于unix/linux/windows等多种操作系统平台。远程数据备份工具,可以实现全备份及增量备份,也可以本地备份。
英文全称是Remote synchronization.
具有以下三种功能:
远程copy的功能:相当于ssh自带的scp命令,但是又优于scp命令,scp每次都是全量拷贝。rsync高在增量拷贝,scp每次都是全量copy。
本地copy功能:相当于是cp命令,但是又优于cp命令,因为cp每次都是全量copy。
rsync还可以实现删除功能:相当于rm命令。
CentOS5.X中自带的rsync版本都是2.6.x的版本,该版本存在的问题主要是性能方面的,比如,同步大量小文件时容易出现内存溢出或同步中断等现象,这主要与其2.6版本采用先列文件列表,再进行同步的处理机制有关,在处理大文件同步时也市场发生同步文件不完整的现象.不过在CentOS6.x系列rsync版本都进行了升级,本篇文章使用的linux系统是CentOS7.9, 自带的rsync的版本是3.1.2.
[root@dbc-server-554 ~]# 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.
[root@dbc-server-554 ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
2. RSYNC的特性
https://download.samba.org/pub/rsync/rsync.html
Some of the additional features of rsync are:
- support for copying links, devices, owners, groups, and permissions
- exclude and exclude-from options similar to GNU tar
- a CVS exclude mode for ignoring the same files that CVS would ignore
- can use any transparent remote shell, including ssh or rsh
- does not require super-user privileges
- pipelining of file transfers to minimize latency costs
- support for anonymous or authenticated rsync daemons (ideal for mirroring)
3. 使用的工作场景
- 把所有客户数据数据同步到备份服务器(可以配合定时任务,实现每天定时备份)
- 实时同步(解决存储服务器的单点故障问题),rsync结合inotify的功能做实时数据同步。
4. RSYNC的工作方式
Rsync大致使用三种主要的传输数据的方式:
- [本地传输模式] 单个主机本地之间的数据传输(此时类似于cp命令的功能)
- [远程shell传输模式] 借助rcp,ssh等通道来传输数据(此时类似于scp命令的功能)
- [守护进程模式] 以守护进程(socket)的方式传输数据(这个是rsync自身的重要的功能)
5. 语法
man rsync
NAME
rsync - a fast, versatile, remote (and local) file-copying tool
SYNOPSIS
Local: rsync [OPTION...] SRC... [DEST]
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST: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
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
Usages with just one SRC arg and no DEST arg will list the source files instead of copying.
OPTIONS SUMMARY
Here is a short summary of the options available in rsync. Please refer to the detailed description below for a complete
description.
-v, --verbose increase verbosity
--info=FLAGS fine-grained informational verbosity
--debug=FLAGS fine-grained debug verbosity
--msgs2stderr special output handling for debugging
-q, --quiet suppress non-error messages
--no-motd suppress daemon-mode MOTD (see caveat)
-c, --checksum skip based on checksum, not mod-time & size
-a, --archive archive mode; equals -rlptgoD (no -H,-A,-X)
--no-OPTION turn off an implied OPTION (e.g. --no-D)
-r, --recursive recurse into directories
-R, --relative use relative path names
--no-implied-dirs don't send implied dirs with --relative
-b, --backup make backups (see --suffix & --backup-dir)
--backup-dir=DIR make backups into hierarchy based in DIR
--suffix=SUFFIX backup suffix (default ~ w/o --backup-dir)
-u, --update skip files that are newer on the receiver
--inplace update destination files in-place
--append append data onto shorter files
--append-verify --append w/old data in file checksum
-d, --dirs transfer directories without recursing
--old-dirs, --old-d works like --dirs when talking to old rsync
-l, --links copy symlinks as symlinks
-L, --copy-links transform symlink into referent file/dir
--copy-unsafe-links only "unsafe" symlinks are transformed
--safe-links ignore symlinks that point outside the tree
--munge-links munge symlinks to make them safer
-k, --copy-dirlinks transform symlink to dir into referent dir
-K, --keep-dirlinks treat symlinked dir on receiver as dir
-H, --hard-links preserve hard links
-p, --perms preserve permissions
-E, --executability preserve executability
--chmod=CHMOD affect file and/or directory permissions
-A, --acls preserve ACLs (implies -p)
-X, --xattrs preserve extended attributes
-o, --owner preserve owner (super-user only)
-g, --group preserve group
--devices preserve device files (super-user only)
--copy-devices copy device contents as regular file
--specials preserve special files
-D same as --devices --specials
-t, --times preserve modification times
-O, --omit-dir-times omit directories from --times
-J, --omit-link-times omit symlinks from --times
--super receiver attempts super-user activities
--fake-super store/recover privileged attrs using xattrs
-S, --sparse handle sparse files efficiently
--preallocate allocate dest files before writing
-n, --dry-run perform a trial run with no changes made
-W, --whole-file copy files whole (w/o delta-xfer algorithm)
-x, --one-file-system don't cross filesystem boundaries
-B, --block-size=SIZE force a fixed checksum block-size
-e, --rsh=COMMAND specify the remote shell to use
--rsync-path=PROGRAM specify the rsync to run on remote machine
--existing skip creating new files on receiver
--ignore-existing skip updating files that exist on receiver
--remove-source-files sender removes synchronized files (non-dir)
--del an alias for --delete-during
--delete delete extraneous files from dest dirs
--delete-before receiver deletes before xfer, not during
--delete-during receiver deletes during the transfer
--delete-delay find deletions during, delete after
--delete-after receiver deletes after transfer, not during
--delete-excluded also delete excluded files from dest dirs
--ignore-missing-args ignore missing source args without error
--delete-missing-args delete missing source args from destination
--ignore-errors delete even if there are I/O errors
--force force deletion of dirs even if not empty
--max-delete=NUM don't delete more than NUM files
--max-size=SIZE don't transfer any file larger than SIZE
--min-size=SIZE don't transfer any file smaller than SIZE
--partial keep partially transferred files
--partial-dir=DIR put a partially transferred file into DIR
--delay-updates put all updated files into place at end
-m, --prune-empty-dirs prune empty directory chains from file-list
--numeric-ids don't map uid/gid values by user/group name
--usermap=STRING custom username mapping
--groupmap=STRING custom groupname mapping
--chown=USER:GROUP simple username/groupname mapping
--timeout=SECONDS set I/O timeout in seconds
--contimeout=SECONDS set daemon connection timeout in seconds
-I, --ignore-times don't skip files that match size and time
--size-only skip files that match in size
--modify-window=NUM compare mod-times with reduced accuracy
-T, --temp-dir=DIR create temporary files in directory DIR
-y, --fuzzy find similar file for basis if no dest file
--compare-dest=DIR also compare received files relative to DIR
--copy-dest=DIR ... and include copies of unchanged files
--link-dest=DIR hardlink to files in DIR when unchanged
-z, --compress compress file data during the transfer
--compress-level=NUM explicitly set compression level
--skip-compress=LIST skip compressing files with suffix in LIST
-C, --cvs-exclude auto-ignore files in the same way CVS does
-f, --filter=RULE add a file-filtering RULE
-F same as --filter='dir-merge /.rsync-filter'
repeated: --filter='- .rsync-filter'
--exclude=PATTERN exclude files matching PATTERN
--exclude-from=FILE read exclude patterns from FILE
--include=PATTERN don't exclude files matching PATTERN
--include-from=FILE read include patterns from FILE
--files-from=FILE read list of source-file names from FILE
-0, --from0 all *from/filter files are delimited by 0s
--old-dirs, --old-d works like --dirs when talking to old rsync
-s, --protect-args no space-splitting; wildcard chars only
--address=ADDRESS bind address for outgoing socket to daemon
--port=PORT specify double-colon alternate port number
--sockopts=OPTIONS specify custom TCP options
--blocking-io use blocking I/O for the remote shell
--outbuf=N|L|B set out buffering to None, Line, or Block
--stats give some file-transfer stats
-8, --8-bit-output leave high-bit chars unescaped in output
-h, --human-readable output numbers in a human-readable format
--progress show progress during transfer
-P same as --partial --progress
-i, --itemize-changes output a change-summary for all updates
-M, --remote-option=OPTION send OPTION to the remote side only
--out-format=FORMAT output updates using the specified FORMAT
--log-file=FILE log what we're doing to the specified FILE
--log-file-format=FMT log updates using the specified FMT
--password-file=FILE read daemon-access password from FILE
--list-only list the files instead of copying them
--bwlimit=RATE limit socket I/O bandwidth
--write-batch=FILE write a batched update to FILE
--only-write-batch=FILE like --write-batch but w/o updating dest
--read-batch=FILE read a batched update from FILE
--protocol=NUM force an older protocol version to be used
--iconv=CONVERT_SPEC request charset conversion of filenames
--checksum-seed=NUM set block/file checksum seed (advanced)
-4, --ipv4 prefer IPv4
-6, --ipv6 prefer IPv6
--version print version number
(-h) --help show this help (see below for -h comment)
Rsync can also be run as a daemon, in which case the following options are accepted:
--daemon run as an rsync daemon
--address=ADDRESS bind to the specified address
--bwlimit=RATE limit socket I/O bandwidth
--config=FILE specify alternate rsyncd.conf file
-M, --dparam=OVERRIDE override global daemon config parameter
--no-detach do not detach from the parent
--port=PORT listen on alternate port number
--log-file=FILE override the "log file" setting
--log-file-format=FMT override the "log format" setting
--sockopts=OPTIONS specify custom TCP options
-v, --verbose increase verbosity
-4, --ipv4 prefer IPv4
-6, --ipv6 prefer IPv6
-h, --help show this help (if used after --daemon)
选项众多,我们会挑选一些常用的介绍
6. 本地传输模式
本地传输模式实现的是本地同步功能,相当于是cp,rm等命令,说的简单一点就是在同一台机器上把数据从一个地方拷贝到另一个地方或者删除数据,rsync比cp命令高级的地方在于,rsync拷贝时使用的是增量拷贝,即只会把不同的内容拷贝过去,这样就大大提升了性能。
6.1 语法格式
rsync [OPTION...] SRC... [DEST]
6.2 语法说明:
- rsync为同步的命令
- [option]为同步时需要的参数
- SRC为源,即待拷贝的分区,文件或目录
- [DEST]为目的分区,文件或目录
6.3 示例
6.3.1 本地拷贝命令
[root@dbc-server-554 null]# mkdir /rsync_tmp/d{1..2}
[root@dbc-server-554 null]# ll
total 4
-rw-r--r-- 1 root root 0 Feb 16 10:44 1
-rw-r--r-- 1 root root 4 Feb 16 10:25 123
-rw-r--r-- 1 root root 0 Feb 16 10:35 16
-rw-r--r-- 1 root root 0 Feb 16 10:35 17
-rw-r--r-- 1 root root 0 Feb 16 10:35 18
-rw-r--r-- 1 root root 0 Feb 16 10:41 19
-rw-r--r-- 1 root root 0 Feb 16 10:44 2
-rw-r--r-- 1 root root 0 Feb 16 10:41 20
-rw-r--r-- 1 root root 0 Feb 16 10:44 3
-rw-r--r-- 1 root root 0 Feb 16 10:44 4
-rw-r--r-- 1 root root 0 Feb 16 10:44 5
[root@dbc-server-554 null]# cp /null/* /rsync_tmp/d1
[root@dbc-server-554 null]# rsync /null/* /rsync_tmp/d2
[root@dbc-server-554 null]# ll /rsync_tmp/d2/
total 4
-rw-r--r-- 1 root root 0 Feb 16 10:46 1
-rw-r--r-- 1 root root 4 Feb 16 10:46 123
-rw-r--r-- 1 root root 0 Feb 16 10:46 16
-rw-r--r-- 1 root root 0 Feb 16 10:46 17
-rw-r--r-- 1 root root 0 Feb 16 10:46 18
-rw-r--r-- 1 root root 0 Feb 16 10:46 19
-rw-r--r-- 1 root root 0 Feb 16 10:46 2
-rw-r--r-- 1 root root 0 Feb 16 10:46 20
-rw-r--r-- 1 root root 0 Feb 16 10:46 3
-rw-r--r-- 1 root root 0 Feb 16 10:46 4
-rw-r--r-- 1 root root 0 Feb 16 10:46 5
看起来差别不太大,实际rsync以增量的方式进行拷贝,所以当文件size比较大的时候,性能还是有所提升的
6.3.2 本地删除命令(这个功能比较危险,不建议用)
[root@dbc-server-554 rsync_tmp]# ll
total 8
-rw-r--r-- 1 root root 0 Feb 16 10:20 1
-rw-r--r-- 1 root root 0 Feb 16 10:20 2
-rw-r--r-- 1 root root 0 Feb 16 10:20 3
-rw-r--r-- 1 root root 0 Feb 16 10:20 4
-rw-r--r-- 1 root root 0 Feb 16 10:20 5
-rw-r--r-- 1 root root 0 Feb 16 10:20 6
-rw-r--r-- 1 root root 0 Feb 16 10:20 7
-rw-r--r-- 1 root root 0 Feb 16 10:20 8
-rw-r--r-- 1 root root 0 Feb 16 10:20 9
-rw-r--r-- 1 root root 158 Feb 16 10:23 hosts
-rw-r--r-- 1 root root 2981 Feb 16 10:25 passwd
[root@dbc-server-554 rsync_tmp]# cd /null/
[root@dbc-server-554 null]# touch {16..18}
[root@dbc-server-554 null]# rsync -avz --delete /null/ /rsync_tmp/
sending incremental file list
./
16
17
18
sent 220 bytes received 76 bytes 592.00 bytes/sec
total size is 4 speedup is 0.01
[root@dbc-server-554 null]# ll /rsync_tmp/
total 4
-rw-r--r-- 1 root root 4 Feb 16 10:25 123
-rw-r--r-- 1 root root 0 Feb 16 10:35 16
-rw-r--r-- 1 root root 0 Feb 16 10:35 17
-rw-r--r-- 1 root root 0 Feb 16 10:35 18
[root@dbc-server-554 null]# rsync -avz --delete --exclude '19' /null/ /rsync_tmp/
sending incremental file list
./
20
sent 165 bytes received 38 bytes 406.00 bytes/sec
total size is 4 speedup is 0.02
[root@dbc-server-554 null]# ll /rsync_tmp/
total 4
-rw-r--r-- 1 root root 4 Feb 16 10:25 123
-rw-r--r-- 1 root root 0 Feb 16 10:35 16
-rw-r--r-- 1 root root 0 Feb 16 10:35 17
-rw-r--r-- 1 root root 0 Feb 16 10:35 18
-rw-r--r-- 1 root root 0 Feb 16 10:41 20
用到的参数:
-v –verbose
:详细输出模式,传输时的进度等信息
-z –compress
:传输时进行压缩以提高传输效率, --compress-level=NUM
可按级别压缩
-a --archive
:归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rtopgDl
--delete
:删除
--exclude
:排除
说明:该命令会把/null/
目录中的文件拷贝到/rsync_tmp/中,并删除/rsync_tmp/中的和/null/中不同的其他文件
需要注意命令中的/null/
一定要加/
,否则会把/null
拷贝到/rsync_tmp/中
6.3.3 其他重要参数介绍
-r --recursive 递归模式,子目录下的所有目录都同样传输
-t --times 保持文件时间信息
-o --owner 保持文件属主信息
-p –perms 保持文件权限
-g --group保持文件属性
-P --progress 显示同步的过程及传输时的进度等信息
-D --devices 保持设备文件信息
-l --links 保留软连接
-e --rsh=command 使用的通道协议,指定替代rsh的shell程序,例如ssh等
–exclude=PATTERN 指定排除不需要传输的文件模式
–exclude-from=file (文件名所在的目录文件)
–bwlimit=RATE 限制传输的速度
7. 通过远程shell进行数据传输(remote shell mode)
用于两台机器之间数据的传输,比如把重要数据备份到服务器。这个模式相当于是scp命令实现的功能,不过rsync还是可以实现增量的拷贝,而且支持目录的拷贝,这个是比rsync好用的地方。
7.1 语法
通过远程shell(rcp,ssh等)传输可以分为两种情况,其语法分别为:
rsync [OPTION...] [USER@]HOST:SRC... [DEST] #拉取
rsync [OPTION...] SRC... [USER@]HOST:DEST #推送
拉取:从远端主机把数据同步到执行命令的本地主机相应的目录;
推送:从本地主机执行命令把本地的数据同步到远端主机指定目录下。
7.2 语法说明:
- rsync 为同步的命令
- [OPTION…]为同步时的参数选项
- [USER@]HOST:SRC…为同步的远程的链接用户和主机地址
- SRC为源,即待copy的分区,文件或目录等,和HOST之间用一个冒号连接
- [DEST]为目的分区,文件或目录等
7.3 示例
拉取的语法示例:
[root@dbc-server-554 rsync_tmp]# rsync -avz -e 'ssh -p 22' root@192.168.71.253:/root/backup/ /rsync_tmp/71.253/
The authenticity of host '192.168.71.253 (192.168.71.253)' can't be established.
ECDSA key fingerprint is SHA256:REIC9jpf+VqtXziGzLwnnqn3pV9pahY/qpd5/kcxMgk.
ECDSA key fingerprint is MD5:77:38:d2:8f:46:cd:38:1f:94:be:03:a8:62:50:15:b8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.71.253' (ECDSA) to the list of known hosts.
root@192.168.71.253's password:
receiving incremental file list
./
1
10
2
3
4
5
6
7
8
9
hosts
sent 236 bytes received 701 bytes 124.93 bytes/sec
total size is 217 speedup is 0.23
[root@dbc-server-554 rsync_tmp]# ll 71.253/
total 4
-rw-r--r-- 1 1016 1016 0 Feb 16 2023 1
-rw-r--r-- 1 1016 1016 0 Feb 16 2023 10
-rw-r--r-- 1 1016 1016 0 Feb 16 2023 2
-rw-r--r-- 1 1016 1016 0 Feb 16 2023 3
-rw-r--r-- 1 1016 1016 0 Feb 16 2023 4
-rw-r--r-- 1 1016 1016 0 Feb 16 2023 5
-rw-r--r-- 1 1016 1016 0 Feb 16 2023 6
-rw-r--r-- 1 1016 1016 0 Feb 16 2023 7
-rw-r--r-- 1 1016 1016 0 Feb 16 2023 8
-rw-r--r-- 1 1016 1016 0 Feb 16 2023 9
-rw-r--r-- 1 1016 1016 217 Feb 16 2023 hosts
推送的语法示例:
[root@dbc-server-554 rsync_tmp]# rsync -avz -e 'ssh -p 22' 71.253/ root@192.168.71.253:/root/backup/new
root@192.168.71.253's password:
sending incremental file list
./
1
10
2
3
4
5
6
7
8
9
hosts
sent 685 bytes received 228 bytes 140.46 bytes/sec
total size is 217 speedup is 0.24
注意一下两个命令的区别:
rsync –avz /opt/ /tmp/
rsync -avz /opt /tmp/
/opt/
表示推送或拉取/opt目录下
的内容,/opt
表示推送或拉取/opt目录及其里面的内容
。
8. 使用RSYNC守护进程模式(重点)
工作原理:
RSYNC守护进程模式,包含服务器以及客户机,服务器上启动rsync的守护进程,客户机配置对应的用户名和密码,实现客户机到服务器的备份功能。RSYNC守护进程启动的机器就是我们文章标题所说的备份服务器
。 其他的机器作为客户端,向这台机器推送数据,或从这台机器上拉取数据。
8.1 服务器端配置
- 首先确认软件是否安装:
[root@k8s-node-02 ~]# rpm -qa rsync
rsync-3.1.2-12.el7_9.x86_64
[root@k8s-node-02 ~]# rsync --version
rsync version 3.1.2 protocol version 31
- 创建用户
[root@k8s-node-02 ~]# useradd rsync -s /sbin/nologin -M #这个是rsync进程使用的用户
[root@k8s-node-02 ~]# id rsync
uid=1016(rsync) gid=1016(rsync) groups=1016(rsync)
客户端连到服务器用rsync
用户访问权限访问数据
- 配置服务器端的配置文件(/etc/rsyncd.conf)
[root@k8s-node-02 ~]# cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
uid = rsync
gid = rsync
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
# exclude = lost+found/
# transfer logging = yes
timeout = 900
# ignore nonreadable = yes
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
fake super = yes
# [ftp]
# path = /home/ftp
# comment = ftp export area
[backup]
path = /root/backup/
ignore errors
read only = false
list = false
hosts allow = 192.168.71.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
配置文件说明:
参数 | 说明 |
---|---|
uid=rsync | rsync使用的用户ID。缺省uid为-2,通常为nobody |
gid=rsync | rsync使用的组(用户所在的组)。缺省gid为-2,通常为nobody |
use chroot=no | 如果为true,daemon会在给客户端传输文件前“chroot to the path”。这是rsync安全的一个配置,因为我们大多数都是在内网使用rsync,所以不用配置也可以 |
max connections=200 | 设置最大连接数,默认为0,意思为无限制,负值为关闭这个模块 |
timeout=300 | 默认为0,意思为no timeout,建议为300-500(s) |
pid file=/var/run/rsyncd.pid | rsync daemon启动后将其进程PID写入此文件。如果这个文件已经存在,rsync进程不会覆盖该文件,而是会终止 |
lock file=/var/run/rysncd.lock | 指定loc文件用来支撑max connections的参数,使得总连接数不会超过限制,默认为/var/run/rsyncd.lock |
log file = /var/log/rsyncd.log | rsync的日志文件 |
ignore errors | 忽略IO错误 |
fake super = yes | 无需让rsync以root身份运行,允许接收文件的完整属性 |
read only = false | 指定客户端是否可以上传文件,默认对所有模块都为true |
list = false | 是否允许客户端可以查看可用模块列表(类似ls),默认为true |
hosts allow = 172.16.1.0/24 | 指定可以联系的客户端主机名或者ip地址或者地址段,默认情况没有此参数,即都可以连接 |
hosts deny = 0.0.0.0/32 | 指定不可联系的客户端主机名或IP地址或地址段,禁止他们连接。默认情况没有此参数,即都可以连接 |
auth users = rsync_backup | 指定以空格或逗号分隔的用户可以使用哪些模块,用户不需要在本地系统中存在,默认所有用户无密码的访问 |
secrets file = /etc/rsync.password | 指定用户名和密码存放的文件。格式:用户名:密码, 密码不超过8位 |
[backup] | 模块名称,需要用中括号括起来,起名没有特殊要求,但最好是有意义的名称,便于以后维护 |
path = /backup/ | 在这个模块中,daemon使用的文件系统或目录,目录的权限要注意和配置文件中的权限一致,否则会遇到读写问题。 |
- 创建备份目录
[root@k8s-node-02 ~]# mkdir /backup
[root@k8s-node-02 ~]# chown -R rsync.rsync /backup/
[root@k8s-node-02 ~]# ls -ld /backup/
drwxr-xr-x 2 rsync rsync 6 Feb 16 06:25 /backup/
- 创建密码文件
和配置文件中secrets file = /etc/rsync.password
一致
[root@k8s-node-02 ~]# echo "rsync_backup:grewan" > /etc/rsync.password
[root@k8s-node-02 ~]# chmod 600 /etc/rsync.password
- 启动服务
[root@k8s-node-02 ~]# rsync --daemon
[root@k8s-node-02 ~]# ps -ef |grep rsync
root 11749 1 0 04:41 ? 00:00:00 rsync --daemon
[root@k8s-node-02 ~]# lsof -i:873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 11749 root 4u IPv4 55656 0t0 TCP *:rsync (LISTEN)
rsync 11749 root 5u IPv6 55657 0t0 TCP *:rsync (LISTEN)
8.2 客户端配置
- 配置密码文件
[root@dbc-server-554 rsync_tmp]# echo "grewan" > /etc/rsync.password
[root@dbc-server-554 rsync_tmp]# chmod 600 /etc/rsync.password
8.3 示例测试
注意:这里的示例都是在客户机端上进行操作的,一般的使用场景都是从客户机备份数据到服务器。
语法格式:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] #从服务器拉取数据,这种方式比较常用
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST #向服务器推送数据,这种方式比较常用
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
- 在客户端上创建/backup目录,及测试文件
[root@dbc-server-554 rsync_tmp]# mkdir /backup;cd /backup;touch {98..100}
[root@dbc-server-554 backup]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 16 11:32 100
-rw-r--r-- 1 root root 0 Feb 16 11:32 98
-rw-r--r-- 1 root root 0 Feb 16 11:32 99
- 把客户端/backup目录中的内容,备份(推送)到备份服务器:
[root@dbc-server-554 backup]# rsync -avz /backup/ rsync_backup@192.168.71.253::backup --password-file=/etc/rsync.password
sending incremental file list
./
100
98
99
sent 203 bytes received 84 bytes 191.33 bytes/sec
total size is 0 speedup is 0.00
命令说明:
rsync -avz
:命令和对应的参数
/backup/
: 把backup目录下的内容推送到服务器
rsync_backup@192.168.71.253::backup
:这个是服务器的信息,其中:
rsync_backup
是配置文件/etc/rsyncd.conf中配置的用户名
192.168.71.253
是服务器的ip地址,
backup
是配置文件/etc/rsyncd.conf中配置的模块名
,这个地方一定不要弄错
--password-file
:指定密码文件,不加这个参数的情况下,要手动输入密码。
- 第二种推送的方式
使用rsync协议的方式进行推送
[root@dbc-server-554 backup]# touch {1..3}
[root@dbc-server-554 backup]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 16 13:27 1
-rw-r--r-- 1 root root 0 Feb 16 11:32 100
-rw-r--r-- 1 root root 0 Feb 16 13:27 2
-rw-r--r-- 1 root root 0 Feb 16 13:27 3
-rw-r--r-- 1 root root 0 Feb 16 11:32 98
-rw-r--r-- 1 root root 0 Feb 16 11:32 99
[root@dbc-server-554 backup]# rsync -avz /backup/ rsync://rsync_backup@192.168.71.253/backup --password-file=/etc/rsync.password
sending incremental file list
./
1
2
3
sent 247 bytes received 84 bytes 662.00 bytes/sec
total size is 0 speedup is 0.00
- 从备份服务器拉取内容
[root@dbc-server-554 new]# rsync -avz rsync_backup@192.168.71.253::backup /backup/new --password-file=/etc/rsync.password
receiving incremental file list
./
1
100
2
3
98
99
sent 145 bytes received 363 bytes 1,016.00 bytes/sec
total size is 0 speedup is 0.00
[root@dbc-server-554 new]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 16 13:27 1
-rw-r--r-- 1 root root 0 Feb 16 11:32 100
-rw-r--r-- 1 root root 0 Feb 16 13:27 2
-rw-r--r-- 1 root root 0 Feb 16 13:27 3
-rw-r--r-- 1 root root 0 Feb 16 11:32 98
-rw-r--r-- 1 root root 0 Feb 16 11:32 99
- 第二种拉取的方式
[root@dbc-server-554 new]# rm -rf *
[root@dbc-server-554 new]# rsync -avz rsync://rsync_backup@192.168.71.253/backup /backup/new --password-file=/etc/rsync.password
receiving incremental file list
./
1
100
2
3
98
99
sent 145 bytes received 363 bytes 1,016.00 bytes/sec
total size is 0 speedup is 0.00
[root@dbc-server-554 new]# ll
total 0
-rw-r--r-- 1 root root 0 Feb 16 13:27 1
-rw-r--r-- 1 root root 0 Feb 16 11:32 100
-rw-r--r-- 1 root root 0 Feb 16 13:27 2
-rw-r--r-- 1 root root 0 Feb 16 13:27 3
-rw-r--r-- 1 root root 0 Feb 16 11:32 98
-rw-r--r-- 1 root root 0 Feb 16 11:32 99
- 删除rsync同步进程
kill `cat /var/run/rsyncd.pid`
kill `/var/run/rsyncd.pid`
9. 多目录共享
多目录共享的意思是说客户端可以向服务器端多个目录下进行推送或拉取。这个实现起来很简单,就是在配置文件中配置多个模块,每个模块可以指定不同的用户名,密码等等信息。如果所有推送的模块基本信息都相同,就可以把配置信息放在多个模块的上面,模块只配置一个对应的路径即可,像下面这个示例这样:
[root@backup backup]# cat /etc/rsyncd.conf
#rsync_config_______________start
#created by grewan 07::23 2016-05-28
#Email:wangqj541@163.com blog: http://www.cnblogs.com/greta/
##rsyncd.conf start##
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
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] #这里配置多个模块,共同的配置项目,移动到模块的上面
path = /backup/
[test]
path = /test/
#rsync_config_______________end