2015-12-12linux系统日常管理第五部分( rsync )



第二部分
rsync  (很好用,每天都在用)用于增量备份!!

1.主要用法:
本地拷贝:
[root@niejicai-linux ~]# rsync  /tmp/1.txt   /tmp/2.txt                   (类似复制,和cp差不多)
                                                           源                  目的
远程拷贝:
[root@niejicai-linux ~]# rsync /tmp/1.txt  username@ip:/tmp/2.txt  (类似上传文件到其他的服务器上)
                                                     需拷贝的原文件    远程)目标用户和ip,到那个路劲!
[root@niejicai-linux ~]# rsync username@ip:/tmp/2.txt   /tmp/1.txt    (类似于下载文件)
                                                     远程服务器的用户名或者ip        
                                                      的/tmp/2.txt                         拷贝到/tmp下的目录。
[root@niejicai-linux ~]# rnsyc username@ip::test/1.txt  /tmp/1.txt     (表示块) 后面我们,会有讲解!

-------------------------------------------------------------------------------------------------------------------------------------------------------------
2.主要参数:
-a (综合的包含:-r -v -l -p -t -g -o -D),其中av经常结合起来用!

-a 归档模式,表示以递归方式传输文件,并保持所有属性,等同于 -rlptgoD, -a选项后面可以跟一个 --no-OPTION 这个表示关闭-rlptgoD中的某一个例如-a--no-l 等同于 -rptgoD
-r 对子目录以递归模式处理,主要是针对目录来说的,如果单独传一个文件不需要加-r,但是传输的是 目录必须加-r选项
-v 打印一些信息出来,比如速率,文件数量等  ( 可视化)
-l 保留软链接
-L 向对待常规文件一样处理软链接,如果是SRC中有软链接文件,则加上该选项后将会把软链接指向的目标文件拷贝到DST
-p 保持文件权限 
-o 保持文件属主信息
-g 保持文件属组信息
-D 保持设备文件信息
-t 保持文件时间信息

