Linux rsync同步

版权声明:QQ:1009002494 https://blog.csdn.net/Doudou_Mylove/article/details/83148670

Linux下数据备份工具rsync

yum install openssh-clients
scp 1.txt /tmp                    拷贝本地的数据,这和cp一样
scp [-r] /dir/ user@ip:/dir/                拷贝到远程,也类似于cp
scp -P 59878 20171127.sql [email protected]:/tmp/    不同端口服务器传输,59878这里是对方的机器端口

rsync的优点在于,可以增量备份,就是说目标机器上已经存在某个文件,且和源文件一样,那它不会覆盖该文件
rsync不仅可以像cp 一样在本地同步数据,还可以同步到远程
没有rsync命令,使用命令yum -y install rsync安装
rsync -av 123.txt /tmp/
rsync -av 123.txt [email protected]:/tmp/            其中root@可以省略掉,回车后会提示让输入目标机器的root密码

scp [email protected]:/root/myfile/1.txt ./1.txt        远程复制文件,把10.10.10.20下的文件复制到本机
scp ./1.txt [email protected]:/root/myfile/1.txt        也可以把本地文件传到远程机器下

rsync -av [email protected]:/root/myfile/1.txt ./1.txt
rsync -av ./1.txt [email protected]:/root/myfile/1.txt

注意:
Rsync 参数选项说明

-v, --verbose 详细模式输出
-q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-R, --relative 使用相对路径信息
--exclude=PATTERN指定排除不需要传输的文件,等号后面跟文件名,可以是万用字符模式(如*.txt)
--delete 删除那些DST中SRC没有的文件
最常用的 -a -v --delete --exclude

rsync -av dir1/ dir2/    其中dir2/目录可以不存在,记得同步目录是一定要在末尾加上/
-a 会把软连接原原本本的拷贝过去,如果拷贝源文件就用-L    rsync -avL test1/ test2/
-u选项的作用是,如果目标文件比源文件新,那么会忽略掉该文件

rsync -avuL --exclude="*.txt" --exclude="2.txt" shell/ shell_1/        可以排除多个选项


rsync应用实例——ssh方式
rsync -avL ip:/tmp/test2/ ./t到脚本中,但可以
由于需要输入密码所以不适合写写成这样的方式:r通过创建密钥对,让两台机器产生信任关系从而不用输入密码
如果ssh端口不是22,那么需要est3/sync -av "--rsh=ssh -p port" /dir1/ ip:/tmp/dir2/


rsync应用实例-后台服务方式

首先touch /etc/rsyncd.conf,因为系统没有这个文件,需要手动创建
vim /etc/rsyncd.conf

配置文件/etc/rsyncd.conf,内容如下:
#port=873            #监听端口默认为873,也可以是别的端口
log file=/var/log/rsync.log    #指定日志
pid file=/var/log/rsyncd.pid    #指定pid
#address=10.10.10.20        #可以定义绑定的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=10.10.10.20        #设置可以允许访问的主机,可以是网段

[test2]
path=/root/abc
use chroot=no
max connections=4
read only=no
list=true
uid=root
gid=root
auth users=test
secrets file=/etc/rsyncd.passwd
hosts allow=10.10.10.10

配置完成后启动rsync服务用rsync --daemon命令,查看是否启动成功ps aux |grep raync查看端口是否正确netstat -nlp |grep rsync
注意:chmod 400 /etc/rsyncd.passwd要给予密码文件权限
密码文件/etc/rsyncd.passwd的内容格式为:username:passwd
默认去使用/etc/rsyncd.conf这个配置文件,也可以指定配置文件rsync --daemon --config=/etc/rsyncd2.conf
几个测试点:port,use chroot,log file,secrets file,hosts allow,list


学会使用curl命令
curl是Linux系统命令行下用来简单测试web访问的工具,几个常用的选项如下:
curl -xip:port www.baidu.com             -x可以指定ip和端口,省略写hosts,
curl -lv http://www.qq.com            -I可以把访问的内容略掉,只显示状态码,-v可以显示详细过程
curl -u user:password http://123.com        -u可以指定用户名和密码
curl http://study.lijunmin.net/index.html -O    可以下载,还可以使用-o自定义名字curl -o index2.html http://study.lijunmin.net/index.html


与网络相关的命令
ping                    linux会一直ping,windows只ping4个包,Linux 可用参数-c n指定ping n次,例如ping -c 3 10.10.10.20
telnet www.baidu.com 80
traceroute www.baidu.com
dig @8.8.8.8 study.lishiming.net        查看域名解析跟DNS有关
nc -z -w2 www.baidu.com 1-1024        -w2表示2s超时,port这里可以只写一个端口,也可以写一个范围。
使用nc扫描端口时,必须要加-z否则不显示结果,另外,如果想把不开放的端口也显示出来,可以加一个-v


查看是否安装软件包yum list |grep wget

如果出现一下错误:
[root@localhost ~]# rsync -avrl [email protected]:/data /
rsync: Failed to exec ssh: No such file or directory (2)
rsync error: error in IPC code (code 14) at pipe.c(84) [receiver=3.0.6]
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [receiver=3.0.6]

使用yum install openssh-clients  安装命令 可以解决上述的问题。

也可以使用which ssh 检查此命令 ,没有的进行安装即可。否则会出现这样或那样的问题。

在最小化安装6.7系统后没有发现ssh,后期要注意了。

猜你喜欢

转载自blog.csdn.net/Doudou_Mylove/article/details/83148670