rsync&vsftpd

rsync:远程同步服务

rsync概述 

#是一个功能强大的文件同步和备份工具,支持本地和远程文件复制,特别适用于需要增量备份
或高效传输大型目录的场景。
#.数据同步过程
sync数据同步:强制将修改过的数据块写入磁盘,对实时性要求较高
async数据异步:将数据先放到缓冲区,再周期性的去同步到磁盘,适合大批量数据同步

#基本语法:
SRC:源地址
DEST:目的地址
HOST:主机
user:用户名
port:端口

#--本地文件同步:
#rsync [选项...] SRC... [DEST]
远程文件同步(通过远程shell访问)
#.pull【拉取,下载】:rsync [选项...] [user@]host:源地址... [DEST]
#.push【上传】:rsync [选项...] SRC... [user@]host:DEST
远程文件同步(通过sync后台进程访问)
#.pull【拉取】:rsync [选项] [user@]host::SRC...[DEST]
                rsync [选项] rsync://[user@]host[:port]/SRC...[DEST]
#.push【上传】:rsync [选项] SRC... [USER@]HOST::DEST
                rsync [选项] SRC... rsync://[USER@]HOST[:PORT]/DEST

 

 本地文件同步

[root@samba ~]# yum -y install rsync  ##安装
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
file:///mnt/iso/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mnt/iso/repodata/repomd.xml"
正在尝试其它镜像。
软件包 rsync-3.1.2-10.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@samba tmp]# mkdir dir{1..3}
[root@samba tmp]# ls
dir1  dir2  dir3
[root@samba tmp]# mkdir dir1/aa{1..4}
[root@samba tmp]# touch dir1/file{1..9}.txt
[root@samba tmp]# ls dir1/
aa1  aa3  file1.txt  file3.txt  file5.txt  file7.txt  file9.txt
aa2  aa4  file2.txt  file4.txt  file6.txt  file8.txt
#--将/tmp/dir1/目录下的内容,同步到/tmp/dir2/目录下
#-a:保持属性
#-v:打印详细输出
[root@samba tmp]# ls dir1/
aa1  aa3  file1.txt  file3.txt  file5.txt  file7.txt  file9.txt
aa2  aa4  file2.txt  file4.txt  file6.txt  file8.txt
[root@samba tmp]# ls dir2/
[root@samba tmp]# rsync -av dir1/ /tmp/dir2/
sending incremental file list
./
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt
aa1/
aa2/
aa3/
aa4/

sent 655 bytes  received 210 bytes  1,730.00 bytes/sec
total size is 0  speedup is 0.00
[root@samba tmp]# ls dir2
aa1  aa3  file1.txt  file3.txt  file5.txt  file7.txt  file9.txt
aa2  aa4  file2.txt  file4.txt  file6.txt  file8.txt
#--查看文件属性基本相同
[root@samba tmp]# ll -h dir2/
总用量 0
drwxr-xr-x 2 root root 6 1月  26 12:27 aa1
drwxr-xr-x 2 root root 6 1月  26 12:27 aa2
drwxr-xr-x 2 root root 6 1月  26 12:27 aa3
drwxr-xr-x 2 root root 6 1月  26 12:27 aa4
-rw-r--r-- 1 root root 0 1月  26 12:28 file1.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file2.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file3.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file4.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file5.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file6.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file7.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file8.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file9.txt
[root@samba tmp]# ll -h dir1/
总用量 0
drwxr-xr-x 2 root root 6 1月  26 12:27 aa1
drwxr-xr-x 2 root root 6 1月  26 12:27 aa2
drwxr-xr-x 2 root root 6 1月  26 12:27 aa3
drwxr-xr-x 2 root root 6 1月  26 12:27 aa4
-rw-r--r-- 1 root root 0 1月  26 12:28 file1.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file2.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file3.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file4.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file5.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file6.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file7.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file8.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file9.txt
#--将/tmp/dir1目录同步到/tmp/dir3/目录下
[root@samba tmp]# rsync -av /tmp/dir1 /tmp/dir3/
sending incremental file list
dir1/
dir1/file1.txt
dir1/file2.txt
dir1/file3.txt
dir1/file4.txt
dir1/file5.txt
dir1/file6.txt
dir1/file7.txt
dir1/file8.txt
dir1/file9.txt
dir1/aa1/
dir1/aa2/
dir1/aa3/
dir1/aa4/

sent 665 bytes  received 211 bytes  1,752.00 bytes/sec
total size is 0  speedup is 0.00
[root@samba tmp]# ls dir3/
dir1
[root@samba tmp]# ll dir3/dir1/
总用量 0
drwxr-xr-x 2 root root 6 1月  26 12:27 aa1
drwxr-xr-x 2 root root 6 1月  26 12:27 aa2
drwxr-xr-x 2 root root 6 1月  26 12:27 aa3
drwxr-xr-x 2 root root 6 1月  26 12:27 aa4
-rw-r--r-- 1 root root 0 1月  26 12:28 file1.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file2.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file3.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file4.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file5.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file6.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file7.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file8.txt
-rw-r--r-- 1 root root 0 1月  26 12:28 file9.txt
#--将/tmp/dir1/目录下的内容同步到新目录/tmp/dir4/【目录不存在将自动创建dir4】
[root@samba tmp]# rsync -av /tmp/dir1/* /tmp/dir4
sending incremental file list
created directory /tmp/dir4
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt
aa1/
aa2/
aa3/
aa4/