-z压缩(拷贝的同时,进行压缩,提高速度
--delete 删除那些DST中SRC没有的文件
--exclude=PATTERN 指定排除不需要传输的文件,等号后面跟文件名,可以是万用字符模式(如*.txt)
-P等于--progress 在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等等
-u 加上这个选项后将会把DST中比SRC还新的文件排除掉,不会覆盖【-u当你的目标文件有更新的时候,为防止源文件覆盖目标文件,(-u就是防止覆盖的意思)。】
最常用的 -a -v --delete --exclude 
------------------------------------------------------------------------------------------------------------------------------------------------------------

3. rsync选项讲解
rsync -av   dir1/ dir2/    # 其中dir2/目录可以不存在,记得同步目录时一定要在末尾加上/
-a 会把软连接原原本本的拷贝过去,那有时候我们想拷贝源文件怎么办?就时候就用到一个 -L  
rsync -avL test1/ test2/ 
-u 选项的作用是,如果目标文件比源文件新,那么会忽略掉该文件 
touch test2/1.txt; rsync -avu test1/ test2/
rsync -av --delete test1/ test2/   #这样会把test2/目录比test1/目录多出来的文件删除掉
rsync -a --exclude=“2.txt” test1/ test2/  #在同步的过程中,会忽略掉2.txt这个文件
rsync -a --progress --exclude=“*.txt” test1/ test2/  #--progress 显示同步过程的详细信息,--exclude后面也可以使用通配符*

-------------------------------------------------------------------------------------------------------------------------------------------------------------
4.实验列子:
列子一:(-av)
[root@niejicai-linux ~]# rsync -av /tmp/1.txt  /root/niejicai.txt     本地拷贝,把/tmp/1.txt 到
/root/,然后命名为niejicai.txt
sending incremental file list
1.txt
sent 5113 bytes  received 31 bytes  10288.00 bytes/sec
total size is 5040  speedup is 0.98
[root@niejicai-linux ~]# rsync -av /tmp/1.txt /root/niejicai.txt  (如果以前拷贝过,现在再拷贝,不会覆盖!
sending incremental file list
sent 30 bytes  received 12 bytes  84.00 bytes/sec
total size is 5040  speedup is 120.00
[root@niejicai-linux ~]# echo "12233" >> /tmp/1.txt             如果我们在1.txt下写一些数字,或者变更一下,然后再次进行拷贝,就会覆盖了!
[root@niejicai-linux ~]# rsync -av /tmp/1.txt /root/niejicai.txt    覆盖了! 
sending incremental file list
1.txt
sent 5119 bytes  received 31 bytes  10300.00 bytes/sec
total size is 5046  speedup is 0.98
为了便于区分增量,做如下的演示
10.txt  33.tar      3.txt   92.txt           install.log         test.txt
12      33.tar.bz2  4.txt   93.txt           install.log.syslog   usr
1.log   33.tar.gz   5.txt   999.txt          jicai
1.txt   33.tar.xz   6.txt   9.txt            nie
234     34.tar      7.txt   anaconda-ks.cfg  niejicailinux.txt
2.log   34.txt      90.txt  error.log        niejicai.txt
2.txt   34.txt.zip  91.txt  etc              root
[root@niejicai-linux ~]# ls usr/
bin
[ root@niejicai-linux ~]# cd
[root@niejicai-linux ~]# rsync -av usr/ /tmp/10000000/
sending incremental file list
created directory /tmp/10000000
./
1.txt
bin/
sent 109 bytes  received 38 bytes  294.00 bytes/sec
total size is 0  speedup is 0.00
[root@niejicai-linux ~]# touch usr/2.txt
[root@niejicai-linux ~]# rsync -av usr/ /tmp/10000000/
sending incremental file list
2.txt                 只会去同步一个文件!
sent 107 bytes  received 32 bytes  278.00 bytes/sec
total size is 0  speedup is 0.00
------------------------------------------------------------------------------------------------------------------------------------------------------------
列子二:  (-L)把软连接的原文件也同步过来!)
-l 保留软链接
[root@niejicai-linux ~]# cd usr/
[root@niejicai-linux usr]# ls
1.txt  bin
[root@niejicai-linux usr]# ls -s /root/
total 1121692
。。。
 37160 34.tar            4 9.txt                      1.ipt
 4 34.txt            4 anaconda-ks.cfg
[root@niejicai-linux usr]# ln -s /root/1.ipt  /root/usr/      创建软连接!
[ root@niejicai-linux usr]# ls -l
total 4
lrwxrwxrwx 1 root root   11 Dec 10 15:43 1.ipt -> /root/1.ipt
-rw-r--r-- 1 root root    0 Dec 10 15:37 1.txt
drwxr-xr-x 2 root root 4096 Dec 10 15:28 bin
[root@niejicai-linux ~]# rsync -avl usr/ /tmp/10000000/
sending incremental file list
./
1.ipt -> /root/1.ipt                  说明了这里同步的是一个连接!
sent 103 bytes  received 19 bytes  244.00 bytes/sec
total size is 11  speedup is 0.09
[root@niejicai-linux ~]# ls -l /tmp/10000000/
total 4
lrwxrwxrwx 1 root root   11 Dec 10 15:43  1.ipt -> /root/1.ipt
-rw-r--r-- 1 root root    0 Dec 10 15:37 1.txt
drwxr-xr-x 2 root root 4096 Dec 10 15:28 bin
[ root@niejicai-linux ~]# rsync -avLusr/ /tmp/10000000/     (把软连接的原文件也同步过来!)
sending incremental file list
1.ipt 
sent 103 bytes  received 19 bytes  244.00 bytes/sec
total size is 11  speedup is 0.09
-------------------------------------------------------------------------------------------------------------------------------------------------------------
列子三: -u
-u当你的目标文件有更新的时候,为防止源文件覆盖目标文件,(-u就是防止覆盖的意思)
在A服务器上:
[root@niejicai-linux ~]# rsync  -av /tmp/1.txt  192.168.1.219:/tmp/2.txt  
                                                          原来文件1.txt                                  远程备份2.txt 
