rsync备份项目
rsync的同步都不是实时同步的,操作一次才能同步一次,即使放定时任务,也不是实时同步。
rsync项目1 rsync命令本地同步备份
一 使用rsync实现数据本地同步备份(前提:本地无需开启rsync服务,但要安装rsync命令)
常见的备份时保持文件属性不变的选项:-avz, 下面的-av,也可以写成-avz
v显示详细信息,z传输时进行压缩以提高效率。a归档,递归传输文件,并保持属性,等于-rtopgDl.
1 rsync -av 要备份的目录 备份到的目录 //同步备份整个目录
2 rsync -av 要备份的目录 / 备份到的目录 //同步备份目录下的各个文件
3 rsync - -delete -av 要备份的目录 / 备份到的目录 //删除目录位置有而原始位置没有的文件
4 rsync -av --bwlimit=100 要备份的目录 / 备份到的目录 //同步限速为100kb
注意3中:
-av和- -delete的顺序无所谓,前面目录有啥,后面目录就有啥,前面目录如果为空,后面也为空,后面跟随前面。
本地机器上配置:
1)安装rsync命令,(不是启动rsync服务):yum -y install rsync
2)建立测试文件:mkdir /yuanshi, echo “1111” > /yuanshi/a.txt, echo “2222” > /yuanshi/b.txt
3)用三种方法使用rsync命令备份:
a)rsync -av /yuanshi /beifen //同步备份整个目录,连yuanshi的目录名一块备份下来
此时查看:/yuanshi目录下:有a.txt和b.txt, /beifen目录下有:/yuanshi/ 、a.txt和b.txt
b)rsync -av /yuanshi/ /beifen //同步备份目录下的各个文件,只备份yuanshi目录名中文件。
此时查看:/yuanshi目录下:有a.txt和b.txt, /beifen目录下有:a.txt和b.txt
c) rm -rf /yuanshi/a.txt ,此时:/yuanshi目录下有:b.txt, /beifen目录下有:a.txt和b.txt
rsync - -delete -av /yuanshi/ /beifen/
此时查看:/yuanshi目录下有:b.txt, /beifen目录下有:b.txt
例子:rsync的删除功能。--delete
[root@localhost 桌面]# mkdir /test/
[root@localhost 桌面]# echo "1111" > /test/a.txt
[root@localhost 桌面]# echo "2222" > /test/b.txt
[root@localhost 桌面]# ls /test/
a.txt b.txt
[root@localhost 桌面]# mkdir /null
[root@localhost 桌面]# ls /null/
空
[root@localhost 桌面]# rsync -av --delete /null/ /test/
[root@localhost 桌面]# ls /null/
空
[root@localhost 桌面]# ls /test/
空
rsync项目2 rsync命令+ssh远程同步备份
使用rsync+ssh实现数据远程同步备份,实现远程下载和上传文件的同步备份
1把远程机上的文件目录下载到本机目录:(远程:安装rsync命令,sshd服务,远程机有用户, 本地:rsync命令)
rsync -av 远程机用户名@远程机IP地址:/远程机目录 本地目录
rsync -av - -delete 远程机用户名@远程机IP地址:/远程机目录 本地目录
rsync -av --bwlimit=100 远程机用户名@远程机IP地址:/远程机目录 本地目录 //下载时限速为100k
2把本机目录文件上传到远程机目录:(远程:安装rsync命令,sshd服务,远程机有用户, 本地:rsync命令)
rsync -av 本地目录 远程机用户名@远程机IP地址:/远程机目录
rsync -av - -delete 本地目录 远程机用户名@远程机IP地址:/远程机目录
rsync -av --bwlimit=100 本地目录 远程机用户名@远程机IP地址:/远程机目录 //上传时限速为100k
注意:- -delete选项是指:删除目录位置有而原始位置没有的文件,也就是说,后面目录要跟着前面目录同步。前面有啥,后面就跟着有啥。
例子:服务端和客户端都是root账户和root密码时的rsync数据同步。
在服务器端配置:
1)双方都用root账户来连接,密码都是123456
2)安装rsync服务: yum -y install rsync
3)需要启动sshd服务,因为rsync备份的传输端口就是通过sshd的22端口传输的.
4)关闭iptables,/etc/init.d/iptables
5)关闭selinux, setenforce 0, 或者修改配置文件永久关闭
6)创建测试文件/yuanshi/a.txt,/yuanshi/b.txt
7)创建接收目录,mkdir /jieshou
8)chmod -R o+w /jieshou/
客户端备份服务器上的文件: //远程下载
创建备份目录和备份文件,mkdir /beifen, rsync -av root@192.168.4.5:/yuanshi/ /beifen/
回车后,输入对方root的密码123456,即可备份成功。此时,客户端/beifen目录下有a.txt和b.txt
此时,服务器端删除一个文件:
rm -rf /yuanshi/a.txt, 现在服务器上只剩下b.txt
此时,客户端再备份服务器上的文件:(另一种方法)
rsync –av - -delete root@192.168.4.5:/yuanshi/ /beifen/
回车后,输入root的密码123456即可备份,现在客户端 /beifen目录下有的文件是:b.txt
客户端上传本地文件到服务器: //远程上传
mkdir /c
echo "cccc" > /c/c.txt
yum -y install rsync
rsync -av /c/ root@192.168.4.5:/jieshou
输入对方root密码 123456即可上传到服务器。
案例1:服务端和客户端都是root账户和root密码时的rsync数据同步。
(服务器端需要:启动sshd服务,rsync命令,用户名和对应密码。但客户端不需要启动这些服务,只需有rsync命令)
在192.168.4.5上:(服务器)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1970) 正在运行...
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# mkdir /yuanshi
[root@localhost 桌面]# echo "1111" > /yuanshi/a.txt
[root@localhost 桌面]# echo "2222" > /yuanshi/b.txt
[root@localhost 桌面]# ls /yuanshi/
a.txt b.txt
[root@localhost 桌面]# mkdir /jieshou
[root@localhost 桌面]# chmod -R o+w /jieshou/
[root@localhost 桌面]# ll -d /jieshou/
drwxr-xrwx. 2 root root 4096 5月 21 19:43 /jieshou/
[root@localhost 桌面]# ls /jieshou/
空 //远程上传前为空
[root@localhost 桌面]# ls /jieshou/
c.txt //远程上传后有文件
在192.168.4.205上(客户端)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1837) 正在运行...
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# mkdir /beifen
[root@localhost 桌面]# rsync -av root@192.168.4.5:/yuanshi/ /beifen/ //远程下载
The authenticity of host '192.168.4.5 (192.168.4.5)' can't be established.
RSA key fingerprint is b6:6b:72:63:3f:f3:a2:3e:db:a9:b2:f1:06:2a:a3:76.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.4.5' (RSA) to the list of known hosts.
root@192.168.4.5's password: 输入对方root密码123456
receiving incremental file list
./
a.txt
b.txt
sent 52 bytes received 162 bytes 10.97 bytes/sec
total size is 10 speedup is 0.05
[root@localhost 桌面]# ls /beifen/
a.txt b.txt
[root@localhost 桌面]# mkdir /c
[root@localhost 桌面]# echo "cccc" > /c/c.txt
[root@localhost 桌面]# ls /c/
c.txt
[root@localhost 桌面]# rsync -av /c/ root@192.168.4.5:/jieshou //远程上传
root@192.168.4.5's password: 输入对方密码:123456
sending incremental file list
./
c.txt
sent 91 bytes received 34 bytes 19.23 bytes/sec
total size is 5 speedup is 0.04
案例2:服务端和客户端都是普通账户和对应密码时的rsync数据同步。
(服务器端需要:启动sshd服务,rsync命令,用户名和对应密码。但客户端不需要启动这些服务,只需有rsync命令)
在192.168.4.5上:(服务器)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1970) 正在运行...
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# useradd shi1
[root@localhost 桌面]# echo "123" |passwd --stdin shi1
root@localhost 桌面]# su - shi1
[shi1@localhost ~]$ ls
空
[shi1@localhost ~]$ mkdir ~/yuanshi
[shi1@localhost ~]$ echo "1111" > ~/yuanshi/a.txt
[shi1@localhost ~]$ echo "2222" > ~/yuanshi/b.txt
[shi1@localhost ~]$ ls ~/yuanshi/
a.txt b.txt
[shi1@localhost ~]$ mkdir ~/jieshou
[shi1@localhost ~]$ ls
jieshou yuanshi
[shi1@localhost ~]$ ls jieshou/
空 //远程上传前为空
[shi1@localhost ~]$ ls jieshou/
c.txt //远程上传后有文件
在192.168.4.205上(客户端)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1837) 正在运行...
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# useradd shi2
[root@localhost 桌面]# echo "456" |passwd --stdin shi2
[root@localhost 桌面]# su - shi2
[shi2@localhost ~]$ ls
空
[shi2@localhost ~]$ mkdir ~/beifen
[shi2@localhost ~]$ rsync -av shi1@192.168.4.5:~/yuanshi/ ~/beifen/ //远程下载
The authenticity of host '192.168.4.5 (192.168.4.5)' can't be established.
RSA key fingerprint is b6:6b:72:63:3f:f3:a2:3e:db:a9:b2:f1:06:2a:a3:76.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.4.5' (RSA) to the list of known hosts.
shi1@192.168.4.5's password: 输入对方密码:123
receiving incremental file list
./
a.txt
b.txt
sent 52 bytes received 175 bytes 34.92 bytes/sec
total size is 10 speedup is 0.04
[shi2@localhost ~]$ ls ~/beifen/
a.txt b.txt
[shi2@localhost ~]$ mkdir ~/shangchuan
[shi2@localhost ~]$ echo "cccc" > ~/shangchuan/c.txt
[shi2@localhost ~]$ ls ~/shangchuan/
c.txt
[shi2@localhost ~]$ rsync -av ~/shangchuan/ shi1@192.168.4.5:~/jieshou/ //远程上传
shi1@192.168.4.5's password:
sending incremental file list
./
c.txt
sent 104 bytes received 34 bytes 21.23 bytes/sec
total size is 5 speedup is 0.04
案例3:服务端和客户端都是普通账户和对应密码时的rsync数据同步。(服务器sshd端口号为:8086,需-e指定通道)
在192.168.4.5上:(服务器) (sshd端口号为:8086)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1970) 正在运行...
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# useradd shi1
[root@localhost 桌面]# echo "123" |passwd --stdin shi1
root@localhost 桌面]# su - shi1
[shi1@localhost ~]$ ls
空
[shi1@localhost ~]$ mkdir ~/yuanshi
[shi1@localhost ~]$ echo "1111" > ~/yuanshi/a.txt
[shi1@localhost ~]$ echo "2222" > ~/yuanshi/b.txt
[shi1@localhost ~]$ ls ~/yuanshi/
a.txt b.txt
[shi1@localhost ~]$ mkdir ~/jieshou
[shi1@localhost ~]$ ls
jieshou yuanshi
[shi1@localhost ~]$ ls jieshou/
空 //远程上传前为空
[shi1@localhost ~]$ ls jieshou/
c.txt //远程上传后有文件
在192.168.4.205上(客户端)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1837) 正在运行...
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# useradd shi2
[root@localhost 桌面]# echo "456" |passwd --stdin shi2
[root@localhost 桌面]# su - shi2
[shi2@localhost ~]$ ls
空
[shi2@localhost ~]$ mkdir ~/beifen
[shi2@localhost ~]$ rsync -av -e "ssh -p 8086" shi1@192.168.4.5:~/yuanshi/ ~/beifen //远程下载(-e通道)
shi1@192.168.4.5's password: 输入对方密码:123
receiving incremental file list
./
a.txt
b.txt
sent 52 bytes received 175 bytes 50.44 bytes/sec
total size is 10 speedup is 0.04
[shi2@localhost ~]$ ls ~/beifen/
a.txt b.txt
[shi2@localhost ~]$ mkdir ~/shangchuan
[shi2@localhost ~]$ echo "cccc" > ~/shangchuan/c.txt
[shi2@localhost ~]$ ls ~/shangchuan/
c.txt
[shi2@localhost ~]$ rsync -av -e "ssh -p 8086" ~/shangchuan/ shi1@192.168.4.5:~/jieshou/ 上传(-e指定通道)
shi1@192.168.4.5's password: 输入对方密码:123
sending incremental file list
./
c.txt
sent 104 bytes received 34 bytes 21.23 bytes/sec
total size is 5 speedup is 0.04
案例4:服务端和客户端都是普通账户和无需密码时的rsync数据同步。(服务器sshd端口号为:8086,需-e指定通道)
在192.168.4.5上:(服务器) (sshd端口号为:8086)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1970) 正在运行...
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# useradd shi1
[root@localhost 桌面]# echo "123" |passwd --stdin shi1
root@localhost 桌面]# su - shi1
[shi1@localhost ~]$ ls
空
[shi1@localhost ~]$ ls -a
. .. .bash_logout .bash_profile .bashrc .gnome2 jieshou .mozilla yuanshi //cp公钥前
[shi1@localhost ~]$ ls -a
. .bash_logout .bashrc jieshou .ssh.. .bash_profile .gnome2 .mozilla yuanshi //cp公钥后
[shi1@localhost ~]$ ls .ssh/
authorized_keys
[shi1@localhost ~]$ mkdir ~/yuanshi
[shi1@localhost ~]$ echo "1111" > ~/yuanshi/a.txt
[shi1@localhost ~]$ echo "2222" > ~/yuanshi/b.txt
[shi1@localhost ~]$ ls ~/yuanshi/
a.txt b.txt
[shi1@localhost ~]$ mkdir ~/jieshou
[shi1@localhost ~]$ ls
jieshou yuanshi
[shi1@localhost ~]$ ls jieshou/
空 //远程上传前为空
[shi1@localhost ~]$ ls jieshou/
c.txt //远程上传后有文件
在192.168.4.205上(客户端)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1837) 正在运行...
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# useradd shi2
[root@localhost 桌面]# echo "456" |passwd --stdin shi2
[root@localhost 桌面]# su - shi2
[shi2@localhost ~]$ ls
空
[shi2@localhost ~]$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/shi2/.ssh/id_dsa):
Enter passphrase (empty for no passphrase): //直接回车
Enter same passphrase again: //直接回车
Your identification has been saved in /home/shi2/.ssh/id_dsa.
Your public key has been saved in /home/shi2/.ssh/id_dsa.pub.
The key fingerprint is:
bb:f7:fe:6a:98:54:2c:c6:45:f1:a2:c0:10:ff:8f:47 shi2@localhost.localdomain
The key's randomart image is:
+--[ DSA 1024]----+
| o. .o. |
| + .. |
| +. o. . |
| o+.o. |
| S.ooE |
| ..+ |
| ...oo |
| .+.. |
| .. ++o. |
+-----------------+
[shi2@localhost ~]$ ls .ssh/
id_dsa id_dsa.pub
[shi2@localhost ~]$ ssh-copy-id -i .ssh/id_dsa.pub "-p 8086 shi1@192.168.4.5" //cp公钥到服务器
The authenticity of host '[192.168.4.5]:8086 ([192.168.4.5]:8086)' can't be established.
RSA key fingerprint is b6:6b:72:63:3f:f3:a2:3e:db:a9:b2:f1:06:2a:a3:76.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.4.5]:8086' (RSA) to the list of known hosts.
shi1@192.168.4.5's password: 输入对方密码:123
Now try logging into the machine, with "ssh '-p 8086 shi1@192.168.4.5'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[shi2@localhost ~]$ ls .ssh/
id_dsa id_dsa.pub known_hosts
[shi2@localhost ~]$ mkdir ~/beifen
[shi2@localhost ~]$ rsync -av -e "ssh -p 8086" shi1@192.168.4.5:~/yuanshi/ ~/beifen //远程下载(-e通道)
receiving incremental file list //无需输入对方用户密码了
./
a.txt
b.txt
sent 52 bytes received 175 bytes 151.33 bytes/sec
total size is 10 speedup is 0.04
[shi2@localhost ~]$ ls beifen/
a.txt b.txt
[shi2@localhost ~]$ mkdir ~/shangchuan
[shi2@localhost ~]$ echo "cccc" > ~/shangchuan/c.txt
[shi2@localhost ~]$ ls ~/shangchuan/
c.txt
[shi2@localhost ~]$ rsync -av -e "ssh -p 8086" ~/shangchuan/ shi1@192.168.4.5:~/jieshou/ 上传(-e指定通道)
sending incremental file list //无需输入对方用户密码了
./
c.txt
sent 104 bytes received 34 bytes 276.00 bytes/sec
total size is 5 speedup is 0.04
rsync项目3 搭建rsync服务器共享方法,让客户端来同步服务器上的文件
使用rsync服务实现数据远程同步备份,通过服务器上设置服务,客户端来同步方法。
(rsync服务器上:需要开启rsync服务,sshd服务,有用户,本地客户端:rsync命令,无需开:sshd和rsync服务)
服务器端一旦配置好rsync服务,上传和下载文件操作都是在客户端进行。
客户端远程查看: rsync -av 服务器IP::
客户端远程下载:
rsync -av 服务器用户名:服务器IP::共享目录名 本地目录
rsync -av --delete 服务器用户名:服务器IP::共享目录名 本地目录
rsync -av --bwlimit=100 服务器用户名:服务器IP::共享目录名 本地目录 //下载时限速为100k
客户端远程上传:
rsync -av 本地目录 服务器用户名:服务器IP::共享目录名
rsync -av - -delete 本地目录 服务器用户名:服务器IP::共享目录名
rsync -av --bwlimit=100 本地目录 服务器用户名:服务器IP::共享目录名 //上传时限速为100k
注意:项目3不适合通道方法,即使改变sshd端口号,也不能用指定通道方法,因为输入的密码是对方机器指定的密码,不是对方系统密码,也不适合秘钥对无密码连接,想无密码连接的方法就是建立并指定对方设置的密码文件。
-av和- -delete的顺序无所谓,前面目录有啥,后面目录就有啥,前面目录如果为空,后面也为空,后面跟随前面。
例子: 服务端和客户端都是用root账户登陆时候的rsync数据同步。
在rsync服务器上配置:(192.168.4.5)
1)建立测试文件:mkdir /homework, echo "1111" > /homework/1.txt, echo "2222" > /homework/2.txt
2)useradd rsync -s /sbin/nologin
3) 修改配置文件,创建共享文件的路径和设置
vim /etc/rsyncd.conf //本身没有这个文件,需要自己写
uid=rsync
gid=rsync
max connections=200 //能连接rsync服务器的最大客户端连接数为200
timeout=300 //连接rsync服务器的超时时间
pid file=/var/run/rsyncd.pid //进程的pid编号放到文件里面
lock file=/var/run/rsyncd.lock //文件锁的机制,多个文件需要同时同步时上锁,一个一个同步,防止冲突
log file=/var/log/rsyncd.log //log日志,将来出错了,有任何问题看log
[homework] //共享目录名
comment this is student homework
path=/homework //共享目录的路径
read only=yes //注意:上传时需要改为no或false
auth users=student //这里的用户名和系统用户名不冲突,可以不是系统用户名。它是虚拟用户
secrets file=/etc/password.txt //用户对应的密码文件,路径可自定义
list=false //false表示不允许列表,所有都允许,如果只允许一部分时候,可以为yes
hosts allow=192.168.4.0/24 //允许的网段访问我rsync服务
hosts deny=10.10.10.0/24 //拒绝的网段访问我rsync服务
4)根据共享文件的配置文件,设置用户名和密码
vim /etc/password.txt
student:456
chmod 600 /etc/password.txt
5)安装rsync服务:yum -y install rsync
6)安装xinetd服务:yum -y install xinetd
7)修改配置文件,唤醒rsync服务
vim /etc/xinetd.d/rsync
disable = no
/etc/init.d/xinetd restart
7)需要启动sshd服务,因为rsync备份的传输端口就是通过sshd的22端口传输的
8)关闭iptables,/etc/init.d/iptables stop
9) 关闭selinux, setenforce 0, 或者修改配置文件永久关闭
10)给共享目录加一个w权限,供客户端上传用: chmod o+w /homework
注意:上面rsync启动是用xinetd带动启动的,也可以用下面守护进程方式启动:
[root@localhost 桌面]# rsync --daemon //以守护进程模式启动。启动后是873端口。
客户端下载服务器上的共享文件:(192.168.4.205)
查看服务器上的共享文件: rsync -av 192.168.4.5:: 查看不到,但能下载
远程同步下载:(一般方式同步下载)
mkdir /beifen, rsync -av student@192.168.4.5::homework /beifen/
输入服务器给的用户名student对应的密码:456,可以下载下来,此时:客户端/beifen目录下有:1.txt和2.txt
将rsync服务器上的一个文件删除
rm -rf /homework/1.txt, 现在服务器上只有2.txt了 ,而客户端上有:1.txt和2.txt
远程同步下载:(另一个方式同步下载)
rsync -av - -delete student@192.168.4.5::homework /beifen/ ,此时,客户端只有2.txt了
客户端把本地的文件上传到服务器的共享目录中:(192.168.4.205)
注意:上传时服务器上需要将配置文件/etc/rsyncd.conf中的read only改为no, read only=no
上传:rsync -av /a/ student@192.168.4.5::homework
输入服务器指定的用户名student的密码:456, 即可上传成功。
此时服务器上/homework目录下有:2.txt a.txt b.txt
注意:下载和上传不需要输入对方密码的设置
客户端设置:
[root@localhost 桌面]# echo “456” > /etc/rsy.passwd //客户端先将服务器端给的密码写入一个文件
rsync -av student@192.168.4.5::homework /beifen/ --password-file=/etc/rsy.passwd 不输密码,即可下载
rsync -av /a/ student@192.168.4.5::homework --password-file=/etc/rsy.passwd 不输密码,即可上传
案例1: 服务端和客户端都是用root账户登陆时候的rsync数据同步。
在rsync服务器上配置:(192.168.4.5)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1809) 正在运行...
[root@localhost 桌面]# mkdir /homework
[root@localhost 桌面]# chmod -R o+w /homework/
[root@localhost 桌面]# echo "1111" > /homework/1.txt
[root@localhost 桌面]# echo "2222" > /homework/2.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt
[root@localhost 桌面]# vim /etc/rsyncd.conf
[homework]
comment this is student homework
path=/homework
read only=no //允许上传
auth users=student
secrets file=/etc/password.txt
[root@localhost 桌面]# vim /etc/password.txt
student:456
[root@localhost 桌面]# chmod 600 /etc/password.txt
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# yum -y install xinetd
[root@localhost 桌面]# vim /etc/xinetd.d/rsync
disable = no
[root@localhost 桌面]# /etc/init.d/xinetd restart
[root@localhost 桌面]# netstat -anptu |grep 873
tcp 0 0 127.0.0.1:873 127.0.0.1:56445 TIME_WAIT -
tcp 0 0 :::873 :::* LISTEN 3261/xinetd
[root@localhost 桌面]# rsync -av 127.0.0.1::
homework
[root@localhost 桌面]# ls /homework/
1.txt 2.txt //客户端上传前
[root@localhost 桌面]# ls /homework/
1.txt 2.txt a.txt b.txt //客户端上传后
客户端下载或上传服务器上的共享文件:(192.168.4.205)
查看服务器上的共享文件:
[root@localhost 桌面]# rsync -av 192.168.4.5::
homework
[root@localhost 桌面]# mkdir /beifen
[root@localhost 桌面]# rsync -av student@192.168.4.5::homework /beifen/ //远程下载
Password: 输入对方设置的student的密码:456
receiving incremental file list
./
1.txt
2.txt
sent 102 bytes received 225 bytes 93.43 bytes/sec
total size is 10 speedup is 0.03
[root@localhost 桌面]# ls /beifen/
1.txt 2.txt
[root@localhost 桌面]# mkdir /a
[root@localhost 桌面]# echo "aaaa" > /a/a.txt
[root@localhost 桌面]# echo "bbbb" > /a/b.txt
[root@localhost 桌面]# ls /a/
a.txt b.txt
[root@localhost 桌面]# rsync -av /a/ student@192.168.4.5::homework //远程上传
Password:
sending incremental file list
./
rsync: failed to set times on "/." (in homework): Operation not permitted (1)
a.txt
b.txt
sent 149 bytes received 49 bytes 3.12 bytes/sec
total size is 10 speedup is 0.05
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
注意:下载和上传不需要输入对方密码的设置(在客户端做如下设置即可)
[root@localhost 桌面]# echo "456" > /etc/abc.passwd //客户端先将服务器端给的密码写入一个文件
[root@localhost 桌面]# chmod 600 /etc/abc.passwd //必须要有这一步骤,否则还需要输入密码
rsync -av student@192.168.4.5::homework /beifen/ --password-file=/etc/abc.passwd 不输密码,即可下载
rsync -av /a/ student@192.168.4.5::homework --password-file=/etc/abc.passwd 不输密码,即可上传
案例2: 服务端和客户端都是用root账户登陆时候的rsync数据同步。
在rsync服务器上配置:(192.168.4.5)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1809) 正在运行...
[root@localhost 桌面]# mkdir /homework
[root@localhost 桌面]# chmod -R o+w /homework/
[root@localhost 桌面]# echo "1111" > /homework/1.txt
[root@localhost 桌面]# echo "2222" > /homework/2.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt
[root@localhost 桌面]# vim /etc/rsyncd.conf
uid=rsync
gid=rsync
max connections=200
timeout=300
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
[homework]
comment this is student homework
path=/homework
read only=no
auth users=student
secrets file=/etc/password.txt
list=false
hosts allow=192.168.4.0/24
hosts deny=10.10.10.0/24
wq
[root@localhost 桌面]# vim /etc/password.txt
student:456
[root@localhost 桌面]# chmod 600 /etc/password.txt
[root@localhost 桌面]# useradd -s /sbin/nologin rsync
[root@localhost 桌面]# chown -R rsync.rsync /homework/
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# yum -y install xinetd
[root@localhost 桌面]# vim /etc/xinetd.d/rsync
disable = no
[root@localhost 桌面]# /etc/init.d/xinetd restart
[root@localhost 桌面]# netstat -anptu |grep 873
tcp 0 0 192.168.4.5:45359 192.168.4.5:873 TIME_WAIT -
tcp 0 0 :::873 :::* LISTEN 3888/xinetd
[root@localhost 桌面]# rsync -av student@127.0.0.1::
空 //查看不到,但能下载
[root@localhost 桌面]# rsync -av student@127.0.0.1::homework /data/ //服务器端下载到本地,测试成功
Password: 输入student的密码:456
receiving incremental file list
./
1.txt
2.txt
sent 102 bytes received 227 bytes 73.11 bytes/sec
total size is 10 speedup is 0.03
[root@localhost 桌面]# ls /data/
1.txt 2.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt //客户端上传前
[root@localhost 桌面]# ls /homework/
1.txt 2.txt a.txt b.txt //客户端上传后
客户端下载或上传服务器上的共享文件:(192.168.4.205)
查看服务器上的共享文件:
[root@localhost 桌面]# rsync -av 192.168.4.5::
空 //查看不到
[root@localhost 桌面]# mkdir /beifen
[root@localhost 桌面]# rsync -av student@192.168.4.5::homework /beifen/ //远程下载
Password: 输入对方设置的student的密码:456
receiving incremental file list
./
1.txt
2.txt
sent 102 bytes received 227 bytes 43.87 bytes/sec
total size is 10 speedup is 0.03
[root@localhost 桌面]# ls /beifen/
1.txt 2.txt
[root@localhost 桌面]# mkdir /a
[root@localhost 桌面]# echo "aaaa" > /a/a.txt
[root@localhost 桌面]# echo "bbbb" > /a/b.txt
[root@localhost 桌面]# ls /a/
a.txt b.txt
[root@localhost 桌面]# rsync -av /a/ student@192.168.4.5::homework //远程上传
Password: 输入对方student密码:456
sending incremental file list
./
a.txt
b.txt
sent 149 bytes received 49 bytes 56.57 bytes/sec
total size is 10 speedup is 0.05
注意:下载和上传不需要输入对方密码的设置(在客户端做如下设置即可)
[root@localhost 桌面]# echo "456" > /etc/abc.passwd //客户端先将服务器端给的密码写入一个文件
[root@localhost 桌面]# chmod 600 /etc/abc.passwd //必须要有这一步骤,否则还需要输入密码
rsync -av student@192.168.4.5::homework /beifen/ --password-file=/etc/abc.passwd 不输密码,即可下载
rsync -av /a/ student@192.168.4.5::homework --password-file=/etc/abc.passwd 不输密码,即可上传
案例3: 服务端和客户端都是用root账户登陆时候的rsync数据同步。(服务器sshd端口号为:8086)
在rsync服务器上配置:(192.168.4.5)端口号为8086
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1809) 正在运行...
[root@localhost 桌面]# mkdir /homework
[root@localhost 桌面]# chmod -R o+w /homework/
[root@localhost 桌面]# echo "1111" > /homework/1.txt
[root@localhost 桌面]# echo "2222" > /homework/2.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt
[root@localhost 桌面]# vim /etc/rsyncd.conf
uid=rsync
gid=rsync
max connections=200
timeout=300
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
[homework]
comment this is student homework
path=/homework
read only=no
auth users=student
secrets file=/etc/password.txt
list=false
hosts allow=192.168.4.0/24
hosts deny=10.10.10.0/24
wq
[root@localhost 桌面]# vim /etc/password.txt
student:456
[root@localhost 桌面]# chmod 600 /etc/password.txt
[root@localhost 桌面]# useradd -s /sbin/nologin rsync
[root@localhost 桌面]# chown -R rsync.rsync /homework/
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# yum -y install xinetd
[root@localhost 桌面]# vim /etc/xinetd.d/rsync
disable = no
[root@localhost 桌面]# /etc/init.d/xinetd restart
[root@localhost 桌面]# netstat -anptu |grep 873
tcp 0 0 192.168.4.5:45359 192.168.4.5:873 TIME_WAIT -
tcp 0 0 :::873 :::* LISTEN 3888/xinetd
[root@localhost 桌面]# rsync -av student@127.0.0.1::
空 //查看不到,但能下载
[root@localhost 桌面]# rsync -av student@127.0.0.1::homework /data/ //服务器端下载到本地,测试成功
Password: 输入student的密码:456
receiving incremental file list
./
1.txt
2.txt
sent 102 bytes received 227 bytes 73.11 bytes/sec
total size is 10 speedup is 0.03
[root@localhost 桌面]# ls /data/
1.txt 2.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt //客户端上传前
[root@localhost 桌面]# ls /homework/
1.txt 2.txt a.txt b.txt //客户端上传后
客户端下载或上传服务器上的共享文件:(192.168.4.205)
注意:服务器端改变端口号,对客户端下载和上传不影响,因为,输入对方密码是服务器指定用户名student的,和服务器本身系统无关系,客户端还是按正常的下载和上传步骤即可。
查看服务器上的共享文件:
[root@localhost 桌面]# rsync -av 192.168.4.5::
空 //查看不到
[root@localhost 桌面]# mkdir /beifen
[root@localhost 桌面]# rsync -av student@192.168.4.5::homework /beifen/ //远程下载
Password: 输入对方设置的student的密码:456
receiving incremental file list
./
1.txt
2.txt
sent 102 bytes received 227 bytes 43.87 bytes/sec
total size is 10 speedup is 0.03
[root@localhost 桌面]# ls /beifen/
1.txt 2.txt
[root@localhost 桌面]# mkdir /a
[root@localhost 桌面]# echo "aaaa" > /a/a.txt
[root@localhost 桌面]# echo "bbbb" > /a/b.txt
[root@localhost 桌面]# ls /a/
a.txt b.txt
[root@localhost 桌面]# rsync -av /a/ student@192.168.4.5::homework //远程上传
Password: 输入对方student密码:456
sending incremental file list
./
a.txt
b.txt
sent 149 bytes received 49 bytes 56.57 bytes/sec
total size is 10 speedup is 0.05
注意:下载和上传不需要输入对方密码的设置(在客户端做如下设置即可)
[root@localhost 桌面]# echo "456" > /etc/abc.passwd //客户端先将服务器端给的密码写入一个文件
[root@localhost 桌面]# chmod 600 /etc/abc.passwd //必须要有这一步骤,否则还需要输入密码
rsync -av student@192.168.4.5::homework /beifen/ --password-file=/etc/abc.passwd 不输密码,即可下载
rsync -av /a/ student@192.168.4.5::homework --password-file=/etc/abc.passwd 不输密码,即可上传
案例4: 服务端和客户端都是用root账户登陆时候的rsync数据同步。(另一种启动方式:rsync --daemon)
在rsync服务器上配置:(192.168.4.5)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1809) 正在运行...
[root@localhost 桌面]# mkdir /homework
[root@localhost 桌面]# chmod -R o+w /homework/
[root@localhost 桌面]# echo "1111" > /homework/1.txt
[root@localhost 桌面]# echo "2222" > /homework/2.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt
[root@localhost 桌面]# vim /etc/rsyncd.conf
uid=rsync
gid=rsync
max connections=200
timeout=300
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
[homework]
comment this is student homework
path=/homework
read only=no
auth users=student
secrets file=/etc/password.txt
list=false
hosts allow=192.168.4.0/24
hosts deny=10.10.10.0/24
wq
[root@localhost 桌面]# vim /etc/password.txt
student:456
[root@localhost 桌面]# chmod 600 /etc/password.txt
[root@localhost 桌面]# useradd -s /sbin/nologin rsync
[root@localhost 桌面]# chown -R rsync.rsync /homework/
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# rsync –daemon
[root@localhost 桌面]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local //设置开启自启服务
[root@localhost 桌面]# netstat -anptu |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 27928/rsync
tcp 0 0 :::873 :::* LISTEN 27928/rsync
[root@localhost 桌面]# rsync -av student@127.0.0.1::
空 //查看不到,但能下载
[root@localhost 桌面]# rsync -av student@127.0.0.1::homework /data/ //服务器端下载到本地,测试成功
Password: 输入student的密码:456
receiving incremental file list
./
1.txt
2.txt
sent 102 bytes received 227 bytes 73.11 bytes/sec
total size is 10 speedup is 0.03
[root@localhost 桌面]# ls /data/
1.txt 2.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt //客户端上传前
[root@localhost 桌面]# ls /homework/
1.txt 2.txt a.txt b.txt //客户端上传后
客户端下载或上传服务器上的共享文件:(192.168.4.205)
查看服务器上的共享文件:
[root@localhost 桌面]# rsync -av 192.168.4.5::
空 //查看不到
[root@localhost 桌面]# mkdir /beifen
[root@localhost 桌面]# rsync -av student@192.168.4.5::homework /beifen/ //远程下载
Password: 输入对方设置的student的密码:456
receiving incremental file list
./
1.txt
2.txt
sent 102 bytes received 227 bytes 43.87 bytes/sec
total size is 10 speedup is 0.03
[root@localhost 桌面]# ls /beifen/
1.txt 2.txt
[root@localhost 桌面]# mkdir /a
[root@localhost 桌面]# echo "aaaa" > /a/a.txt
[root@localhost 桌面]# echo "bbbb" > /a/b.txt
[root@localhost 桌面]# ls /a/
a.txt b.txt
[root@localhost 桌面]# rsync -av /a/ student@192.168.4.5::homework //远程上传
Password: 输入对方student密码:456
sending incremental file list
./
a.txt
b.txt
sent 149 bytes received 49 bytes 56.57 bytes/sec
total size is 10 speedup is 0.05
注意:下载和上传不需要输入对方密码的设置(在客户端做如下设置即可)
[root@localhost 桌面]# echo "456" > /etc/abc.passwd //客户端先将服务器端给的密码写入一个文件
[root@localhost 桌面]# chmod 600 /etc/abc.passwd //必须要有这一步骤,否则还需要输入密码
rsync -av student@192.168.4.5::homework /beifen/ --password-file=/etc/abc.passwd 不输密码,即可下载
rsync -av /a/ student@192.168.4.5::homework --password-file=/etc/abc.passwd 不输密码,即可上传
rsync下载和上传时候排除不需要同步的内容(客户端设置排除)
案例5: 服务端和客户端都是用root账户登陆时候的rsync数据同步。(另一种启动方式:rsync --daemon)
下载和上传时候排除不需要同步的内容(客户端设置排除单个)
在rsync服务器上配置:(192.168.4.5)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1809) 正在运行...
[root@localhost 桌面]# mkdir /homework
[root@localhost 桌面]# chmod -R o+w /homework/
[root@localhost 桌面]# echo "1111" > /homework/1.txt
[root@localhost 桌面]# echo "2222" > /homework/2.txt
[root@localhost 桌面]# echo "3333" > /homework/3.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt
[root@localhost 桌面]# vim /etc/rsyncd.conf
uid=rsync
gid=rsync
max connections=200
timeout=300
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
[homework]
comment this is student homework
path=/homework
read only=no
auth users=student
secrets file=/etc/password.txt
list=false
hosts allow=192.168.4.0/24
hosts deny=10.10.10.0/24
wq
[root@localhost 桌面]# vim /etc/password.txt
student:456
[root@localhost 桌面]# chmod 600 /etc/password.txt
[root@localhost 桌面]# useradd -s /sbin/nologin rsync
[root@localhost 桌面]# chown -R rsync.rsync /homework/
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# rsync –daemon
[root@localhost 桌面]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local //设置开启自启服务
[root@localhost 桌面]# netstat -anptu |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 27928/rsync
tcp 0 0 :::873 :::* LISTEN 27928/rsync
[root@localhost 桌面]# rsync -av student@127.0.0.1::
空 //查看不到,但能下载
[root@localhost 桌面]# rsync -av student@127.0.0.1::homework /data/ //服务器端下载到本地,测试成功
Password: 输入student的密码:456
receiving incremental file list
./
1.txt
2.txt
3.txt
sent 121 bytes received 303 bytes 169.60 bytes/sec
total size is 15 speedup is 0.04
[root@localhost 桌面]# ls /data/
1.txt 2.txt 3.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt //客户端上传前
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt b.txt c.txt //客户端上传后
客户端下载或上传服务器上的共享文件:(192.168.4.205)
查看服务器上的共享文件:
[root@localhost 桌面]# rsync -av 192.168.4.5::
空 //查看不到
[root@localhost 桌面]# mkdir /beifen
[root@localhost 桌面]# rsync -av --exclude=1.txt student@192.168.4.5::homework /beifen/下载
Password: 输入对方设置的student的密码:456
receiving incremental file list
./
2.txt
3.txt
sent 113 bytes received 233 bytes 98.86 bytes/sec
total size is 10 speedup is 0.03
[root@localhost 桌面]# ls /beifen/
2.txt 3.txt
[root@localhost 桌面]# mkdir /a
[root@localhost 桌面]# echo "aaaa" > /a/a.txt
[root@localhost 桌面]# echo "bbbb" > /a/b.txt
[root@localhost 桌面]# echo "cccc" > /a/c.txt
[root@localhost 桌面]# ls /a/
a.txt b.txt c.txt
[root@localhost 桌面]# rsync -av --exclude=a.txt /a/ student@192.168.4.5::homework //上传
Password:
sending incremental file list
./
b.txt
c.txt
sent 153 bytes received 49 bytes 57.71 bytes/sec
total size is 10 speedup is 0.05
注意:下载和上传不需要输入对方密码的设置(在客户端做如下设置即可)
[root@localhost 桌面]# echo "456" > /etc/abc.passwd //客户端先将服务器端给的密码写入一个文件
[root@localhost 桌面]# chmod 600 /etc/abc.passwd //必须要有这一步骤,否则还需要输入密码
rsync -av --exclude=1.txt student@192.168.4.5::homework /beifen/ --password-file=/etc/abc.passwd
不输密码,即可下载
rsync -av --exclude=a.txt /a/ student@192.168.4.5::homework --password-file=/etc/abc.passwd
不输密码,即可上传
案例6: 服务端和客户端都是用root账户登陆时候的rsync数据同步。(另一种启动方式:rsync --daemon)
下载和上传时候排除不需要同步的内容(客户端设置排除多个)
在rsync服务器上配置:(192.168.4.5)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1809) 正在运行...
[root@localhost 桌面]# mkdir /homework
[root@localhost 桌面]# chmod -R o+w /homework/
[root@localhost 桌面]# echo "1111" > /homework/1.txt
[root@localhost 桌面]# echo "2222" > /homework/2.txt
[root@localhost 桌面]# echo "3333" > /homework/3.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt
[root@localhost 桌面]# vim /etc/rsyncd.conf
uid=rsync
gid=rsync
max connections=200
timeout=300
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
[homework]
comment this is student homework
path=/homework
read only=no
auth users=student
secrets file=/etc/password.txt
list=false
hosts allow=192.168.4.0/24
hosts deny=10.10.10.0/24
wq
[root@localhost 桌面]# vim /etc/password.txt
student:456
[root@localhost 桌面]# chmod 600 /etc/password.txt
[root@localhost 桌面]# useradd -s /sbin/nologin rsync
[root@localhost 桌面]# chown -R rsync.rsync /homework/
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# rsync –daemon
[root@localhost 桌面]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local //设置开启自启服务
[root@localhost 桌面]# netstat -anptu |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 27928/rsync
tcp 0 0 :::873 :::* LISTEN 27928/rsync
[root@localhost 桌面]# rsync -av student@127.0.0.1::
空 //查看不到,但能下载
[root@localhost 桌面]# rsync -av student@127.0.0.1::homework /data/ //服务器端下载到本地,测试成功
Password: 输入student的密码:456
receiving incremental file list
./
1.txt
2.txt
3.txt
sent 121 bytes received 303 bytes 169.60 bytes/sec
total size is 15 speedup is 0.04
[root@localhost 桌面]# ls /data/
1.txt 2.txt 3.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt //客户端上传前
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt c.txt //客户端上传后
客户端下载或上传服务器上的共享文件:(192.168.4.205)
查看服务器上的共享文件:
[root@localhost 桌面]# rsync -av 192.168.4.5::
空 //查看不到
[root@localhost 桌面]# mkdir /beifen
[root@localhost 桌面]# vim /root/download_pc.log
1.txt
2.txt
wq
[root@localhost 桌面]# vim /root/upload_pc.log
a.txt
b.txt
wq
[root@localhost 桌面]# rsync -av --exclude-from=/root/download_pc.log student@192.168.4.5::homework /beifen/
Password: 输入student密码:456
receiving incremental file list
./
3.txt
sent 105 bytes received 167 bytes 108.80 bytes/sec
total size is 5 speedup is 0.02
[root@localhost 桌面]# ls /beifen/
3.txt
[root@localhost 桌面]# mkdir /a
[root@localhost 桌面]# echo "aaaa" > /a/a.txt
[root@localhost 桌面]# echo "bbbb" > /a/b.txt
[root@localhost 桌面]# echo "cccc" > /a/c.txt
[root@localhost 桌面]# ls /a/
a.txt b.txt c.txt
[root@localhost 桌面]# rsync -av --exclude-from=/root/upload_pc.log /a/ student@192.168.4.5::homework
Password: 输入student密码:456
sending incremental file list
./
c.txt
sent 87 bytes received 30 bytes 33.43 bytes/sec
total size is 5 speedup is 0.04
注意:
1)下载和上传不需要输入对方密码的设置(在客户端做如下设置即可)
[root@localhost 桌面]# echo "456" > /etc/abc.passwd //客户端先将服务器端给的密码写入一个文件
[root@localhost 桌面]# chmod 600 /etc/abc.passwd //必须要有这一步骤,否则还需要输入密码
rsync -av --exclude-from=/root/download_pc.log student@192.168.4.5::homework /beifen/
--password-file=/etc/abc.passwd 不输入密码,即可下载
rsync -av --exclude-from=/root/upload_pc.log /a/ student@192.168.4.5::homework \
--password-file=/etc/abc.passwd 不输入密码,即可上传
2)排除多个还可用下面方法,和上面效果一一样。
下载排除多个: rsync -av --exclude={1.txt,2.txt} student@192.168.4.5::homework /beifen/
上传排除多个:rsync -av --exclude={a.txt,b.txt} /a/ student@192.168.4.5::homework
rsync下载和上传时候排除不需要同步的内容(服务端设置排除)
案例7: 服务端和客户端都是用root账户登陆时候的rsync数据同步。(另一种启动方式:rsync --daemon)
下载和上传时候排除不需要同步的内容(服务端设置排除多个)
在rsync服务器上配置:(192.168.4.5)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1809) 正在运行...
[root@localhost 桌面]# mkdir /homework
[root@localhost 桌面]# chmod -R o+w /homework/
[root@localhost 桌面]# echo "1111" > /homework/1.txt
[root@localhost 桌面]# echo "2222" > /homework/2.txt
[root@localhost 桌面]# echo "3333" > /homework/3.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt
[root@localhost 桌面]# vim /etc/rsyncd.conf
uid=rsync
gid=rsync
max connections=200
timeout=300
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
[homework]
comment this is student homework
path=/homework
read only=no
auth users=student
secrets file=/etc/password.txt
list=false
hosts allow=192.168.4.0/24
hosts deny=10.10.10.0/24
exclude=1.txt 2.txt a.txt b.txt //多个排除的用空格隔开
wq
[root@localhost 桌面]# vim /etc/password.txt
student:456
[root@localhost 桌面]# chmod 600 /etc/password.txt
[root@localhost 桌面]# useradd -s /sbin/nologin rsync
[root@localhost 桌面]# chown -R rsync.rsync /homework/
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# rsync –daemon
[root@localhost 桌面]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local //设置开启自启服务
[root@localhost 桌面]# netstat -anptu |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 27928/rsync
tcp 0 0 :::873 :::* LISTEN 27928/rsync
[root@localhost 桌面]# rsync -av student@127.0.0.1::
空 //查看不到,但能下载
[root@localhost 桌面]# rsync -av student@127.0.0.1::homework /data/ //服务器端下载到本地,测试成功
Password: 输入student的密码:456
receiving incremental file list
./
1.txt
2.txt
3.txt
sent 121 bytes received 303 bytes 169.60 bytes/sec
total size is 15 speedup is 0.04
[root@localhost 桌面]# ls /data/
1.txt 2.txt 3.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt //客户端上传前
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt c.txt //客户端上传后
客户端下载或上传服务器上的共享文件:(192.168.4.205)
查看服务器上的共享文件:
[root@localhost 桌面]# rsync -av 192.168.4.5::
空 //查看不到
[root@localhost 桌面]# mkdir /beifen
[root@localhost 桌面]# rsync -av student@192.168.4.5::homework /beifen/ //下载
Password: 输入student密码:456
receiving incremental file list
./
3.txt
sent 83 bytes received 167 bytes 55.56 bytes/sec
total size is 5 speedup is 0.02
[root@localhost 桌面]# ls /beifen/
3.txt
[root@localhost 桌面]# mkdir /a
[root@localhost 桌面]# echo "aaaa" > /a/a.txt
[root@localhost 桌面]# echo "bbbb" > /a/b.txt
[root@localhost 桌面]# echo "cccc" > /a/c.txt
[root@localhost 桌面]# ls /a/
a.txt b.txt c.txt
[root@localhost 桌面]# rsync -av /a/ student@192.168.4.5::homework //上传
Password: 输入student密码:456
sending incremental file list
./
skipping daemon-excluded file "a.txt"
skipping daemon-excluded file "b.txt"
c.txt
sent 119 bytes received 30 bytes 59.60 bytes/sec
total size is 15 speedup is 0.10
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
注意:
1)下载和上传不需要输入对方密码的设置(在客户端做如下设置即可)
[root@localhost 桌面]# echo "456" > /etc/abc.passwd //客户端先将服务器端给的密码写入一个文件
[root@localhost 桌面]# chmod 600 /etc/abc.passwd //必须要有这一步骤,否则还需要输入密码
rsync -av student@192.168.4.5::homework /beifen/ /beifen/--password-file=/etc/abc.passwd
不输入密码,即可下载
rsync -av /a/ student@192.168.4.5::homework --password-file=/etc/abc.passwd
不输入密码,即可上传
rsync无差异同步(有数据丢失风险)
案例8: 服务端和客户端都是用root账户登陆时候的rsync数据同步。(另一种启动方式:rsync --daemon)
原先客、服文件一致,当服务器端数据删除,客户端也跟着删除,服务端增加,客户端也跟着增加,客户端根据服务器数据进行同步。
在rsync服务器上配置:(192.168.4.5)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1809) 正在运行...
[root@localhost 桌面]# mkdir /homework
[root@localhost 桌面]# chmod -R o+w /homework/
[root@localhost 桌面]# echo "1111" > /homework/1.txt
[root@localhost 桌面]# echo "2222" > /homework/2.txt
[root@localhost 桌面]# echo "3333" > /homework/3.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt
[root@localhost 桌面]# vim /etc/rsyncd.conf
uid=rsync
gid=rsync
max connections=200
timeout=300
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
[homework]
comment this is student homework
path=/homework
read only=no
auth users=student
secrets file=/etc/password.txt
list=false
hosts allow=192.168.4.0/24
hosts deny=10.10.10.0/24
wq
[root@localhost 桌面]# vim /etc/password.txt
student:456
[root@localhost 桌面]# chmod 600 /etc/password.txt
[root@localhost 桌面]# useradd -s /sbin/nologin rsync
[root@localhost 桌面]# chown -R rsync.rsync /homework/
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# rsync –daemon
[root@localhost 桌面]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local //设置开启自启服务
[root@localhost 桌面]# netstat -anptu |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 27928/rsync
tcp 0 0 :::873 :::* LISTEN 27928/rsync
[root@localhost 桌面]# rsync -av student@127.0.0.1::
空 //查看不到,但能下载
[root@localhost 桌面]# rsync -av student@127.0.0.1::homework /data/ //服务器端下载到本地,测试成功
Password: 输入student的密码:456
receiving incremental file list
./
1.txt
2.txt
3.txt
sent 121 bytes received 303 bytes 169.60 bytes/sec
total size is 15 speedup is 0.04
[root@localhost 桌面]# ls /data/
1.txt 2.txt 3.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt
客户端下载或上传服务器上的共享文件:(192.168.4.205)
查看服务器上的共享文件:
[root@localhost 桌面]# rsync -av 192.168.4.5::
空 //查看不到
[root@localhost 桌面]# mkdir /beifen
[root@localhost 桌面]# rsync -av student@192.168.4.5::homework /beifen/ //下载
Password: 输入对方设置的student的密码:456
receiving incremental file list
./
1.txt
2.txt
3.txt
sent 121 bytes received 297 bytes 119.43 bytes/sec
total size is 15 speedup is 0.04
[root@localhost 桌面]# ls /beifen/
1.txt 2.txt 3.txt
[root@localhost 桌面]# mkdir /a
[root@localhost 桌面]# echo "aaaa" > /a/a.txt
[root@localhost 桌面]# echo "bbbb" > /a/b.txt
[root@localhost 桌面]# echo "cccc" > /a/c.txt
[root@localhost 桌面]# ls /a/
a.txt b.txt c.txt
在rsync服务器上:(192.168.4.5)删除一部分数据
[root@localhost 桌面]# rm -f /homework/1.txt
[root@localhost 桌面]# ls /homework/
2.txt 3.txt
客户端下载或上传服务器上的共享文件:(192.168.4.205) 跟着删除一部分数据
[root@localhost 桌面]# ls /beifen/
1.txt 2.txt 3.txt
[root@localhost 桌面]# rsync -av --delete student@192.168.4.5::homework /beifen/ //下载
Password: 输入对方设置的student的密码:456
receiving incremental file list
deleting 1.txt
./
sent 64 bytes received 135 bytes 4.37 bytes/sec
total size is 10 speedup is 0.05
[root@localhost 桌面]# ls /beifen/
2.txt 3.txt
在rsync服务器上:(192.168.4.5)增加一部分数据
[root@localhost 桌面]# echo "1111" > /homework/1.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt
客户端下载或上传服务器上的共享文件:(192.168.4.205) 跟着增加一部分数据
[root@localhost 桌面]# ls /beifen/
2.txt 3.txt
[root@localhost 桌面]# rsync -av --delete student@192.168.4.5::homework /beifen/
Password:
receiving incremental file list
./
1.txt
sent 83 bytes received 199 bytes 80.57 bytes/sec
total size is 15 speedup is 0.05
[root@localhost 桌面]# ls /beifen/
1.txt 2.txt 3.txt
注意:下载和上传不需要输入对方密码的设置(在客户端做如下设置即可)
[root@localhost 桌面]# echo "456" > /etc/abc.passwd //客户端先将服务器端给的密码写入一个文件
[root@localhost 桌面]# chmod 600 /etc/abc.passwd //必须要有这一步骤,否则还需要输入密码
rsync -av --delete student@192.168.4.5::homework /beifen/ --password-file=/etc/abc.passwd
不输密码,即可下载
rsync无差异同步(有数据丢失风险)
案例9: 服务端和客户端都是用root账户登陆时候的rsync数据同步。(另一种启动方式:rsync --daemon)
原先客、服文件一致,当客户端数据删除,服务端也跟着删除,客户端增加数据,服务端也跟着增加数据,服务端根据客户端数据进行同步。
在rsync服务器上配置:(192.168.4.5)
[root@localhost 桌面]# /etc/init.d/sshd status
openssh-daemon (pid 1809) 正在运行...
[root@localhost 桌面]# mkdir /homework
[root@localhost 桌面]# chmod -R o+w /homework/
[root@localhost 桌面]# echo "1111" > /homework/1.txt
[root@localhost 桌面]# echo "2222" > /homework/2.txt
[root@localhost 桌面]# echo "3333" > /homework/3.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt
[root@localhost 桌面]# vim /etc/rsyncd.conf
uid=rsync
gid=rsync
max connections=200
timeout=300
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
[homework]
comment this is student homework
path=/homework
read only=no
auth users=student
secrets file=/etc/password.txt
list=false
hosts allow=192.168.4.0/24
hosts deny=10.10.10.0/24
wq
[root@localhost 桌面]# vim /etc/password.txt
student:456
[root@localhost 桌面]# chmod 600 /etc/password.txt
[root@localhost 桌面]# useradd -s /sbin/nologin rsync
[root@localhost 桌面]# chown -R rsync.rsync /homework/
[root@localhost 桌面]# yum -y install rsync
[root@localhost 桌面]# rsync –daemon
[root@localhost 桌面]# echo "/usr/bin/rsync --daemon" >> /etc/rc.local //设置开启自启服务
[root@localhost 桌面]# netstat -anptu |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 27928/rsync
tcp 0 0 :::873 :::* LISTEN 27928/rsync
[root@localhost 桌面]# rsync -av student@127.0.0.1::
空 //查看不到,但能下载
[root@localhost 桌面]# rsync -av student@127.0.0.1::homework /data/ //服务器端下载到本地,测试成功
Password: 输入student的密码:456
receiving incremental file list
./
1.txt
2.txt
3.txt
sent 121 bytes received 303 bytes 169.60 bytes/sec
total size is 15 speedup is 0.04
[root@localhost 桌面]# ls /data/
1.txt 2.txt 3.txt
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt
客户端下载或上传服务器上的共享文件:(192.168.4.205)
查看服务器上的共享文件:
[root@localhost 桌面]# rsync -av 192.168.4.5::
空 //查看不到
[root@localhost 桌面]# mkdir /beifen
[root@localhost 桌面]# rsync -av student@192.168.4.5::homework /beifen/ //下载
Password: 输入对方设置的student的密码:456
receiving incremental file list
./
1.txt
2.txt
3.txt
sent 121 bytes received 297 bytes 119.43 bytes/sec
total size is 15 speedup is 0.04
[root@localhost 桌面]# ls /beifen/
1.txt 2.txt 3.txt
在rsync客户端上:(192.168.4.205) 客户端删除一部分数据
[root@localhost 桌面]# rm -f /beifen/3.txt
[root@localhost 桌面]# ls /beifen/
1.txt 2.txt
[root@localhost 桌面]# rsync -av --delete /beifen/ student@192.168.4.5::homework
Password: 输入对方设置的student的密码:456
sending incremental file list
./
deleting 3.txt
sent 69 bytes received 11 bytes 32.00 bytes/sec
total size is 10 speedup is 0.12
在rsync服务器上:(192.168.4.5) 服务器也跟着删除一部分数据
[root@localhost 桌面]# ls /homework/
1.txt 2.txt 3.txt //客户端删除一部分数据,再实行同步前
[root@localhost 桌面]# ls /homework/ //客户端删除一部分数据,再实行同步后
1.txt 2.txt
在rsync客户端上:(192.168.4.205)增加一部分数据,并实行与服务器端同步
[root@localhost 桌面]# echo "3333" > /beifen/3.txt
[root@localhost 桌面]# ls /beifen/
1.txt 2.txt 3.txt
[root@localhost 桌面]# rsync -av --delete /beifen/ student@192.168.4.5::homework
Password: 输入对方设置的student的密码:456
sending incremental file list
./
3.txt
sent 174 bytes received 55 bytes 24.11 bytes/sec
total size is 15 speedup is 0.07
在rsync服务器上:(192.168.4.5) 服务器也跟着增加一部分数据
[root@localhost 桌面]# ls /homework/
1.txt 2.txt //客户端增加一部分数据,再实行同步前
[root@localhost 桌面]# ls /homework/ //客户端删除一部分数据,再实行同步后
1.txt 2.txt 3.txt
注意:下载和上传不需要输入对方密码的设置(在客户端做如下设置即可)
[root@localhost 桌面]# echo "456" > /etc/abc.passwd //客户端先将服务器端给的密码写入一个文件
[root@localhost 桌面]# chmod 600 /etc/abc.passwd //必须要有这一步骤,否则还需要输入密码
rsync -av --delete /beifen/ student@192.168.4.5::homework /beifen/ --password-file=/etc/abc.passwd
不输密码,即可上传同步
如果对运维课程感兴趣,可以在b站上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频