sent 640 bytes  received 239 bytes  1,758.00 bytes/sec
total size is 0  speedup is 0.00
[root@samba tmp]# ls dir4/
aa1  aa3  file1.txt  file3.txt  file5.txt  file7.txt  file9.txt
aa2  aa4  file2.txt  file4.txt  file6.txt  file8.txt
#--保留相对路径的目录[连同/tmp目录一起拷贝]
[root@samba tmp]# rsync -avR /tmp/dir1/ /tmp/dir3/
sending incremental file list
/tmp/
/tmp/dir1/
/tmp/dir1/file1.txt
/tmp/dir1/file2.txt
/tmp/dir1/file3.txt
/tmp/dir1/file4.txt
/tmp/dir1/file5.txt
/tmp/dir1/file6.txt
/tmp/dir1/file7.txt
/tmp/dir1/file8.txt
/tmp/dir1/file9.txt
/tmp/dir1/aa1/
/tmp/dir1/aa2/
/tmp/dir1/aa3/
/tmp/dir1/aa4/

sent 701 bytes  received 215 bytes  1,832.00 bytes/sec
total size is 0  speedup is 0.00
您在 /var/spool/mail/root 中有新邮件
[root@samba tmp]# ls /dir3
[root@samba tmp]# ls dir3/
aa1  aa3  file1.txt  file3.txt  file5.txt  file7.txt  file9.txt
aa2  aa4  file2.txt  file4.txt  file6.txt  file8.txt  tmp
[root@samba tmp]# ll -dh dir3/tmp/   ##tmp目录的粘贴位也拷贝了
drwxrwxrwt 3 root root 18 1月  26 12:37 dir3/tmp/
#--先删除与源文件不一致的文件,再进行同步
[root@samba tmp]# ls dir3
dir1
[root@samba tmp]# ls dir1
aa1  aa3  file1.txt  file3.txt  file5.txt  file7.txt  file9.txt
aa2  aa4  file2.txt  file4.txt  file6.txt  file8.txt
[root@samba tmp]# rsync -av --delete /tmp/dir1/ /tmp/dir3/
sending incremental file list
deleting dir1/aa4/
deleting dir1/aa3/
deleting dir1/aa2/
deleting dir1/aa1/
deleting dir1/file9.txt
deleting dir1/file8.txt
deleting dir1/file7.txt
deleting dir1/file6.txt
deleting dir1/file5.txt
deleting dir1/file4.txt
deleting dir1/file3.txt
deleting dir1/file2.txt
deleting dir1/file1.txt
deleting dir1/
./
file1.txt
file2.txt
file3.txt
file4.txt
file5.txt
file6.txt
file7.txt
file8.txt
file9.txt
aa1/
aa2/
aa3/
aa4/

sent 655 bytes  received 429 bytes  2,168.00 bytes/sec
total size is 0  speedup is 0.00

远程文件同步

#push:上传文件到远程服务器端
#基本语法:
#rsync -av 本地文件或目录 远程用户名@远程服务器的ip地址:目录路径
#案例:
#rsync -av rs01.txt root@192.168.10.12:/root  同步文档
#rsync -av linux root@192.168.10.12:/root  同步目录
#--创建客户端环境
[root@client tmp]# cp /etc/passwd .
[root@client tmp]# ls
passwd
[root@client tmp]# mkdir test
[root@client tmp]# echo "hello" > test/a.txt
[root@client tmp]# yum -y install rsync
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
file:///mnt/iso/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /mniso/repodata/repomd.xml"
正在尝试其它镜像。
软件包 rsync-3.1.2-10.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@client tmp]# ls
passwd  test
#--服务器环境
[root@server tmp]# ls
dir1  dir2  dir3  dir4
#--将client主机中/tmp/passwd文件上传到server服务器/tmp/目录下
[root@client tmp]# rsync -av /tmp/passwd root@192.168.81.130:/tmp/
The authenticity of host '192.168.81.130 (192.168.81.130)' can't be established.
ECDSA key fingerprint is SHA256:Z8gjXz/HXW5nDz0N/l3R4O/rPspAhxyEhrZ3IbfUnVQ.
ECDSA key fingerprint is MD5:19:aa:15:53:a8:1a:bf:ee:71:11:cc:69:33:3b:d3:ec.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.81.130' (ECDSA) to the list of known hosts.
root@192.168.81.130's password: 
sending incremental file list
passwd

sent 1,038 bytes  received 35 bytes  429.20 bytes/sec
total size is 946  speedup is 0.88
[root@server tmp]# ls    ##在服务器端查看该文件
dir1  dir2  dir3  dir4  passwd
#-同步目录
[root@client tmp]# rsync -av /tmp/test root@192.168.81.130:/tmp/
root@192.168.81.130's password: 
sending incremental file list
test/
test/a.txt

sent 127 bytes  received 39 bytes  110.67 bytes/sec
total size is 6  speedup is 0.04
[root@server tmp]# ll   ##在服务器端查看该文件
总用量 4
drwxr-xr-x 6 root root 203 1月  26 12:28 dir1
drwxr-xr-x 6 root root 203 1月  26 12:28 dir2
drwxr-xr-x 7 root root 214 1月  26 13:06 dir3
drwxr-xr-x 6 root root 203 1月  26 12:37 dir4
-rw-r--r-- 1 root root 946 1月  26 19:33 passwd
drwxr-xr-x 2 root root  19 1月  26 19:34 test
#-清空客户端环境
[root@client tmp]# ls
passwd  test
[root@client tmp]# rm -rf *
[root@client tmp]# ls
[root@client tmp]# 
#--将远程服务器的目录同步到本地
[root@client tmp]# rsync -av root@192.168.81.130:/tmp/dir1 .
root@192.168.81.130's password: 
receiving incremental file list
dir1/
dir1/file1.txt
dir1/file2.txt
dir1/file3.txt
dir1/file4.txt
dir1/file5.txt
dir1/file6.txt
dir1/file7.txt
dir1/file8.txt
dir1/file9.txt
dir1/aa1/
dir1/aa2/
dir1/aa3/
dir1/aa4/

