baodeyun 发表于 2018-10-21 21:09:12

linux下数据备份工具rsync的使用

linux下的数据备份工具 rsync(remote sync 远程同步) 名词解释:sync(Synchronize,即“同步”)为UNIX操作系统的标准系统调用,功能为将内核文件系统缓冲区的所有数据(也即预定将通过低级I/O系统调用写入存储介质的数据)写入存储介质(如硬盘)。sync是一个linux同步命令,含义为迫使缓冲块数据立即写盘并更新超级块。在linux系统中,为了加快数据的读取速度,默认情况下,某些数据将不会直接写入硬盘,而是先暂存内存中,如果一个数据被重复写,这样速度一定快,但存在一个问题,万一重新启动,或者是关机,或者是不正常断电的情况下,由于数据还没来得及存入硬盘,会造成数据更新不正常,这时需要命令sync进行数据的写入,即#sync,在内存中尚未更新的的数据会写入硬盘中。所以在关机或者开机之前最好多执行这个几次,以确保数据写入硬盘。 Rsync不仅可以远程同步数据(类似于scp),当然还可以本地同步数据(类似于cp),但不同于cp或scp的一点是,rsync不像cp/scp一样会覆盖以前的数据(如果数据已经存在),它会先判断已经存在的数据和新数据有什么不同,只有不同时才会把不同的部分覆盖掉。 scp 用来远程拷贝数据,通过ssh协议通信。它的语法很简单,类似于cp,唯一不同的是,源地址或者目标地址需要使用远程主机的ip或者hostname. 例如要把本地的数据拷贝到远程一台主机(192.168.0.111)的/data/目录下,可以这样实现:scp /dir/filename root@192.168.0.111:/data/ 其中filename 可以是目录也可以是文件。或者也可以把远程的文件拷贝到本地:scp root@192.168.0.111:/data/filename /data/ 示例,scp拷贝root目录下面的文件到远程主机192.168.20.10的data目录下;# scp /root/iptables.shroot@192.168.20.10:/dataThe authenticity of host '192.168.20.10(192.168.20.10)' can't be established.RSA key fingerprint is84:47:af:bf:11:69:43:aa:bc:fe:9b:d6:08:b4:c4:1a.Are you sure you want to continue connecting (yes/no)?yesWarning: Permanently added '192.168.20.10' (RSA) tothe list of known hosts.root@192.168.20.10's password: iptables.sh                                          100%254   0.3KB/s00:00首次连接会提示是否要继续连接,我们输入yes继续,当建立连接后,需要输入远程主机root密码。 示例,scp拷贝目录需要加-r参数,拷贝的同时可以更改目录名;# scp -r /root/rsync/test1/192.168.20.10:/data/a/root@192.168.20.10's password: 1                                                      100%    0   0.0KB/s00:00    1.txt                                                100%    0   0.0KB/s00:00    2                                                       100%    0    0.0KB/s   00:00    2.txt                                                100%    0   0.0KB/s00:00    aa                                                   100%    0   0.0KB/s00:00    3                                                       100%    0    0.0KB/s   00:00 安装rysnc的命令:yum install -y rsync示例一,拷贝当前目录下test.txt 到/tmp目录下,拷贝的同时也可以更改文件名;# rsync -av test.txt /tmp/a.txtsending incremental file listtest.txtsent 279 bytes received 31 bytes620.00bytes/sectotal size is 203 speedup is 0.65# ls /tmp/a.txt /tmp/a.txt示例二,拷贝当前目录test.txt到远程主机192.168.20.10 /data目录下,需要输入远程主机的密码;# rsync -av test.txt 192.168.20.10:/data/root@192.168.20.10's password: sending incremental file listtest.txtsent 279 bytes received 31 bytes88.57 bytes/sectotal size is 203 speedup is 0.65示例三,从远程主机192.168.20.10/data目录下拷贝httpd文件到本地当前目录下;# rsync -av 192.168.20.10:/data/httpd ./root@192.168.20.10's password: receiving incremental file listhttpdsent 30 bytes received 7538228 bytes886853.88bytes/sectotal size is 7537230 speedup is 1.00 1. rsync 命令格式rsync ... SRC DESTrsync ... SRC HOST:DESTrsync ... HOST:SRCDESTrsync ... HOST::SRCDESTrsync ... SRC HOST::DEST上面第一个例子即为第一种格式,第二个例子即为第二种格式,但不同的是,user@host如果不加默认指的是root用户;第三个例子即为第三种格式是从远程目录同步数据到本地。第四种以及第五种格式使用了两个冒号,这种方式和前面的方式的不同在于验证方式不同。 2. rsync常用选项 -a    归档模式,表示以递归方式传输文件,并保持所有属性,等同于-rlptgoD, -a选项后面可以跟一个 --no-OPTION 这个表示关闭-rlptgoD中的某一个,例如-a--no-l等同于-rptgoD-r    对子目录以递归模式处理,主要是针对目录来说的,如果单独传一个文件不需要加-r,但是传输的是目录必须加-r选项-v    打印一些信息出来,比如速率,文件数量等-l   保留软链结-L   向对待常规文件一样处理软链接,如果是SRC中有软链接文件,则加上该选项后将会把软链接指向的目标文件拷贝到DST-p   保持文件权限-o   保持文件属主信息-g   保持文件属组信息-D   保持设备文件信息-t   保持文件时间信息--delete    删除那些DST中SRC没有的文件--exclude=PATTERN    指定排除不需要传输的文件,等号后面跟文件名,可以是万用字符模式(如*.txt)--progress    在同步的过程中可以看到同步的过程状态,比如统计要同步的文件数量、同步的文件传输速度等等-u    加上这个选项后将会把DEST目标文件中比SRC源文件还新的文件排除掉,不会覆盖最常用的选项有 -a -v--delete --exclude 示例:创建实验环境# mkdir rsync# cd rsync/# mkdir test1# cd test1# touch 1 2 3# ln -s /root/123.txt ./123.txt# ls -l-rw-r--r-- 1 root root 0 Apr 22 16:02 1lrwxrwxrwx 1 root root 13 Apr 22 16:03 123.txt ->/root/123.txt-rw-r--r-- 1 root root 0 Apr 22 16:02 2-rw-r--r-- 1 root root 0 Apr 22 16:02 3# cd ..实验目的,拷贝test1目录到test2目录,实际上rsync新建立了一个test2目录并把test1目录放在test2目录下面,这不是我们想要的结果; # rsync -a test1 test2# ls test2test1# lstest1test2# ls test2/test1/1123.txt 23为了避免上面的操作,在同步目录的时候在目录后面加/# rsync -a test1/ test2/# lstest1test2# ls test2/1123.txt23-a选项还可以与--no-OPTION一起使用,意思为不理会选项的文件;如下例,跳过123.txt软链接文件,不做拷贝;# rsync -av --no-l test1/ test2/sending incremental file listcreated directory test2./1skipping non-regular file "123.txt"23sent 200 bytes received 72 bytes544.00bytes/sectotal size is 13 speedup is 0.05# lstest1test2# ls -l test2/total 0-rw-r--r-- 1 root root 0 Apr 22 16:02 1-rw-r--r-- 1 root root 0 Apr 22 16:02 2-rw-r--r-- 1 root root 0 Apr 22 16:02 3加-L选项,拷贝的时候会拷贝软链接文件对应的源文件到目标目录里面;# rsync -avL test1/ test2/sending incremental file listcreated directory test2./1123.txt23sent 231 bytes received 91 bytes644.00bytes/sectotal size is 0 speedup is 0.00# ls -l test2/total 0-rw-r--r-- 1 root root 0 Apr 22 16:02 1-rw-r--r-- 1 root root 0 Apr 22 16:03 123.txt-rw-r--r-- 1 root root 0 Apr 22 16:02 2-rw-r--r-- 1 root root 0 Apr 22 16:02 3 test1和test2目录下的1文件,创建的时间是一样的;touch test2/1之后创建时间晚了一些。同步的时候不加-u选项,发现同步后的时间还是test1/1的创建时间; # ll test1/1 test2/1-rw-r--r-- 1 root root 0 Apr 22 16:02 test1/1-rw-r--r-- 1 root root 0 Apr 22 16:02 test2/1# touch test2/1# ll test1/1 test2/1-rw-r--r-- 1 root root 0 Apr 22 16:02 test1/1-rw-r--r-- 1 root root 0 Apr 22 16:31 test2/1# rsync -av test1/1 test2/1sending incremental file list1sent 65 bytes received 31 bytes192.00bytes/sectotal size is 0 speedup is 0.00# ll test1/1 test2/1-rw-r--r-- 1 root root 0 Apr 22 16:02 test1/1-rw-r--r-- 1 root root 0 Apr 22 16:02 test2/1加-u选项,如果目标文件比源文件新,那么会忽略掉该文件,不做同步;# touch test2/1# ll test2/1-rw-r--r-- 1 root root 0 Apr 22 16:37 test2/1# rsync -avu test1/1 test2/1sending incremental file listsent 26 bytes received 12 bytes76.00 bytes/sectotal size is 0 speedup is 0.00# ll test2/1 test1/1-rw-r--r-- 1 root root 0 Apr 22 16:02 test1/1-rw-r--r-- 1 root root 0 Apr 22 16:37 test2/1删除test1/123.txt,同步之后test2目录下的123.txt不会删除;加--delete选项,同步之后test2目录下的123.txt文件也会删除;具体作用是删除目标目录比源目录多出来的文件;# rm -f test1/123.txt # ls test1/123# rsync -av test1/ test2/sending incremental file list./1sent 94 bytes received 34 bytes256.00bytes/sectotal size is 0 speedup is 0.00# ls test2/1123.txt23# rsync -av --delete test1/ test2/sending incremental file listdeleting 123.txtsent 52 bytes received 12 bytes128.00bytes/sectotal size is 0speedup is 0.00# ls test2/123--exclude选项的作用是,同步的过程中排除文件;选项后面的文件不会同步拷贝到目标目录下; # touch test1/aa# rsync -a --exclude="aa"test1/ test2/# ls test1/123aa# ls test2/123--progress选项的作用是显示同步过程的详细信息;--exclude选项后面也可以使用通配符 * # touch test1/1.txt test1/2.txt# rsync -a --progress--exclude="*.txt" test1/ test2/sending incremental file list./aa         0 100%   0.00kB/s    0:00:00 (xfer#1,to-check=0/5)sent 109 bytes received 34 bytes286.00bytes/sectotal size is 0 speedup is 0.00# ls test2/123aa# ls test1/11.txt2 2.txt3aa 3. rsync选项总结rsync -avdir1/ dir2/    #其中dir2/目录可以不存在,记得同步目录时一定要在末尾加上/-a    会把软连接原原本本的拷贝过去;-v    可视化显示同步信息,如传输的文件大小,传输速度,发送和接收的大小;rsync -avL test1/ test2/    加-L会拷贝软连接文件对应的源文件到目标目录里面;touch test2/1.txt; rsync -avutest1/ test2/    -u 选项的作用是,如果目标文件比源文件新,那么会忽略掉该文件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. rsync应用实例 - ssh方式访问 第一种方式:本地拷贝到远程linux主机,需要输入远程主机的密码;# rsync -avL test1/ root@192.168.20.10:/tmp/test2/root@192.168.20.10's password: sending incremental file listcreated directory /tmp/test2./11.txt22.txt3aasent 336 bytes received 129 bytes132.86bytes/sectotal size is 0 speedup is 0.00第二种方式:从远程linux主机同步到本地机器,也需要输入远程主机的密码;# rsync -avLroot@192.168.20.10:/tmp/test2/ ./test3/root@192.168.20.10's password: receiving incremental file listcreated directory ./test3./11.txt22.txt3aasent 128 bytes received 329 bytes130.57bytes/sectotal size is 0 speedup is 0.00 通过创建秘钥对,让两台机器产生信任关系同步数据时不用输入密码具体步骤如下:在A机器当前用户家目录创建.ssh目录,执行ssh-keygen命令,提示输入密码的时候直接回车,默认为空密码,最后生成公钥id_rsa.pub和私钥id_rsa文件; # mkdir .ssh# 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:ab:0e:83:11:69:69:34:d2:34:87:b8:07:40:36:e4:a3 root@yong.comThe key's randomart image is:+--[ RSA 2048]----+|*O=..         ||=o+*            || =*             ||ooo.            ||E..   S      ||   o   .      ||. o   .       ||   o .         ||   .o          |+-----------------+[root@yong ~]# ls .ssh/id_rsa id_rsa.pub# cat .ssh/id_rsa.pub ssh-rsaAAAAB3NzaC1yc2EAAAABIwAAAQEArLHpwzyzfPtjihx90/vdl9HqtLfmpuNDGYL+UWOcFAAul6PVz81fb/0RSAAPSu1Q3UdBWXlTqPUH4JucwaxYW+obW/zmteuZRaGf06iY01cj/Nr74ML0792cvfjCU87FyEH+ZcvNhgRU+iTo+ES0kkLEuHV1x3JJLOhgYzIU0RtuU3CRiFxPHo92jNjpOs66YW3TbOX0AEB47WoRmKThiQVwoU7Lwqpl/N7vQHtdj9vPtsVZsguKlCB5a2YoxRpnbdn8a5jdzVKuy+hBrdfP/1NkmVU9mBTV/f0b+is5uHmQqNVAQW5fIi7QdVWG8HuyQliOXZUxoxGPCAjCuNy+dw==root@yong.com拷贝A机器的公钥内容,复制到B机器的/root/.ssh/authorized_keys文件中(如果B机器没有.ssh目录和authorized_keys文件需要创建)# mkdir .ssh# chmod 700 .ssh/# touch .ssh/authorized_keys# vi .ssh/authorized_keys返回到A机器,执行命令sshroot@192.168.20.10(B机器的ip地址)不用输入密码即可访问B机器了。 # ssh root@192.168.20.10The authenticity of host '192.168.20.10(192.168.20.10)' can't be established.RSA key fingerprint is 84:47:af:bf:11:69:43:aa:bc:fe:9b:d6:08:b4:c4:1a.Are you sure you want to continue connecting (yes/no)?yesWarning: Permanently added '192.168.20.10' (RSA) tothe list of known hosts.Last login: Wed Apr 22 19:35:40 2015 from 192.168.20.1# hostname localhost.localdomainA机器执行exit退出,执行同步命令,这次不用输入密码即可同步。 # exitlogoutConnection to 192.168.20.10 closed.# rsync -av rsync/test1/root@192.168.20.10:/tmp/test3/sending incremental file listcreated directory /tmp/test3./11.txt22.txt3aasent 336 bytes received 129 bytes310.00bytes/sectotal size is 0 speedup is 0.00如果ssh端口不是22,那么需要指定一个端口号,命令为:rsync-av "--rsh=ssh -p port” /dir1/ root@192.168.20.10:/tmp/dir2/ 或者 rsync -av-e "ssh -p port"/dir1/ root@192.168.20.10:/tmp/dir2/ 更改B机器的ssh端口号为222,在A机器使用telnet 检测B机器22端口,提示连接拒绝,当然也不能同步数据,需要指定端口号"--rsh=ssh -p 222" 可以同步数据,更改端口号之后更加安全,防止他人通过端口攻击。 更改ssh端口号,编辑/etc/ssh/sshd_config文件,找到PORT22这一行更改即可,保存退出后,重启sshd服务生效。1# vi /etc/ssh/sshd_config # service sshd restart停止sshd:                                             [确定]正在启动sshd:# telnet 192.168.20.10 22Trying 192.168.20.10...telnet: connect to address 192.168.20.10: Connectionrefused # rsync -av rsync/test1/root@192.168.20.10:/tmp/test2/ssh: connect to host 192.168.20.10 port 22: Connectionrefusedrsync: connection unexpectedly closed (0 bytesreceived so far) rsync error: error in rsync protocol data stream (code12) at io.c(600) # rsync -av "--rsh=ssh-p 222" rsync/test1/ root@192.168.20.10:/tmp/test2/sending incremental file listcreated directory /tmp/test2./11.txt22.txt3aasent 336 bytes received 129 bytes310.00bytes/sectotal size is 0 speedup is 0.00 5. rsync应用实例 - 服务器客户端C/S模式 这种方式可以理解成这样,在远程主机上建立一个rsync的服务器,在服务器上配置好rsync的各种应用,然后本机作为rsync的一个客户端去连接远程的rsync服务器。安全性能会大大提高,也可以自定义配置很多项目,比较灵活。日常环境推荐使用。 需要新建配置文件 /etc/rsyncd.conf ,内容如下,#号后的内容不用写进去;#port=873      #监听端口默认为873,也可以是别的端口log file=/var/log/rsync.log#指定日志pid file=/var/run/rsyncd.pid#指定pid#address=192.168.20.10 #可以定义绑定的ip以上部分为全局配置部分,以下为模块内的设置    #为模块名,自定义path=/tmp/rsync # 指定该模块对应在哪个目录下use chroot=true #是否限定在该目录下,默认为true,当有软连接时,需要改为faslemax connections=4 # 指定最大可以连接的客户端数read only=no#是否为只读,yes为只读,不能在对应目录下写入文件。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 实验,编辑rsyncd.conf文件,保存退出;配置文件hosts allow 允许访问主机设置ip网段,也可以设置固定ip,多个ip用空格分隔。# cat /etc/rsyncd.conf #port=873log file=/var/log/rsync.logpid file=/var/run/rsyncd.pid#address=192.168.20.10path=/tmp/rsyncuse chroot=truemax connections=4read only=nolist=trueuid=rootgid=root#auth users=test#secrets file=/etc/rsyncd.passwdhosts allow=192.168.20.0/24启动rsync服务,进程也启动,默认873端口也监听; # rsync --daemon# ps aux |grep rsyncroot   15830.00.16232   644 ?      Ss14:49   0:00 rsync --daemonroot   15850.00.15976   740 pts/0    S+14:50   0:00 grep rsync# netstat -nlp |grep rsynctcp       0      0 0.0.0.0:873               0.0.0.0:*      LISTEN      1583/rsync          tcp       0      0 :::873                      :::*         LISTEN      15配置OK后,访问远程服务器#rsync-av test@192.168.20.10::test/test1/ /tmp/test1/中间2个冒号,后面跟模块名test,后面跟test1目录,实际就是/tmp/rsync/test1/目录,模块对应的目录里面。 1)从本地同步数据到服务器端,同步的时候也可以进行改名;# rsync -avL --progress test1/test@192.168.20.10::test/test/sending incremental file listcreated directory /test./1         0100%    0.00kB/s    0:00:00 (xfer#1, to-check=8/10)1.txt      1076100%    0.00kB/s   0:00:00 (xfer#2, to-check=7/10)2         0100%    0.00kB/s    0:00:00 (xfer#3, to-check=6/10)2.txt         0100%    0.00kB/s    0:00:00 (xfer#4, to-check=5/10)3         0100%    0.00kB/s    0:00:00 (xfer#5, to-check=4/10)aa         0100%    0.00kB/s    0:00:00 (xfer#6, to-check=3/10)tree       36464100%    1.93MB/s    0:00:00 (xfer#7, to-check=2/10)a1/b1/sent 37992 bytes received 152 bytes3632.76bytes/sectotal size is 37540 speedup is 0.98 # ls test/11.txt2 2.txt3a1 aab1tree2)从服务器同步数据到本地;# rsync -avL --progress192.168.20.10::test/abc ./receiving incremental file listabc         0100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)sent 45 bytes received 94 bytes13.24 bytes/sectotal size is 0 speedup is 0.00# ls abc abc3)更改服务器配置文件read only=yes再从本地同步数据到服务器,会报错提示module模块只读,不能写入数据;从服务器同步到本地不受影响。 # sed -i 's/read only=no/readonly=yes/' /etc/rsyncd.conf # grep 'read' /etc/rsyncd.conf read only=yes# rsync -avL --progress test1/192.168.20.10::test/abc/sending incremental file listERROR: module is read onlyrsync error: syntax or usage error (code 1) atmain.c(866) rsync: read error: Connection reset by peer (104)rsync error: error in rsync protocol data stream (code12) at io.c(759)
页: [1]
查看完整版本: linux下数据备份工具rsync的使用