侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

CentOS5 + rsync 同步2台服务器的文件

linux  /  管理员 发布于 7年前   182

Always use rsync over ssh

Since rsync does not provide any security while transferring data it is recommended that you use rsync over ssh . This allows a secure remote connection. Now let us see some examples of rsync.

rsync command common options

  • --delete : delete files that don't exist on sender (system)
  • -v : Verbose (try -vv for more detailed information)
  • -e "ssh options" : specify the ssh as remote shell
  • -a : archive mode
  • -r : recurse into directories
  • -z : compress file data

Task : Copy file from a local computer to a remote server

Copy file from /www/backup.tar.gz to a remote server called openbsd.nixcraft.in

$ rsync -v -e ssh /www/backup.tar.gz [email protected]:~

Output:

Password: sent 19099 bytes received 36 bytes 1093.43 bytes/sec total size is 19014 speedup is 0.99

Please note that symbol ~ indicate the users home directory (/home/jerry).

Task : Copy file from a remote server to a local computer

Copy file /home/jerry/webroot.txt from a remote server openbsd.nixcraft.in to a local computer /tmp directory:

$ rsync -v -e ssh [email protected]:~/webroot.txt /tmp

Password

Task: Synchronize a local directory with a remote directory

$ rsync -r -a -v -e "ssh -l jerry" --delete openbsd.nixcraft.in:/webroot/ /local/webroot

Task: Synchronize a remote directory with a local directory

$ rsync -r -a -v -e "ssh -l jerry" --delete /local/webroot openbsd.nixcraft.in:/webroot

Task: Synchronize a local directory with a remote rsync server

$ rsync -r -a -v --delete rsync://rsync.nixcraft.in/cvs /home/cvs

Task: Mirror a directory between my "old" and "new" web server/ftp

You can mirror a directory between my "old" (my.old.server.com) and "new" web server with the command (assuming that ssh keys are set for password less authentication)

$ rsync -zavrR --delete --links --rsh="ssh -l vivek" my.old.server.com:/home/lighttpd /home/lighttpd

===================================================

当需要把服务器上的文件复制到另外的机器上,可用rsync来同步文件。

一、服务器端配置:
# yum -y install xinetd

# vi /etc/xinetd.d/rsync
将如下代码

复制代码 代码如下:
service rsync
{
disable = yes
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = Cdaemon
log_on_failure += USERID
}

中的 disable = yes 改成 disable = no

然后启动 xinetd
复制代码 代码如下:
# /etc/init.d/xinetd start

注意:如果服务器上装有防火墙记得要打开端口,默认端口是873

复制代码 代码如下:
# telnet 127.0.0.1 873

Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
# iptables -A INPUT -s 192.168.0.0/255.255.255.0 -p tcp -m tcp --dport 873 -j ACCEPT
# iptables -A INPUT -p tcp -m tcp --dport 873 -j DROP

# vi /etc/rsyncd.conf

复制代码 代码如下:
[backup]
path = /www
auth users = admin
uid = root
gid = root
secrets file = /etc/rsyncd.secrets
read only = no

[服务器代号]
path = 备份文件路径
auth users = 授权帐号
uid = 执行时的uid
gid = 执行时的gid
secrets file = 密码文件位置
read only = 是否只读

# vi /etc/rsyncd.secrets

复制代码 代码如下:
admin:1234 #用户名:密a

给文件正确的权限
# chown root:root /etc/rsyncd.secrets
# chmod 600 /etc/rsyncd.secrets

二、client 端进行同步
客户端默认好像已经装了rsync,没有的话装下:
# yum -y install rsync

执行异步同步操作:

/usr/bin/rsync -avz Cprogress  [email protected]::backup /www

同步命令说明:

1 显示目录内容

命令
――
a) rsync
b) rsync -r
c) rsync [email protected]::
d) rsync [email protected]:

命令说明
―――
a) 显示目录内容(第一层)
b) 递归显示目录内容
c) 显示远程主机目录内容
*注1:端口模式, 基于rsync用户的身份验证
*注2:rsync server上的目录必须具有xx7的权限.
d) 查看远程主机目录内容
*注1:remote shell模式, 通过ssh连接的基于系统本地用户的身份验证
*注2:这里只使用了一个冒号(:),同时用户名是远程主机的ssh 用户,密码也是ssh用户对应的密码。
*注3:使用””,则列出文件夹本身的信息。若要列出文件夹内容,应使用”/”。

参数说明
―――
-r 对目录进行递归操作

2 本地目录之间同步

命令
――
a) rsync -av Cprogress / *** 注意(/) ***
b) rsync -av Cprogress
c) rsync -avu Cprogress Cdelete /
d) rsync -av Cprogress Ctemp-dir=/tmp /

命令说明
―――
a) 同步src-dir目录下所有文件到dst-dir目录下
b) 同步src-dir目录下所有文件到dst-dir/src-dir目录下
c) 对src-dir目录内容向dst-dir目录下进行差异更新,有增加/更新则添加替换,有减少则对其删减
d) 比a)多了Ctemp-dir=/tmp,即指定/tmp为临时交换区,这样可以避免因目标目录空间不够引起的无法同步文件的错误。

参数说明
―――
-a 相当于 -rlptgoD 的集合
-u 等同于 Cupdate,在目标文件比源文件新的情况下不更新
-v 显示同步的文件
Cprogress 显示文件同步时的百分比进度、传输速率
Cdelete 删除目标目录中多于源目录的文件

3 异地主机之间同步
命令
――
a) rsync -avz Cprogress [email protected]::/
b) rsync -avz Cprogress [email protected]::/ Cpassword-file=/home/jack/rsync.jack
c) rsync -avuz Cprogress Cdelete [email protected]::/ Cpassword-file=/home/jack/rsync.jack
d) rsync -avz Cprogress [email protected]::/

命令说明
―――
a) 同步本地目录的内容到远程主机192.168.0.1的目录下,jack是rsync数据库用户(参见3. /etc/rsync.secrets)
b) 通过自动读取用户密码而实现非交互登录文件同步
c) 较b)多了-u和Cdelete
d) 同步远程主机内容到本地目录


  • 上一条:
    Linux Nginx VPS下简单解决CC攻击
    下一条:
    centos redhat系列对抗ddos之居家必备利器 banip.txt
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客