sent 219 bytes  received 661 bytes  586.67 bytes/sec
total size is 0  speedup is 0.00
[root@client tmp]# ls   
dir1
#--将远程服务器的文件同步到本地
[root@client tmp]# rsync -av root@192.168.81.130:/tmp/dir1/file1.txt .
root@192.168.81.130's password: 
receiving incremental file list
file1.txt

sent 43 bytes  received 91 bytes  53.60 bytes/sec
total size is 0  speedup is 0.00
[root@client tmp]# ls
dir1  file1.txt

rsync作为系统服务

#可以将rsync服务开启,rsync作为命令使用是找不到进程的,rsync作为系统服务,需要以
下思路对外提供服务---监听端口---启动服务---启动脚本---配置文件
#可以将rsync服务开启,rsync作为命令使用是找不到进程的,rsync作为系统服务,需要以下思路对外提供服务---监听端口---启动服务---启动脚本---配置文件
#--环境准备
#.一台code代码服务器,一台backup备份服务器
[root@server tmp]# hostnamectl set-hostname code
[root@server tmp]# su -
上一次登录:日 1月 26 20:24:29 CST 2025pts/0 上
[root@code ~]# ip -br a
lo               UNKNOWN        127.0.0.1/8 ::1/128 
ens33            UP             192.168.81.130/24 fe80::20c:29ff:fefd:2a/64 
[root@client tmp]# hostnamectl set-hostname backup
[root@client tmp]# su -
上一次登录:日 1月 26 20:24:13 CST 2025pts/0 上
[root@backup ~]# ip -br a
lo               UNKNOWN        127.0.0.1/8 ::1/128 
ens33            UP             192.168.81.10/24 fe80::20c:29ff:fe32:6f9/64 
#.关闭防火墙,selinux,启动rsync服务
[root@code ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@backup ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@code ~]# getenforce
Disabled
[root@backup ~]# getenforce
Disabled
#.关闭networkManager
[root@code ~]# systemctl stop NetworkManager
[root@backup ~]# systemctl stop NetworkManager
#.配置yum源
[root@code ~]# rpm -ivh wget-1.14-18.el7_6.1.x86_64.rpm 
准备中...                          ################################# [100%]
正在升级/安装...
   1:wget-1.14-18.el7_6.1             ################################# [100%]
[root@code ~]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo     ##安装epel源
--2025-01-26 21:09:23--  https://mirrors.aliyun.com/repo/epel-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 122.189.170.104, 122.189.170.97, 122.189.170.99, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|122.189.170.104|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:664 [application/octet-stream]
正在保存至: “/etc/yum.repos.d/epel.repo”

100%[===================================================================================================================>] 664         --.-K/s 用时 0s      