在B服务器上:  
现在我把备份的文件进行更改(特殊性要求)如下                                    
[root@niejicai-linux-web ~]#echo "100987890" >> /tmp/2.txt
而已下次我同步备份的时候,我不希望我的备份文件2.txt之前的更改被覆盖!所以这里我们就需要用到一个参数!    -u
在A服务器上:
[root@niejicai-linux ~]# rsync  -avu /tmp/1.txt  192.168.1.219:/tmp/2.txt 
备份文件(192.168.1.219下的/tmp/2.txt,就不会被改变!
-------------------------------------------------------------------------------------------------------------------------------------------------------------
列子四: --delete
有时候,我们就是无聊没有事情做,就在乱弄,结果在目标文件下面
增加了很多无聊的文件!
所以我每次同步的时候,我都需要把这些无聊的文件给删除去,然后同步!
[root@niejicai-linux ~]# rsync -av usr/   /tmp/123/
sending incremental file list
created directory /tmp/123
./
1.ipt -> /root/1.ipt
1.txt
bin/
sent 145 bytes  received 41 bytes  372.00 bytes/sec
total size is 11  speedup is 0.06
[root@niejicai-linux 123]# cd /tmp/123/
[ root@niejicai-linux 123]# ls
1.ipt  1.txt  bin
[root@niejicai-linux ~]# rsync  -av --delete usr/ /tmp/123/     这样会把/tmp/123/目录比/usr/目录多出来的文件删除掉
sending incremental file list
./
deleting 2.txt
sent 100 bytes  received 16 bytes  232.00 bytes/sec
total size is 11  speedup is 0.09
[root@niejicai-linux ~]# cd /tmp/123
[root@niejicai-linux 123]# ls
1.ipt  1.txt  bin
所以现在可以保持源文件和目标文件时时一样了!
-------------------------------------------------------------------------------------------------------------------------------------------------------------
列子五: --exclude
--exclude=PATTERN 指定排除不需要传输的文件,等号后面跟文件名,可以是万用字符模式(如*.txt)
[root@niejicai-linux ~]# rsync -av --exclude="*.txt" usr/  /tmp/123/
sending incremental file list
created directory /tmp/123
./
1.ipt -> /root/1.ipt
bin/
sent 85 bytes  received 22 bytes  214.00 bytes/sec
total size is 11  speedup is 0.10
[root@niejicai-linux ~]# ls usr/
1.ipt  1.txt  bin
[root@niejicai-linux ~]# ls /tmp/123/
1.ipt  bin
也可以指定多个参数
[ root@niejicai-linux ~]# rsync -av  --exclude="*.txt"  --exclude="*.ipt" usr/ /tmp/123/   可以一下排除多个文件!
sending incremental file list
created directory /tmp/123
./
bin/
sent 52 bytes  received 19 bytes  142.00 bytes/sec
total size is 0  speedup is 0.00
[ root@niejicai-linux ~]# cd usr/
[ root@niejicai-linux usr]# ls
1.ipt  1.txt  bin
[ root@niejicai-linux ~]# ls /tmp/123/
bin
-------------------------------------------------------------------------------------------------------------------------------------------------------------
列子六: -bwlimit 
(这个选项不怎么使用,但是有时候也会用到的,
比如说,你在rsync 去做个远程备份,我的带宽比较小,我的服务器上还跑了各种的业务
比如说Apache,web,之类的东西。我的r sync 去做个远程备份的时候会占用很多的带宽,直接会影响
到我线上服务器在跑的业务!
这时候,我们就是需要考虑这个参数,用来进行限制
[root@niejicai-linux ~]# rsync -avP --bwlimit=100 usr/ /tmp/123/
这里的100的单位是kB。
-------------------------------------------------------------------------------------------------------------------------------------------------------------

5.rsync应用实例 

1. rsync应用实例 - ssh方式(主要是针对22端口的)
rsync -avL test1/  www@192.168.0.101:/tmp/test2/ 
rsync -avL 192.168.0.101:/tmp/test2/ ./test3/ 
由于需要输入密码所以不适合写到脚本中,但可以通过创建秘钥对,让两台机器产生信任关系从而不用输入密码
如果ssh端口不是22,那么需要写成这样的形式:rsync -av "--rsh=ssh -p port”  /dir1/ 192.168.0.101:/tmp/dir2/ 

测试如下
1).远程拷贝:(默认端口为22)
在A服务器上面(192.168.1.218),
远程上传!
[root@niejicai-linux ~]# cd usr/
[ root@niejicai-linux usr]# ls
1.ipt  1.txt  bin
[root@niejicai-linux ~]# rsync -avzP /root/usr/ 192.168.1.219:/tmp/usr/
                                                           远程服务器B的ip,以及要拷贝到的目录下!
root@192.168.1.219's password:           输入密码  
sending incremental file list
created directory /tmp/usr
./
1.ipt -> /root/1.ipt
1.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1/4)
bin/
sent 142 bytes  received 41 bytes  8.13 bytes/sec
total size is 11  speedup is 0.06

