Operation and maintenance
  • 目录
  • linux 历史
    • Linux网卡配置
    • linux互相登陆
  • 文件目录管理
    • linux目录结构
    • 文件的基本信息
    • linux别名alias
    • 环境变量
    • linux终端相关快捷键
    • linux文件和目录添加删除
    • linux文件查看命令
    • linux文件目录权限
    • linux文件特殊权限
    • 链接文件
    • linux下文件查找
    • linux和window文件互传
    • linux文件压缩和打包
    • vim工具集
  • 用户和组管理
    • 用户组和用户管理
    • 修改组和用户
    • 用户切换
    • 禁止root远程登陆
  • linux磁盘管理
    • 磁盘分区
    • 格式化磁盘
    • 分区挂载
    • 手动添加swap分区
    • lvm基础
  • linux软件安装和卸载
    • rpm工具
    • yum工具
    • 源码安装包
  • 日常运维
    • 监控磁盘IO
    • 查看网络状态
    • linux抓包
    • linux网络相关
    • Linux防火墙-netfilter
    • linux任务计划
    • linux系统服务管理
    • linux同步工具rsync
    • linux系统日志和screen
  • LAMP架构
    • 安装mysql
    • 免编译安装mariadb
    • 安装apache
    • 安装php5
    • 安装php7
    • apache 配置虚拟主机
    • apache 用户认证
    • apache 域名跳转
    • apache 访问日志
    • apache 不记录文件日志
    • apache 配置静态元素过期时间
    • apache 配置防盗链
    • apache 访问控制
    • apache 禁止解析php文件
    • apache 禁止user_agent
    • php 配置文件
    • php 动态扩展模块安装
  • LNMP架构
    • 安装mariadb
    • 安装php
    • 安装nginx
    • nginx 默认虚拟主机
    • nginx 用户认证
    • nginx 域名重定向
    • nginx 访问日志
    • nginx 日志切割
    • nginx不记录指定文件类型日志
    • nginx 静态文件和过期时间
    • nginx 防盗链
    • nginx 访问控制
    • nginx 解析php
    • nginx 代理
    • nginx 负载均衡
    • nginx 支持https
  • MySQL集合
    • 更改root密码
    • mysql 常用操作
    • mysql 常用sql
    • mysql 数据备份
    • mysql 主从配置
  • linux常用服务
    • nfs 服务搭建
    • CentOS7 搭建snmp服务
    • ftp 服务搭建
    • samba 服务搭建
    • Squid 服务搭建
    • Tomcat 服务搭建
    • resin 搭建
    • dns 服务搭建
    • iscsi 服务搭建
  • linux开发语言环境
    • python 开发环境
    • golang 开发环境
  • linux集群架构
    • linux 高可用
    • linux 负载均衡
  • linux监控平台
    • zabbix 监控平台(上)
  • shell集合
  • NoSql
    • memcached (上)
    • memcached(下)
    • redis(上)
    • redis(中)
    • redis(下)
    • mongodb(上)
    • mongodb(中)
    • mongodb(下)
  • 代码管理工具
    • 代码管理工具 svn
    • 代码管理工具 git
    • 搭建git私有仓库
    • 安装配置gitlab
  • docker 入门
    • docker 安装
    • docker 镜像管理
    • docker 容器管理
    • docker 仓库管理
    • docker 共享目录
    • docker 数据备份和恢复
    • docker 网络管理
    • docker 使用dockerfile
    • docker compose
  • 自动化运维
    • saltstack 安装
    • saltstack 配置认证
    • saltstack 远程执行命令
    • saltstack grains
    • saltstack pillar
    • saltstack 实战:安装配置httpd
    • saltstack 实战:配置管理文件
    • saltstack 实战:配置管理远程命令
    • saltstack 实战:配置管理任务计划
    • saltstack salt-ssh工具
    • ansible 安装
    • ansible 远程命令
    • ansible 远程目录拷贝
    • ansible 任务计划
    • ansible 远程rpm/服务管理
    • ansible playbook 详解
    • ansible playbook 实战:安装nginx
  • linux虚拟化
    • KVM介绍
    • 安装KVM
    • 虚拟机管理
    • 克隆虚拟机
    • 快照管理
    • 磁盘扩容
    • 调整虚拟机配置
    • 迁移虚拟机
  • 错误排查
    • linux启动挂在不上sysroot问题
由 GitBook 提供支持
在本页
  • rsync使用
  • rsync 以ssh的方式同步数据
  • rsync 服务方式同步

这有帮助吗?

  1. 日常运维

linux同步工具rsync

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。

rsync使用

rsync 命令:

介绍:linux中的文件同步工具,yum install -y rsync

格式:rsync [option] [src] [user@ip:][dest]