2025-01-26 21:09:24 (120 MB/s) - 已保存 “/etc/yum.repos.d/epel.repo” [664/664])
#--准备代码文件
[root@code ~]# mkdir -p /app/java
[root@code ~]# mkdir -p /app/java/aa{1..4}
[root@code ~]# touch /app/java/file{1..9}.java
[root@code ~]# ls /app/java/
aa1  aa3  file1.java  file3.java  file5.java  file7.java  file9.java
aa2  aa4  file2.java  file4.java  file6.java  file8.java
#--配置定时同步
[root@code ~]# systemctl start rsyncd
#[app]:标签,要对应一个需要同步的目录
#path = /app/java/:指定需要同步的路径
#log file = /var/log/rsync.log:指定要生成的日志位置
[root@code ~]# vi /etc/rsyncd.conf 
...
#在文件末行添加
[app]
path = /app/java/
log file = /var/log/rsync.log
[root@code ~]# systemctl start rsyncd   ##启动,查看服务
[root@code ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
   Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2025-01-26 22:43:37 CST; 4min 3s ago
 Main PID: 6471 (rsync)
   CGroup: /system.slice/rsyncd.service
           └─6471 /usr/bin/rsync --daemon --no-detach

1月 26 22:43:37 code systemd[1]: Started fast remote file copy program daemon.
1月 26 22:43:37 code rsyncd[6471]: rsyncd version 3.1.2 starting, listenin...73
Hint: Some lines were ellipsized, use -l to show in full.
[root@code ~]# ss -tunlp | grep 6471    ##根据服务id查看启动端口
tcp    LISTEN     0      5         *:873                   *:*                   users:(("rsync",pid=6471,fd=4))
tcp    LISTEN     0      5      [::]:873                [::]:*                   users:(("rsync",pid=6471,fd=5))
#--在备份服务器查看标签
[root@backup ~]# rsync -av root@192.168.81.130::
app
[root@backup ~]# mkdir -p /backup/java_backup
#--手动下载文件
[root@backup ~]# rsync -av root@192.168.81.130::app /backup/java_backup/
receiving incremental file list
./
file1.java
file2.java
file3.java
file4.java
file5.java
file6.java
file7.java
file8.java
file9.java
aa1/
aa2/
aa3/
aa4/

sent 218 bytes  received 664 bytes  1,764.00 bytes/sec
total size is 0  speedup is 0.00
[root@backup ~]# ls /backup/java_backup/  ##已同步文件与属性信息
aa1  aa3  file1.java  file3.java  file5.java  file7.java  file9.java
aa2  aa4  file2.java  file4.java  file6.java  file8.java
#--还原环境
[root@backup ~]# rm -rf /backup/java_backup/*
[root@backup ~]# ll /backup/java_backup/
总用量 0
#--定时重启
[root@backup ~]# which rsync
/bin/rsync
[root@backup ~]# vi rsync.sh
[root@backup ~]# cat rsync.sh   ##完全一致,将目的目录中不同的删除[--delete]
#!/bin/bash
/bin/rsync -av root@192.168.81.130::app /backup/java_backup/
[root@backup ~]# chmod +x rsync.sh
[root@backup ~]# crontab -e   ##创建定时任务,指定每两分钟执行一次脚本
crontab: installing new crontab
[root@backup ~]# crontab -l
*/2 * * * * /bin/bash /root/rsync.sh
#--给代码添加内容
[root@code ~]# cd /app/java/
[root@code java]# ls
aa1  aa3  file1.java  file3.java  file5.java  file7.java  file9.java
aa2  aa4  file2.java  file4.java  file6.java  file8.java
[root@code java]# touch b1.txt
[root@code java]# mkdir test01
[root@code java]# tail -f /var/log/rsync.log   ##查看同步日志
2025/01/26 14:52:13 [6560] sent 684 bytes  received 223 bytes  total size 0
2025/01/26 14:55:02 [6578] rsync on app/ from UNKNOWN (192.168.81.10)
2025/01/26 14:55:02 [6578] building file list
2025/01/26 14:55:02 [6578] sent 684 bytes  received 223 bytes  total size 0
2025/01/26 15:06:01 [6666] rsync on app/ from UNKNOWN (192.168.81.10)
2025/01/26 15:06:01 [6666] building file list
2025/01/26 15:06:01 [6666] sent 744 bytes  received 242 bytes  total size 0
2025/01/26 15:08:02 [6680] rsync on app/ from UNKNOWN (192.168.81.10)
2025/01/26 15:08:02 [6680] building file list
2025/01/26 15:08:02 [6680] sent 368 bytes  received 40 bytes  total size 0
#--查看是否同步
[root@backup ~]# cd /backup/java_backup/
[root@backup java_backup]# ls
aa1  aa3  b1.txt      file2.java  file4.java  file6.java  file8.java  test01
aa2  aa4  file1.java  file3.java  file5.java  file7.java  file9.java

rsync扩展

#-----------给rsync服务添加密码
#--修改配置文件
配置多个用户用 逗号 隔开
#[app]:标签对应一个备份目录
#path = /app/java/:需要备份的目录路径
#log file = /var/log/rsync.log:同步日志位置
#auth users = user1,user2:配置多个用户
#secrets file = /etc/rsync.secrets:自定义密码文件
[root@code java]# vim /etc/rsyncd.conf 
[root@code java]# cat /etc/rsyncd.conf 
...
# [ftp]
#        path = /home/ftp
#        comment = ftp export area
[app]
path = /app/java/
log file = /var/log/rsync.log

auth users = user1,user2
secrets file = /etc/rsync.secrets
[root@code java]# vi /etc/rsync.secrets   ##创建对应的密码文件,并赋予权限
[root@code java]# cat /etc/rsync.secrets  ##格式:用户名:密码
user1:123
user2:123
[root@code java]# chmod 600 /etc/rsync.secrets 
[root@code java]# ll /etc/rsync.secrets 
-rw------- 1 root root 20 1月  26 23:23 /etc/rsync.secrets
[root@code java]# systemctl restart rsyncd   ##重启服务
[root@code java]# id user1   ##查看用户
id: user1: no such user
#--备份服务器查看标签
[root@backup tmp]# rsync -av root@192.168.81.130::   ##使用root用户查看标签
app            	
[root@backup tmp]# rsync -av user1@192.168.81.130::  ##使用虚拟用户查看标签
app
#--验证使用虚拟用户进行同步
[root@backup ~]# cd "/tmp"   ##清除备份服务器数据
[root@backup tmp]# ls
dir1  file1.txt
[root@backup tmp]# rm -rf *
[root@backup tmp]# ls
[root@backup tmp]# rsync -av user1@192.168.81.130::app .   ##输入虚拟用户密码123
Password: 
receiving incremental file list
./
b1.txt
file1.java
file2.java
file3.java
file4.java
file5.java
file6.java
file7.java
file8.java
file9.java
aa1/
aa2/
aa3/
aa4/
test01/

sent 241 bytes  received 754 bytes  221.11 bytes/sec
total size is 0  speedup is 0.00
[root@backup tmp]# ls   ##查看同步内容
aa1  aa3  b1.txt      file2.java  file4.java  file6.java  file8.java  test01
aa2  aa4  file1.java  file3.java  file5.java  file7.java  file9.java
#--使用虚拟用户user2进行同步
[root@backup tmp]# ls
[root@backup tmp]# rsync -av user2@192.168.81.130::app .
Password: 
receiving incremental file list
./
b1.txt
file1.java
file2.java
file3.java
file4.java
file5.java
file6.java
file7.java
file8.java
file9.java
aa1/
aa2/
aa3/
aa4/
test01/

sent 241 bytes  received 754 bytes  398.00 bytes/sec
total size is 0  speedup is 0.00
[root@backup tmp]# ls
aa1  aa3  b1.txt      file2.java  file4.java  file6.java  file8.java  test01
aa2  aa4  file1.java  file3.java  file5.java  file7.java  file9.java

inotify工具实时同步

浏览器进入链接->点击“Download”等待下载

inotify-tools download | SourceForge.net

[root@code ~]# tar -xvf inotify-tools-3.13.tar.gz -C /usr/local/  ##解压
[root@code inotify-tools-3.13]# pwd  ##进入解压目录
/usr/local/inotify-tools-3.13
[root@code inotify-tools-3.13]# yum -y install gcc-c++ make    ##安装依赖包
[root@client1 inotify-tools-3.13]# ./configure
[root@client1 inotify-tools-3.13]# make && make install
#--安装完成将出现两条命令
[root@client1 inotify-tools-3.13]# ls -l /usr/local/bin/
总用量 108
-rwxr-xr-x. 1 root root 52376 1月  27 00:19 inotifywait
-rwxr-xr-x. 1 root root 54472 1月  27 00:19 inotifywatch
#.inotifywaith:命令用于收集关于被监视的文件属性变更,包括每一个inotify事件发生了多少次
#.inotifywatch:命令-收集关于被监视的文件系统的统计数据

#/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib,move /app/java |while read events; do
##执行inotifywait命令,如果在目录下做了修改,删除,创建,属性变更,剪切 对/app/java/这个目录,将这个处罚的动作传递给变量events[读事件传递给events这个变量]
#    rsync -av --delete /app/java/ root@192.168.81.10:/backup/java_backup
##则将这个目录从/app/java/同步到/backup/java_backup目录下[通过命令同步到10这个服务器上]
#    echo "date +%F\ %T 出现事件$events" >> /var/log/rsync.log 2>&1
##打印时间日期,打印小时分钟秒出现这个事件的“动作”传递到日志文件中[打印日志]
#done   ##结束

[root@client1 ~]# vim inotify.sh
[root@client1 ~]# cat inotify.sh
#!/bin/bash
/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib,move /app/java | while read events; do
    rsync -av --delete /app/java/ root@192.168.81.10:/backup/java_backup
    echo "date +%F\ %T 出现事件$events" >> /var/log/rsync.log 2>&1
done
[root@client1 ~]# ls
anaconda-ks.cfg  inotify.sh  inotify-tools-3.13.tar.gz
[root@client1 ~]# chmod +x inotify.sh

 窗口1 :脚本在后台执行

[root@code ~]# vi inotify.sh 
[root@code ~]# nohup ./inotify.sh 
nohup: 忽略输入并把输出追加到"nohup.out"

窗口2:动态查看相关日志

[root@code ~]# tail -0f /var/log/rsync.log 
date +%F\ %T 出现事件/app/java/ CREATE,ISDIR jjjjj
date +%F\ %T 出现事件/app/java/ CREATE xx.txt
date +%F\ %T 出现事件/app/java/ ATTRIB xx.txt
2025/01/27 09:32:01 [16785] rsync on app/ from UNKNOWN (192.168.81.10)
2025/01/27 09:32:01 [16785] building file list
2025/01/27 09:32:02 [16785] sent 476 bytes  received 32 bytes  total size 0

窗口3:更改原目录内容

[root@code ~]# cd /app/java/
[root@code java]# ls
aa1  aa3  b1.txt      file2.java  file4.java  file6.java  file8.java  nohup.out
aa2  aa4  file1.java  file3.java  file5.java  file7.java  file9.java  test01
[root@code java]# touch cc.txt
[root@code java]# mkdir dd
[root@code java]# mkdir jjjjj
[root@code java]# touch xx.txt

改动文件内容时,事实查看日志信息,如下所示:

 

FTP文件传输协议 

 

 搭建ftp服务器

#基础环境:关闭防火墙,selinux,配置yum源,epel源
[root@server ~]# yum -y install vsftpd   ##安装
[root@server ~]# systemctl restart vsftpd
[root@server ~]# systemctl status vsftpd
[root@server ~]# ss -tunlp | grep 21   ##默认为被动模式,另一个为随机端口
tcp    LISTEN     0      32     [::]:21                 [::]:*                   users:(("vsftpd",pid=17090,fd=4))
#--在默认目录中写入内容,进入windows系统查看
[root@server pub]# echo "hello ftp" > /var/ftp/pub/a.txt
#安装vsftpd服务后,默认的上传和下载文件的目录在/var/ftp/pub/
[root@server pub]# cat /var/ftp/pub/a.txt 
hello ftp
  • windows访问 

windows直接访问ftp服务器 

默认对目录有查看和下载权限,不能上传和修改

使用windows的软件FlashFXP访问ftp服务器

  • linux访问

通过linux客户端命令行工具访问ftp服务器
ftp服务器配置文件

[root@client ~]# yum -y install lftp   ##安装客户端工具
#--下载离线包lftp
[root@server opt]# yum install yum-utils
[root@server opt]# yumdownloader --resolve lftp
#--安装离线包
[root@client ~]# yum localinstall *.rpm
#--通过lftp工具连接ftp服务器
[root@client ~]# lftp 192.168.81.130   
lftp 192.168.81.130:~> ls
drwxr-xr-x    2 0        0              19 Jan 27 10:04 pub
lftp 192.168.81.130:/> ls pub/
-rw-r--r--    1 0        0              10 Jan 27 10:04 a.txt
#--查看vsftpd相关的文件
/usr/share/doc/vsftpd-3.0.2/SECURITY/OVERVIEW
/usr/share/doc/vsftpd-3.0.2/SECURITY/TRUST
/usr/share/doc/vsftpd-3.0.2/SIZE
/usr/share/doc/vsftpd-3.0.2/SPEED
/usr/share/doc/vsftpd-3.0.2/TODO
/usr/share/doc/vsftpd-3.0.2/TUNING
/usr/share/doc/vsftpd-3.0.2/vsftpd.xinetd
/usr/share/man/man5/vsftpd.conf.5.gz
/usr/share/man/man8/vsftpd.8.gz
/var/ftp
/var/ftp/pub
[root@server ~]# rpm -ql vsftpd
#--配置文件内容
#anonymous_enable=YES   ##yes允许,默认允许匿名用户访问
#local_enable=YES     ##允许本地用户登录ftp
#write_enable=YES     ##开启写
#local_umask=022       ##本地umask码
#anon_upload_enable=YES  ##允许匿名用户上传文件
#anon_mkdir_write_enable=YES  ##允许匿名用户创建目录

 

 

 

  • vsftpd匿名用户使用 

开匿名用户上传文件
#vsftpd 默认情况下是开启匿名用户访问,并且只允许匿名用户查看根目录下的文件和下载及切换根目录,不允许其它操作
#anon_upload_enable=YES    ##允许匿名用户上传文件
#--配置允许匿名用户上传文件
[root@server ~]# vi /etc/vsftpd/vsftpd.conf
...
anon_upload_enable=YES   ##取消注释
[root@server ~]# systemctl restart vsftpd
#-赋予目录写权限
[root@server ~]# ll -d /var/ftp/pub/
drwxr-xr-x. 2 root root 19 1月  27 18:04 /var/ftp/pub/
[root@server ~]# chmod 777 /var/ftp/pub/
[root@server ~]# ll -d /var/ftp/pub/
drwxrwxrwx. 2 root root 19 1月  27 18:04 /var/ftp/pub/
windows上传文件成功

 开匿名用户创建目录/文件
#anon_mkdir_write_enable=YES   ##允许匿名用户创建文件夹
#--配置允许匿名用户上传文件
[root@server ~]# vi /etc/vsftpd/vsftpd.conf
...
anon_mkdir_write_enable=YES   ###取消注释
anon_upload_enable=YES   ##取消注释
[root@server ~]# systemctl restart vsftpd
 开启匿名用户删除修改文件
#anon_other_write_enable=YES   ##允许匿名删除修改文件
#--允许匿名删除修改文件
[root@server ~]# vi /etc/vsftpd/vsftpd.conf
...
anon_mkdir_write_enable=YES   ##取消注释
anon_upload_enable=YES   ##取消注释 
anon_other_write_enable=YES  ###添加一行
指定匿名用户的根目录

[默认匿名用户根目录在/var/ftp/pub/目录]

注意:设置了普通用户的根目录后,根目录权限不能超过755,如果超过,访问时就会提示输入用户名和密码,无法登录。只能在根目录(/data/anon)下创建一个共享目录并给 777权限进行文件共享。

#anon_root=/data/anon      ##修改匿名用户根目录为/data/anon
#--允许匿名删除修改文件
[root@server ~]# vi /etc/vsftpd/vsftpd.conf
...
anon_mkdir_write_enable=YES   ##取消注释
anon_upload_enable=YES   ##取消注释 
anon_other_write_enable=YES  ##添加一行
anon_root=/data/anon   ###添加一行
[root@server ~]# systemctl restart vsftpd
[root@server ~]# mkdir -p /data/anon
[root@server ~]# ll -d /data/anon
drwxr-xr-x. 2 root root 6 1月  27 19:54 /data/anon
尝试访问空文件夹(默认以匿名用户访问)

 

新建目录并赋权 
一般在根目录下新建目录,并赋予权限
[root@server ~]# mkdir /data/anon/test
[root@server ~]# chmod 777 /data/anon/test
[root@server ~]# ll -d /data/anon/test
drwxrwxrwx. 2 root root 6 1月  27 19:58 /data/anon/test
 尝试匿名用户访问

【此时可以创建目录,修改名称,上传,下载,删除

  • vsftpd本地用户使用

 

[root@client ~]# yum -y install lftp   ##安装客户端工具
#--下载离线包lftp
[root@server opt]# yum install yum-utils
[root@server opt]# yumdownloader --resolve lftp
#--安装离线包
[root@client ~]# yum localinstall *.rpm
#--通过lftp工具连接ftp服务器
[root@client ~]# lftp 192.168.81.130   
lftp 192.168.81.130:~> ls
drwxr-xr-x    2 0        0              19 Jan 27 10:04 pub
lftp 192.168.81.130:/> ls pub/
-rw-r--r--    1 0        0              10 Jan 27 10:04 a.txt
#--查看vsftpd相关的文件
/usr/share/doc/vsftpd-3.0.2/SECURITY/OVERVIEW
/usr/share/doc/vsftpd-3.0.2/SECURITY/TRUST
/usr/share/doc/vsftpd-3.0.2/SIZE
/usr/share/doc/vsftpd-3.0.2/SPEED
/usr/share/doc/vsftpd-3.0.2/TODO
/usr/share/doc/vsftpd-3.0.2/TUNING
/usr/share/doc/vsftpd-3.0.2/vsftpd.xinetd
/usr/share/man/man5/vsftpd.conf.5.gz
/usr/share/man/man8/vsftpd.8.gz
/var/ftp
/var/ftp/pub
[root@server ~]# rpm -ql vsftpd
#--配置文件内容
#anonymous_enable=YES   ##yes允许,默认允许匿名用户访问
#local_enable=YES     ##允许本地用户登录ftp
#write_enable=YES     ##开启写
#local_umask=022       ##本地umask码
#anon_upload_enable=YES  ##允许匿名用户上传文件
#anon_mkdir_write_enable=YES  ##允许匿名用户创建目录

[root@client ~]# rm -rf 1.sh
[root@client ~]# vi 1.sh
[root@client ~]# cat 1.sh
#vsftpd 默认情况下是开启匿名用户访问,并且只允许匿名用户查看根目录下的文件和下载及切换根目录,不允许其它操作
#anon_upload_enable=YES    ##允许匿名用户上传文件
#--配置允许匿名用户上传文件
[root@server ~]# vi /etc/vsftpd/vsftpd.conf
...
anon_upload_enable=YES   ##取消注释
[root@server ~]# systemctl restart vsftpd
#-赋予目录写权限
[root@server ~]# ll -d /var/ftp/pub/
drwxr-xr-x. 2 root root 19 1月  27 18:04 /var/ftp/pub/
[root@server ~]# chmod 777 /var/ftp/pub/
[root@server ~]# ll -d /var/ftp/pub/
drwxrwxrwx. 2 root root 19 1月  27 18:04 /var/ftp/pub/

[root@client ~]# vi 1.sh
[root@client ~]# 
[root@client ~]# cat 1.sh
[root@server ~]# useradd kefu
[root@server ~]# echo "1" | passwd --stdin kefu
更改用户 kefu 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@server ~]# mkdir /data/kefu
[root@server ~]# vim /etc/vsftpd/vsftpd.conf 
...
anonymous_enable=NO      ##禁止匿名用户登录
#anon_root=/data/anon     ##注释掉匿名用户根目录
local_root=/data/kefu     ##添加本地用户根目录为/data/kefu
chroot_local_user=YES     ##取消注释,允许本地用户访问
allow_writeable_chroot=YES   ##添加一行,允许本地用户写
local_enable=YES      ##添加一行,支持非匿名用户登录
write_enable=YES      ##添加一行,允许写操作
#anon_upload_enable=YES    ##注释之前的匿名用户相关操作
#anon_mkdir_write_enable=YES
#anon_other_write_enable=YES
[root@server ~]# setfacl -R -m u:kefu:rwx /data/kefu  #修改本地共享目录权限
[root@server ~]# getfacl /data/kefu
getfacl: Removing leading '/' from absolute path names
# file: data/kefu
# owner: root
# group: root
user::rwx
user:kefu:rwx
group::r-x
mask::rwx
other::r-x
[root@server ~]# systemctl restart vsftpd
尝试非匿名用户访问

 