验证是否成功:
登录到B的服务器上来(192.168.1.219)
etc.bak                  vmware-config0
ks-script-Kxkbm6         VMwareDnD
ks-script-Kxkbm6.log     vmware-niejicai
pulse-12AO83pQO0W5       vmware-root
pulse-Gg0ejUxaAYKw       vmware-root-2117549315
usr                       yum.log
virtual-niejicai.p2jSIK
1.ipt  1.txt  bin

远程下载:
在A服务器上面,(192.168.1.218)
[root@niejicai-linux ~]# rm -rf usr/*
[root@niejicai-linux ~]# rsync -avPL 192.168.1.219:/tmp/usr/ ./usr/
                                                                远程的ip,以及备份的文件   拷贝到./usr/的目录下来!
root@192.168.1.219's password:     输入密码
receiving incremental file list
symlink has no referent: "/tmp/usr/1.ipt"
./
1.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1/3)
bin/
sent 37 bytes  received 168 bytes  58.57 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(1505) [generator=3.0.6]
[root@niejicai-linux ~]# ls usr/          (已经复制过来)
1.txt  bin



2).ssh远程拷贝:(默认端口不是22时候)
[root@niejicai-linux ~]# vim /etc/ssh/sshd_config
进行,更改端口号!

port=220
[root@niejicai-linux ~]# netstat -lnp | grep sshd
tcp        0      0 0.0.0.0:220                 0.0.0.0:*                   LISTEN      3907/sshd
tcp        0      0 :::220                      :::*                        LISTEN      3907/sshd
这里已经更改成功!
如果 出现问题,我们一般怎么办,第一要去查看日志
tail  /var/log/messages(没有对应的日志/var/log/sshd/messages
如果没有对应的日志,应该怎么办!
我们可以通过这个 strace命令追踪进程的。
#strace   /etc/init.d/sshd restart          追踪进程的    
如果没有我听了请继续看如下:

我在A服务器上(192.168.1.218),更改了端口为220.

然后我在B服务器上(192.168.1.219)测试如下:
[ root@niejicai-linux-web ~]# telnet 192.168.1.218 220
Trying 192.168.1.218...                                            说明更改的端口是成功,没有问题!
Connected to 192.168.1.218.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.3                       (ctrl +])
^]
telnet>
在B服务器上面:
[ root@niejicai-linux-web ~]#  rsync  -avPL -e "ssh -p 220" 192.168.1.218:/tmp/usr/ ./usr/
                                                                       指定端口
