密钥对
选择一个账户zhangsan su zhangsan
生成公钥和私钥
~/.ssh/
Id_rsa 私钥
Id_ras.pub 公钥
将公钥中字符串追加到对方主机的对于用户的家目录的~/.ssh/authorized_key
公钥可以i复制字符串 私钥绑定
ssh-copy-id-p9999 用户@ip
Linux系统下数据同步服务RSYNC
RSYNC概述
1.什么是rsync
rsync的好姐妹
- sync 同步:刷新文件系统缓存,强制将修改过的数据块写入磁盘,并且更新超级块
- async 异步:将数据先放到缓冲区,再周期性(一般是30s)的去同步到磁盘
- rsync 远程同步:==remote synchronous==
数据同步过程
sync数据同步 =>保存文件(目标)=>强制把缓存中的数据写入磁盘(立即保存),实时性要求比较高的场景
asyn数据异步 =>保存文件(目标)=>将数据先放到缓冲区,再周期性(一般是30s)的去同步到磁盘,适合大批量数据同步的场景
2.rsync特点
可以镜像保存整个目录树和文件夹
可以保留原有的权限
3.rsync与scp的区别
两者都可以实现远程同步,但是相对比而言,rsync能力更强
支持增量备份
数据同步时保持文件的原有属性
基本语法:
本地文件同步
[root@localhost ~]# touch folder/f1/file{0..4}
[root@localhost ~]# which rsync
/usr/bin/rsync
[root@localhost ~]# # folder目录下的文件传到opt
[root@localhost ~]# rsync -av folder/ /opt/
sending incremental file list
./
f1/
f1/file0
f1/file1
f1/file2
f1/file3
f1/file4
f2/
f3/
sent 401 bytes received 130 bytes 1,062.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls /opt/
f1 f2 f3
[root@localhost ~]# rsync -av folder /opt/
sending incremental file list
folder/
folder/f1/
folder/f1/file0
folder/f1/file1
folder/f1/file2
folder/f1/file3
folder/f1/file4
folder/f2/
folder/f3/
sent 412 bytes received 131 bytes 1,086.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls /opt/
f1 f2 f3 folder
[root@localhost ~]# # 不加/,就会将folder整个目录同步到/opt下
[root@localhost ~]# rm -rf /opt/*
[root@localhost ~]# ls /opt/
[root@localhost ~]# rsync -av folder/ /opt/
sending incremental file list
./
f1/
f1/file0
f1/file1
f1/file2
f1/file3
f1/file4
f2/
f3/
sent 401 bytes received 130 bytes 1,062.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# rm -rf /opt/*
[root@localhost ~]# rsync -av folder/ /opt/
sending incremental file list
./
f1/
f1/file0
f1/file1
f1/file2
f1/file3
f1/file4
f2/
f3/
sent 401 bytes received 130 bytes 1,062.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# rsync -avR folder/ /opt/
sending incremental file list
folder/
folder/f1/
folder/f1/file0
folder/f1/file1
folder/f1/file2
folder/f1/file3
folder/f1/file4
folder/f2/
folder/f3/
sent 412 bytes received 131 bytes 1,086.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls /opt/
f1 f2 f3 folder
[root@localhost ~]# # 现在不传到/opt
[root@localhost ~]# rsync -av folder/f1/ folder/f2/
sending incremental file list
./
file0
file1
file2
file3
file4
sent 314 bytes received 114 bytes 856.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# touch folder/f1/file5
[root@localhost ~]# ls
[root@localhost ~]# touch folder/f1/file5
[root@localhost ~]# ls folder/f1
file0 file1 file2 file3 file4 file5
[root@localhost ~]# ls folder/f2
file0 file1 file2 file3 file4
[root@localhost ~]# rsync -av folder/f1/ folder/f2/
sending incremental file list
./
file5
sent 179 bytes received 38 bytes 434.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls folder/f2
file0 file1 file2 file3 file4 file5
[root@localhost ~]# ls folder/f1
file0 file1 file2 file3 file4 file5
[root@localhost ~]# rm -rf folder/f1/file0
[root@localhost ~]# ls folder/f1
file1 file2 file3 file4 file5
[root@localhost ~]# ls folder/f2
file0 file1 file2 file3 file4 file5
[root@localhost ~]# rsync -av folder/f1/ folder/f2/
sending incremental file list
./
sent 127 bytes received 19 bytes 292.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls folder/f2
file0 file1 file2 file3 file4 file5
[root@localhost ~]# rsync -av -delete folder/f1/ folder/f2/
sending incremental file list
sent 120 bytes received 12 bytes 264.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls folder/f2
file0 file1 file2 file3 file4 file5
[root@localhost ~]# # rsync语法:
[root@localhost ~]# #rsync [选项] 原数据位置 目标位置
[root@localhost ~]# # 文件的增加会同步
[root@localhost ~]# vim folder/f1/file1
[root@localhost ~]# cat folder/f1/file1
大家好我是渣渣辉
[root@localhost ~]# # 修改了f1中的内容,f2目录中没有发生改变
[root@localhost ~]# rsync -av --delete folder/f1/ folder/f2/
sending incremental file list
deleting file0
./
file1
sent 199 bytes received 47 bytes 492.00 bytes/sec
total size is 25 speedup is 0.10
[root@localhost ~]# cat folder/f2/file1
大家好我是渣渣辉
[root@localhost ~]# # 文件的修改会被rsync同步
[root@localhost ~]# touch folder/f1/file0 -m -d "2014-7-14 00:00"
[root@localhost ~]# rsync -av --delete folder/f1/ folder/f2/
sending incremental file list
./
file0
sent 181 bytes received 38 bytes 438.00 bytes/sec
total size is 25 speedup is 0.11
[root@localhost ~]#
[root@localhost ~]# ls -l folder/f1/file0
-rw-r--r--. 1 root root 0 7月 14 2014 folder/f1/file0
[root@localhost ~]# chmod g+w folder/f1/file0
[root@localhost ~]# ls -l folder/f1/file0
-rw-rw-r--. 1 root root 0 7月 14 2014 folder/f1/file0
[root@localhost ~]# rsync -av --delete folder/f1/ folder/f2/
sending incremental file list
sent 146 bytes received 19 bytes 330.00 bytes/sec
total size is 25 speedup is 0.15
[root@localhost ~]# ls -l folder/f2/file0
-rw-rw-r--. 1 root root 0 7月 14 2014 folder/f2/file0
[root@localhost ~]#
本地同步
- 安装rsync
- Rsync -av 源 目标地址
- 如果源目录不以/结尾,整个目录同步包含目录文件,带斜杠,只同步目录下的文件
- -R保留目录的相对路径,也会携带目录
- 同步的内容 文件的新增 修改,删除(--delete)属性 (时间,权限)
[root@localhost ~]# netstat -lntup|grep rsync
[root@localhost ~]# vi /etc/rsy
rsyncd.conf rsyslog.conf rsyslog.d/
[root@localhost ~]# vi /etc/rsyncd.conf
[root@localhost ~]# mkdir -p /app/studentweb/src/main/java/co/goho/an.studentweb
[root@localhost ~]# ls /app/
studentweb
[root@localhost ~]# touch /app/studentweb/src/main/java/co/goho/an.studentweb//file{0..9}.java
[root@localhost ~]# tree rsync
rsync [error opening dir]
0 directories, 0 files
[root@localhost ~]# tree /opt/
/opt/
├── f1
│?? ├── file0
│?? ├── file1
│?? ├── file2
│?? ├── file3
│?? ├── file4
│?? └── file5
├── f2
│?? ├── file0
│?? ├── file1
│?? ├── file2
│?? ├── file3
│?? ├── file4
│?? └── file5
├── f3
└── folder
├── f1
│?? ├── file0
│?? ├── file1
│?? ├── file2
│?? ├── file3
│?? ├── file4
│?? └── file5
├── f2
│?? ├── file0
│?? ├── file1
│?? ├── file2
│?? ├── file3
│?? ├── file4
│?? └── file5
└── f3
7 directories, 24 files
[root@localhost ~]# tree /app/
/app/
└── studentweb
└── src
└── main
└── java
└── co
└── goho
└── an.studentweb
├── file0.java
├── file1.java
├── file2.java
├── file3.java
├── file4.java
├── file5.java
├── file6.java
├── file7.java
├── file8.java
└── file9.java
[root@localhost ~]# vi /etc/rsyncd.conf
[app] //21行后面加
path=/app/studentweb/
log file=/var/log/rsync.log
[root@localhost ~]# systemctl restart rsyncd //起服务
[root@localhost ~]# tree /app/
在/etc⽬录下创建rsyncd.secrets⽂件
# vim /etc/rsyncd.secrets user1:123 => 设置密码,⽤户名:密码
user2:123
user2:123
更改密码⽂件权限为600
# chmod 600 /etc/rsyncd.secrets
重启rsyncd服务
# systemctl restart rsyncd
Backup备份服务器:
# rsync -av user1@10.1.1.10::app ./
Password:123
RSYNC集合INOTIFY⼯具实现代码实时同步(重点)
第⼀步:直接安装inotify-tools yum -y install inotify-tools
监听指定⽬录,⼀旦⽬录发上修改,就执⾏指定的指令
# tar -xf inotify-tools-3.13.tar.gz -C /usr/local/
# cd /usr/local/inotify-tools-3.14
# ./configure
# make
# make install
安装完后,就会产⽣下⾯两个命令
/usr/bin/inotifywait 等待
/usr/bin/inotifywatch 看守 /usr/bin/inotifywait
-m : ⼀直监控某个⽬录,create、delete、modify等⾏为
-r : 递归,不仅仅监控⽬录还要监控⽬录下的⽂件
-q : 获取操作信息,但是不输出
-e : 哪些⾏为需要被监控,modify,delete,create,attrib,move
modify: ⽂件被修改
delete: ⽂件被删除
create: ⽂件被创建
attrib: ⽂件属性被修改
move: ⽂件被移动
第⼆步:编写inotify.sh
# vim inotify.sh
#!/bin/bash
添加可执⾏权限
# chmod +x inotify.sh
让inotify.sh⽂件⼀直执⾏下去
# nohup ./inotify.sh &
& : 让inotify.sh在计算机后台运⾏,可以使⽤jobs命令查看,kill %编号结束,当我们退出 终端时,这个执⾏会⾃动结束
nohup : 让程序⼀直在后台运⾏,即使我们关闭了终端