linux客户端lftp工具的使用

chroot_local_user=YES  ##限制用户在根目录下活动
[root@client ~]# lftp -u kefu 192.168.81.130
口令:     ##输入为kefu用户设置的密码,这里为1
lftp kefu@192.168.81.130:~> ls           
drwxr-xr-x    2 1000     1000            6 Jan 27 12:59 dir
drwxr-xr-x    2 1000     1000          231 Jan 27 13:00 新建文件夹
lftp kefu@192.168.81.130:/> cd dir
lftp kefu@192.168.81.130:/dir> ls
lftp kefu@192.168.81.130:/dir> pwd   ##不显示根目录信息
ftp://kefu@192.168.81.130/dir
lftp kefu@192.168.81.130:/dir> exit
[root@server ~]# vim /etc/vsftpd/vsftpd.conf
...
#root_local_user=YES   ##注释掉先前配置
[root@server ~]# systemctl restart vsftpd
[root@client ~]# lftp -u kefu 192.168.81.130
口令: 
lftp kefu@192.168.81.130:~> ls           
drwxr-xr-x    2 1000     1000            6 Jan 27 12:59 dir
drwxr-xr-x    2 1000     1000          231 Jan 27 13:00 新建文件夹
lftp kefu@192.168.81.130:~/dir> pwd    ##可以显示根目录
ftp://kefu@192.168.81.130/%2Fdata/kefu/dir
lftp kefu@192.168.81.130:~/dir> 
#--在客户端创建初始环境
[root@client tmp]# ls
[root@client tmp]# touch {a..d}.txt
[root@client tmp]# mkdir file{1..5}
[root@client tmp]# echo "hello ftp" > file1/f1.txt
[root@client tmp]# echo "this test" > file2/f2.txt
[root@client tmp]# ls 
a.txt  b.txt  c.txt  d.txt  file1  file2  file3  file4  file5
[root@client tmp]# cat file1/f1.txt 
hello ftp
[root@client tmp]# cat file2/f2.txt 
this test
#--查看帮助
[root@client tmp]# lftp -u kefu 192.168.81.130
口令: 
lftp kefu@192.168.81.130:~> help         
    !<shell-command>                     (commands)
    alias [<name> [<value>]]             attach [PID]
    bookmark [SUBCMD]                    cache [SUBCMD]
    cat [-b] <files>                     cd <rdir>
    chmod [OPTS] mode file...            close [-a]
    [re]cls [opts] [path/][pattern]
    debug [<level>|off] [-o <file>]      du [options] <dirs>
    exit [<code>|bg]
    get [OPTS] <rfile> [-o <lfile>]      glob [OPTS] <cmd> <args>
    help [<cmd>]
    history -w file|-r file|-c|-l [cnt]  jobs [-v] [<job_no...>]
    kill all|<job_no>                    lcd <ldir>
    lftp [OPTS] <site>                   ln [-s] <file1> <file2>
    ls [<args>]                          mget [OPTS] <files>
