rsync远程同步

文件传输常用软件

文件传输:lrzsz、ftp、samba、cifs、nfs、sftp、WinSCP、rsync、SVN、git

定时同步和实时同步

定时同步:rsync + crontab

实时同步:rsync + inotifywait

关于rsync

一款快速增量备份的工具

远程文件同步,可实现跨主机增量同步文件数据。

rsync的命令格式

下行同步:

rsync [选项] 原始位置(源服务器的地址) 目标位置(本地目录)

上行同步:

rsync [选项] 目标位置(本地目录) 原始位置(源服务器的地址

选项 作用
-a 归档模式,保留文件的权限、属性等信息,等同于组合选项“-rlptgoD”。
--delete 删除目标位置有而原始位置没有的文件.

rsync远程同步_第1张图片

配置rsync同步源服务器

/etc/resyncd.conf

uid = root                      #指定用户
gid = root
use chroot =yes                 #是否做禁锢
pid file = /var/run/rsyncd.pid  #指定pid文件所在目录
log file = /var/log/rsyncd.log  #指定日志文件所在目录
address = 192.168.116.111       #指定本机监听,默认是任意地址
port = 873         # 指定端口
#exxcludde = 排除哪个目录
#transfer logging = yes #传输过程中是否做日志目录
#timeout =900         #超时时间900秒
#ignore nonreadable = yes   #是否忽略不可读
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2     #哪些文件不做压缩 
hosts allow = 192.168.116.0/24  #指定白名单,允许客户端访问
​
​
自定义共享模块:
[shareky33]
path = /opt/data  # 设置共享目录
comment = this is rsync shared area#做描述
read only = yes   #是否只读
auth users = hyh   #认证用户
secrets file = /opt/rsync_userlist #指定密码文件位置
​
在/opt下
mkdir 一个data文件夹,设置共享目录
创建密码文件
touch rsync_userlist
vim rsync_userlist
hyh:abc123
加权限:chmod 600 rsync_userlist
​
rsync --daemon #开启进程在后台运行
​
netstat -lntup | grep rsync#查看端口
​
cp -a /etc/passwd /etc/shadow /etc/yum.repos.d/ ./
同步源服务器配置完成

rsync远程同步_第2张图片 

配置发起端,进行同步

在发起端建立一个共享的目录
mkdir ky33
cd ky33
​
rsync -avz [email protected]::shareky33 ./
password:

免交互:
在发起端/opt下准备一个密码文件
echo abc123 > ./rsync_passwd
chmod 600 rsync_passwd
到同步端建立三个新文件或目录,不然无法同步新的内容
echo a > a
echo b > b
echo c > c
​
到发送端
rsync -avz --password-file=/opt/rsync_passwd rsync://[email protected]/shareky33 /root/ky33

rsync远程同步_第3张图片

做任务计划,定时同步:

目标位置和原始位置内容同步,并多余的内容删除


rsync -avz --password-file=/opt/rsync_passwd --delete [email protected]::shareky33 /root/ky33

rsync远程同步_第4张图片

rsync远程同步_第5张图片

如何快速清空目录中的大量文件?

方法一:rsync -a --delete 空目录/ 目标目录/

方法二:find 目标目录/ [选项] -delete

方法二效率没有一高

rsync有两种常用的认证方式

1.rsync-daemon方式

2.ssh,使用rsync-daemon会有缺陷,使用ssh更灵活

inotify

  • 使用inotify通知接口,可用用来监控文件的各种变化清空,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。

  • 将inotify机制与rsync工具相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。

  • 因为 inotify 通知机制由 Linux 内核提供,因此主要做本机监控,在触发式备份中应用时更适合上行同步

rsync远程同步_第6张图片

发起端配置rsync+inotify

1.修改源服务器的配置文件

vim /etc/rsyncd.conf
read only =no 这样就能在指定目录中写入
cat /var/run/rsyncd.conf#查看进程pid号
kill进程号
rsync --daemon
cat /var/run/rsyncd.pid
netstat -lntp | grep rsync

2、调整 inotify 内核参数

在Linux内核中,默认的inotify机制提供了三个调控参数:max_queue_events(监控事件队列,默认值为16384)、max_user_instances(最多监控实例数,默认值为128)、max_user_watches(每个实例最多监控文件数,默认值为8192)。当要监控的目录、文件数量较多或者变化较频繁时,建议加大这三个参数的值。
​
cat /proc/sys/fs/inotify/max_queued_events
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_user_watches
​
​
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
​
sysctl -p   #内核优化
​

3.安装 inotify-tools

用 inotify 机制还需要安装 inotify-tools,以便提供 inotifywait、inotifywatch 辅助工具程序,用来监控、汇总改动情况。 inotifywait:可监控modify(修改)、create(创建)、move(移动)、delete(删除)、attrib(属性更改)等各种事件,一有变动立即输出结果。

上传软件包到/opt
​
tar zxvf inotify-tools-3.14.tar.gz
cd /opt/inotify-tools-3.14
./configure
make && make install
实时监控
inotifywait -mrq -e modify,create,move,delete /var/www/html
#选项“-e”:用来指定要监控哪些事件
#选项“-m”:表示持续监控
#选项“-r”:表示递归整个目录
#选项“-q”:简化输出信息

4.在另外一个终端编写触发式同步脚本

(注意,脚本名不可包含 rsync 字符串,否则脚本可能不生效)

先用inotify监控,通过管道符号交给while read,读取到相关事件后,如果rsync未在执行则立即启动

vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /root/ky33
RSYNC_CMD="rsync -azH --delete --password-file=/opt/ky33 /root/ky33 [email protected]::shareky33/"
#使用while、read持续获取监控结果,根据结果可以作进一步判断是否读取到输出的监控记录
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
    #如果rsync未在执行,则立即启动
        $RSYNC_CMD
    fi
done
​
​
​
chmod +x inotify.sh
​
​
在源服务器里

rsync远程同步_第7张图片
​了解一些git的用法

需要yum -y install git

SVN、Git 代码版本控制软件,主要功能作代码版本控制,支持日志备份、数据恢复、文件同步等功能

git类型 作用
git clone 克隆代码
git add 添加文件到暂存区
git commit 提交代码
git push 推送代码到代码仓库(国际的代码仓库:github、国内的gitee、本地搭建私有的代码仓库gitlab)
git log 查看所有的git的操作日志
git pull 更新代码
git branch 管理代码分支
git checkout 切换分支
git clone https://mirror.ghproxy.com/https://github.com/otale/tale.git

使用国内的镜像去从网站上克隆代码到本地

你可能感兴趣的:(大数据)