The authenticity of host '[192.168.1.218]:220 ([192.168.1.218]:220)' can't be established.
RSA key fingerprint is f7:1e:03:03:fa:07:8a:99:34:09:1b:af:b9:95:24:9a.
Are you sure you want to continue connecting (yes/no)?  yes
Warning: Permanently added '[192.168.1.218]:220' (RSA) to the list of known hosts.
root@192.168.1.218's password:   输入密码 (A服务器的密码)
receiving incremental file list
./
sent 14 bytes  received 34 bytes  3.56 bytes/sec
total size is 0  speedup is 0.00



如果在传输的时候吗,不想输入密码

在A服务器上面(192.168.1.218):
[root@niejicai-linux ~]# ls -la .ssh/
total 16
drwx------.  2 root root 4096 Dec 10 12:33 .
dr-xr-x---. 11 root root 4096 Dec 11 09:56 ..
-rw-------.  1 root root  398 Nov 10 14:06 authorized_keys
-rw-r--r--   1 root root  395 Dec 10 12:33 known_hosts
[root@niejicai-linux ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
11:ec:45:6d:08:df:cf:26:1d:dc:59:84:cc:c2:d8:0f  root@niejicai-linux
The key's randomart image is:
+--[ RSA 2048]----+
|       .oo.* o oo|
|        .o+.E.+.o|
|       ......+o..|
|        ..   +.. |
|        S   . =  |
|             o   |
|                 |
|                 |
|                 |
+-----------------+
找到这个配置文件,然后复制到B的服务器上面!
[root@niejicai-linux ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArR3V9VsD2jC1uol/ZpdncFJE0IpjGpSwQTzwyCdSMM4Mid5NlcWQi4CNbx4k2A/clv90AhFSSf92/3eJjWm9INgjmqsuE4gmY7YtHDLTg38bSqQy3yf7XAIiJFN7xFl4kPHoqDZkEu00d2qFuOYMcDKDrBZeUly8pXEqD/Kk/KriMJ9bUUkc+rLq2sZpTgGHLRSpQNB0vKWQvLQBOUiz9MMRb7/n33+FVy+0rG2MeMKQXEeTLH1mypqSVi/eE5uLyCT/DU5IGzJyM8eFskp9BQcXw7V+/MVRj8pwYMnZqXm2MHoe79rHbIMnL0d3Gc9E75tBMv18761iphJG1LES/w==  r




在B 的服务器上面(192.168.1.219)
[root@niejicai-linux-web ~]# mkdir .ssh
[root@niejicai-linux-web ~]# chmod 600 .ssh/authorized_keys
[ root@niejicai-linux-web ~]#  setenforce 0
[ root@niejicai-linux-web ~]#  vim .ssh/authorized_keys      把A服务器上的  cat .ssh/id_rsa.pub复制内容打这里 粘贴
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEArR3V9VsD2jC1uol/ZpdncFJE0IpjGpSwQTzwyCdSMM4Mid5NlcWQi4CNbx4k2A/clv90AhFSSf92/3eJjWm9INgjmqsuE4gmY7YtHDLTg38bSqQy3yf7XAIiJFN7xFl4kPHoqDZkEu00d2qFuOYMcDKDrBZeUly8pXEqD/Kk/KriMJ9bUUkc+rLq2sZpTgGHLRSpQNB0vKWQvLQBOUiz9MMRb7/n33+FVy+0rG2MeMKQXEeTLH1mypqSVi/eE5uLyCT/DU5IGzJyM8eFskp9BQcXw7V+/MVRj8pwYMnZqXm2MHoe79rHbIMnL0d3Gc9E75tBMv18761iphJG1LES/w== 
[ root@niejicai-linux-web ~]#  rsync  -avPL -e "ssh -p 220" 192.168.1.218:/tmp/usr/ ./usr/
                                                                       指定端口
 就不用输入密码了!!!


-------------------------------------------------------------------------------------------------------------------------------------------------------------

3).C/S架构模式传输:
 rsync应用实例 - 后台服务方式
配置文件 /etc/rsyncd.conf ,内容如下:
#port=873        #监听端口默认为873,也可以是别的端口
log file=/var/log/rsync.log  #指定日志
pid file=/var/run/rsyncd.pid  #指定pid
#address=192.168.0.10  #可以定义绑定的ip
以上部分为全局配置部分,以下为模块内的设置
[test]    #为模块名,自定义
path=/root/rsync  # 指定该模块对应在哪个目录下
use chroot=true #是否限定在该目录下,默认为true,当有软连接时,需要改为 fasle
max connections=4  # 指定最大可以连接的客户端数
read only=no  #是否为只读
list=true  #是否可以列出模块名
uid=root #以哪个用户的身份来传输
gid=root  #以哪个组的身份来传输
auth users=test #指定验证用户名,可以不设置
secrets file=/etc/rsyncd.passwd #指定密码文件,如果设定验证用户,这一项必须设置
hosts allow=192.168.0.101 #设置可以允许访问的主机,可以是网段
密码文件/etc/rsyncd.passwd的内容格式为:username:password
启动服务的命令是:rsync --daemon 
默认去使用/etc/rsyncd.conf这个配置文件,也可以指定配置文件 rsync --daemon --config=/etc/rsyncd2.conf
可使用的选项有: rsync --daemon --help
几个测试点:port, use chroot, log file, secrets file, hosts allow, list
-----------------------------------------------------------------------------------------------------------------------

B服务器(192.168.1.219,server模式):
[root@web ~]# vim /etc/rsyncd.conf
[root@web ~]# cat /etc/rsyncd.conf 
#port=873           
log file=/var/log/rsync.log   
pid file=/var/run/rsyncd.pid  
 #address=192.168.0.10  
[test]                       
path=/tmp/rsync   
use chroot=true      
max connections=4  
read only=no     
list=true              
uid=root             
gid=root             
#auth users=test   
#secrets file=/etc/rsyncd.passwd   
hosts allow=192.168.1.218       (这里是写客户端的ip,也就是你要给谁来访问你的ip)

[root@web ~]# rsync --daemon
[ root@web ~]# ps aux | grep rsync
root      1962  0.0  0.4   4624   484 ?        Ss   23:48   0:00 rsync --daemon
root      1964  0.0  0.6   4356   720 pts/0    S+   23:48   0:00 grep rsync
[root@web ~]# netstat -lnp |grep 873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      1962/rsync
tcp        0      0 :::873                      :::*                        LISTEN      1962/rsync
[root@web ~]# ls -l /var/log/rsync.log*
-rw-r--r-- 1 root root  80 Dec 10 23:55 /var/log/rsync.log
-rw-r--r-- 1 root root 119 Dec 10 23:54 /var/log/rsync.log 
[root@web ~]# mkdir /tmp/rsync   创建对应的目录


A服务器(192.168.1.218,客户端模式):
现在是把A客户端上数据上传到B服务器!
[root@niejicai-linux ~]# rsync -av 3
33.tar      33.tar.gz   34.tar      34.txt.zip
33.tar.bz2  33.tar.xz   34.txt       3.txt
[root@niejicai-linux ~]# rsync -av 3.txt 192.168.1.219::test/5.txt      把A客户端上面的3.txt  同步到B服务器的 /tmp/rsync   目录下,test= /tmp/rsync   (请看配置文件)。
sending incremental file list
3.txt
sent 85 bytes  received 27 bytes  224.00 bytes/sec
total size is 16  speedup is 0.14

B服务器上面测试是否成功!
[root@web ~]# ls /tmp/rsync/
5.txt

换一种方式测试:
从B服务器上面下载数据到A客户端!

在B服务器上面做一个软连接
[root@web ~]# cd /tmp/rsync/
[root@web rsync]# ls
5.txt
[root@web rsync]# ln -s /root/install.log 1.txt
[root@web rsync]# ls -l
total 4
lrwxrwxrwx 1 root root 17 Dec 11 00:29  1.txt -> /root/install.log   做一个软连接,
-rw-r--r-- 1 root root 16 Dec  3 00:26 5.txt
[ root@web rsync]#

在A服务器(A客户端的上面)进行下载:

[root@niejicai-linux ~]# rsync -avL 192.168.1.219::test/     ./nie/  就可同步了
receiving incremental file list
symlink has no referent: "/1.txt" (in test)
./
5.txt
sent 48 bytes  received 189 bytes  474.00 bytes/sec
total size is 16  speedup is 0.07
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1505) [generator=3.0.6]