#--上传文件
lftp kefu@192.168.81.130:~> put /tmp/a.txt 
lftp kefu@192.168.81.130:~> ls                  
-rw-r--r--    1 1000     1000            0 Jan 27 13:24 a.txt
drwxr-xr-x    2 1000     1000            6 Jan 27 12:59 dir
drwxr-xr-x    2 1000     1000          231 Jan 27 13:00 新建文件夹
lftp kefu@192.168.81.130:~> put /tmp/b.txt /tmp/c.txt 
Total 2 files transferred
lftp kefu@192.168.81.130:~> ls
-rw-r--r--    1 1000     1000            0 Jan 27 13:24 a.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:24 b.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:24 c.txt
drwxr-xr-x    2 1000     1000            6 Jan 27 12:59 dir
drwxr-xr-x    2 1000     1000          231 Jan 27 13:00 新建文件夹
#--删除文件
lftp kefu@192.168.81.130:~> rm a.txt b.txt c.txt
rm: Access failed: 550 Delete operation failed. (b.tx)
rm failed for 1 of 3 files
lftp kefu@192.168.81.130:~> ls
drwxr-xr-x    2 1000     1000            6 Jan 27 12:59 dir
drwxr-xr-x    2 1000     1000          231 Jan 27 13:00 新建文件夹
#--批量上传
lftp kefu@192.168.81.130:~> mput /tmp/*.txt
Total 4 files transferred
lftp kefu@192.168.81.130:~> ls
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 a.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 b.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 c.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 d.txt
drwxr-xr-x    2 1000     1000            6 Jan 27 12:59 dir
drwxr-xr-x    2 1000     1000          231 Jan 27 13:00 新建文件夹
#--下载文件,默认下载在登录lftp的目录
[root@client tmp]# cd /root
[root@client ~]# lftp -u kefu 192.168.81.130
口令: 
lftp kefu@192.168.81.130:~> get a.txt
get: a.txt: file already exists and xfer:clobber is unset
lftp kefu@192.168.81.130:~> exit
[root@client ~]# ls
1.sh  3.sh  anaconda-ks.cfg  a.txt
lftp kefu@192.168.81.130:~> get a.txt
get: /tmp/a.txt: 文件已存在
#--批量下载文件
[root@client ~]# lftp -u kefu 192.168.81.130
口令: 
lftp kefu@192.168.81.130:~> mget *.txt
mget: a.txt: file already exists and xfer:clobber is unset
Transfer of 1 of 4 files failed
lftp kefu@192.168.81.130:~> exit
您在 /var/spool/mail/root 中有新邮件
[root@client ~]# ls
1.sh  3.sh  anaconda-ks.cfg  b.txt  d.txt            rsync.sh
2.sh  4.sh  a.txt            c.txt  id_rsa_2048.pub
#--上传目录,将本地目录上传到远程服务器
[root@client ~]# lftp -u kefu 192.168.81.130
口令: 
lftp kefu@192.168.81.130:~> mirror -R /tmp/file1
Total: 1 directory, 1 file, 0 symlinks 
New: 1 file, 0 symlinks
10 bytes transferred
lftp kefu@192.168.81.130:~> ls
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 a.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 b.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 c.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 d.txt
drwxr-xr-x    2 1000     1000            6 Jan 27 12:59 dir
drwxr-xr-x    2 1000     1000           20 Jan 27 13:34 file1
lftp kefu@192.168.81.130:~> mirror -R /tmp/file3
Total: 1 directory, 0 files, 0 symlinks
lftp kefu@192.168.81.130:~> mirror -R /tmp/file4
Total: 1 directory, 0 files, 0 symlinks
lftp kefu@192.168.81.130:~> mirror -R /tmp/file5
Total: 1 directory, 0 files, 0 symlinks
lftp kefu@192.168.81.130:~> ls
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 a.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 b.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 c.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 d.txt
drwxr-xr-x    2 1000     1000            6 Jan 27 12:59 dir
drwxr-xr-x    2 1000     1000           20 Jan 27 13:34 file1
drwxr-xr-x    2 1000     1000            6 Jan 27 13:35 file3
drwxr-xr-x    2 1000     1000            6 Jan 27 13:35 file4
drwxr-xr-x    2 1000     1000            6 Jan 27 13:35 file5
#--下载目录,将远程服务器的目录下载到当前/opt/目录下
[root@client opt]# lftp -u kefu 192.168.81.130
口令: 
lftp kefu@192.168.81.130:~> mirror file1
Total: 1 directory, 1 file, 0 symlinks
New: 1 file, 0 symlinks
10 bytes transferred
lftp kefu@192.168.81.130:~> exit
[root@client opt]# ls
biji  file1
#--删除目录
[root@client opt]# lftp -u kefu 192.168.81.130
口令:
lftp kefu@192.168.81.130:~> rm -rf file1
rm ok, 2 files removed
#--创建目录
lftp kefu@192.168.81.130:~> mkdir test01
mkdir 成功, 建立 `test01'
lftp kefu@192.168.81.130:~> ls
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 a.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 b.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 c.txt
-rw-r--r--    1 1000     1000            0 Jan 27 13:27 d.txt
drwxr-xr-x    2 1000     1000            6 Jan 27 12:59 dir
drwxr-xr-x    2 1000     1000            6 Jan 27 13:35 file3
drwxr-xr-x    2 1000     1000            6 Jan 27 13:35 file4
drwxr-xr-x    2 1000     1000            6 Jan 27 13:35 file5
drwxr-xr-x    2 1000     1000            6 Jan 27 13:40 test01
lftp kefu@192.168.81.130:~> mkdir -p file01/fil02/file03
mkdir 成功, 建立 `file01/fil02/file03'
lftp kefu@192.168.81.130:~> ls file01/
drwxr-xr-x    3 1000     1000           20 Jan 27 13:41 fil02
lftp kefu@192.168.81.130:~> ls file01/fil02/
drwxr-xr-x    2 1000     1000            6 Jan 27 13:41 file03

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值