选项:

  • -a 包含-rtplgoD

  • -r 同步目录时要加上,类似cp时的-r选项

  • -v 同步时显示一些信息,让我们知道同步的过程

  • -l 保留软连接 -L 加上该选项后,同步软链接时会把源文件给同步

  • -p 保持文件的权限属性

  • -o 保持文件的属主

  • -g 保持文件的属组

  • -D 保持设备文件信息

  • -t 保持文件的时间属性

  • --delete 删除DEST中SRC没有的文件

  • --exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步

  • -P 显示同步过程,比如速率,比-v更加详细

  • -u 加上该选项后,如果DEST中的文件比SRC新,则不同步

  • -z 传输时压缩

用法:

  • rsync -av /etc/passwd /tmp/1.txt :本地同步

  • rsync -av /tmp/1.txt 192.168.188.128:/tmp/2.txt :远程同步 (远程的机器上也要安装rsync)

  • rsync -av /root/ /tmp/root/ :同步目录,注意源目录和目标目录尾加 /

[root@localhost ~]# rsync -av /etc/passwd /tmp/passwd
sending incremental file list
passwd

sent 1,102 bytes  received 35 bytes  2,274.00 bytes/sec
total size is 1,010  speedup is 0.89
[root@localhost ~]# rsync -av /etc/passwd root@192.168.127.128:/tmp/passwd
sending incremental file list

sent 45 bytes  received 12 bytes  114.00 bytes/sec
total size is 1,010  speedup is 17.72
[root@localhost ~]# ls /tmp/passwd 
/tmp/passwd
[root@localhost ~]# rsync -av /root/ /tmp/root/

rsync 以ssh的方式同步数据

  • rsync -av test1/ 192.168.127.129:/tmp/test2/

    :同步目录

  • rsync -av -e "ssh -p 22" test1/ 192.168.127.129:/tmp/test2/ :ssh 方式同步

  • rsync -av -e "ssh -p 22" 192.168.127.129:/tmp/test2/

    test1/ :从远程机器下载

rsync 服务方式同步

rsync 也可以使用服务方式来同步数据,但需要一些配置。

1.修改配置文件:/etc/rsyncd.conf

[root@localhost ~]# vim /etc/rsyncd.conf 
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.127.128
[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.127.129 192.168.127.0/24

rsyncd.conf配置文件详解

  • port:指定在哪个端口启动rsyncd服务,默认是873端口。

  • log file:指定日志文件。

  • pid file:指定pid文件,这个文件的作用涉及服务的启动、停止等进程管理操作。

  • address:指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动。 []:指定模块名,里面内容自定义。

  • path:指定数据存放的路径。

  • use chroot true|false:表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,阿铭建议你设置成false。

  • max connections:指定最大的连接数,默认是0,即没有限制。

  • read only ture|false:如果为true,则不能上传到该模块指定的路径下。

  • list:表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏。

  • uid/gid:指定传输文件时以哪个用户/组的身份传输。

  • auth users:指定传输时要使用的用户名。

  • secrets file:指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意该密码文件的权限一定要是600。格式:用户名:密码

  • hosts allow:表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开。

当设置了auth users和secrets file后,客户端连服务端也需要用用户名密码了,若想在命令行中带上密码,可以设定一个密码文件 rsync -avL test@192.168.133.130::test/test1/ /tmp/test8/ --password-file=/etc/pass 其中/etc/pass内容就是一个密码,权限要改为600

2.创建同步的目录

[root@localhost ~]# mkdir /tmp/rsync/
[root@localhost ~]# chmod 755 -R !$
chmod 755 -R /tmp/rsync/
[root@localhost ~]# ls -ld /tmp/rsync
drwxr-xr-x 2 root root 6 8月   8 23:03 /tmp/rsync

3.启动服务:rsync --daemon

[root@localhost ~]# rsync --daemon
[root@localhost ~]# ps aux|grep "rsync"
root       1684  0.0  0.0 114740   564 ?        Ss   23:04   0:00 rsync --daemon
root       1686  0.0  0.0 112720   984 pts/1    R+   23:04   0:00 grep --color=auto rsync
[root@localhost ~]# netstat -tnlp|grep "rsync"
tcp        0      0 192.168.127.128:873     0.0.0.0:*               LISTEN      1684/rsync

4.测试:在客户端192.168.127.129

## 客户端 192.168.127.129
[root@localhost ~]# mkdir /tmp/rsync
[root@localhost ~]# echo "test" >> /tmp/rsync/test
[root@localhost ~]# ls /tmp/rsync/
test
[root@localhost ~]# rsync -avP /tmp/rsync/ 192.168.127.128::test/
sending incremental file list
./
test
              5 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=0/2)

sent 112 bytes  received 38 bytes  14.29 bytes/sec
total size is 5  speedup is 0.03

## 服务端 192.168.127.128
[root@localhost ~]# ls /tmp/rsync/
test
上一页linux系统服务管理下一页linux系统日志和screen

最后更新于4年前

这有帮助吗?