验证:
[ root@niejicai-linux ~]# ls
10.txt  33.tar      3.txt   92.txt           install.log         test.txt
12      33.tar.bz2  4.txt   93.txt           install.log.syslog  usr
1.log   33.tar.gz   5.txt   999.txt          jicai
1.txt   33.tar.xz   6.txt   9.txt            nie
234     34.tar      7.txt   anaconda-ks.cfg  niejicailinux.txt
2.log   34.txt      90.txt  error.log        niejicai.txt
2.txt   34.txt.zip  91.txt  etc              root
[ root@niejicai-linux ~]# cd nie
[root@niejicai-linux nie]# ls                                  成功了!
5.txt

接着:
在B服务器上(192.168.1.219)
[root@web rsync]# ln -s 5.txt 4.txt
[ root@web rsync]# ls -l
total 4
lrwxrwxrwx 1 root root 17 Dec 11 00:29  1.txt -> /root/install.log  这个一个软连接
lrwxrwxrwx 1 root root  5 Dec 11 00:41  4.txt -> 5.txt              做一个软连接
-rw-r--r-- 1 root root 16 Dec  3 00:26 5.txt

再验证:
在A客户端上(192.168.1.218)
[root@niejicai-linux nie]# rsync -avL 192.168.1.219::test/ ./nie/
receiving incremental file list
symlink has no referent: "/1.txt" (in test)
created directory ./nie
./
4.txt
5.txt
sent 67 bytes  received 218 bytes  570.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(1505) [generator=3.0.6]
[ root@niejicai-linux nie]# ls
4.txt  5.txt                                     4.txt 被同步过来了,但是 /root/install.log 没有被同步过来呢!
这是什么原因呢?

-----------------------------------------------------------------------------------------------------------------------------------------------------------

指定密码文件
在B服务器上(192.168.1.219)
[root@web ~]# vim /etc/rsyncd.conf
指定密码文件
secrets file=/etc/rsyncd.passwd  
以及 auth users= student
用户一定要记好!
#vim /etc/rsyncd.passwd
test: 123456
#chomd 600  /etc/rsyncd.passwd

在A服务器(客户端192.168.1.218)
#rsync -av 3.txt  student@192.168.1.219::test/8.txt
手工输入密码
还有一种可以写人脚本里面的,也就是我事先把密码写好,然后去引用我的密码就可以
这里我创建一个passwd文件
#vim  /etc/pass.txt
123456 (前面我设置的)
#chmod 600 /etc/pass.txt
然后去执行,
#rsync -av --password-file=/etc/pass.txt  3.txt   student@192.168.1.219::test/9.txt

------------------------------------------------------------------------------------------------------------------------------------------------------------
补充(可以看也可以不看)
怎么通过一个inum去删除一个文件
[root@web ~]# ls ls -i /var/log/rsync.log*
787052 /var/log/rsync.log  787033 /var/log/rsync.log
[root@web ~]# find /var/log/ -inum 787052 |xargs rm -rf

-------------------------------------------------------------------------------------------------------------------------------------------------



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值