[root@nfs01 sersync]# vim /server/scripts/inotify.sh
#!/bin/bash
inotifywait -mrq /data --format "%w%f" -e create,delete,moved_to,close_write|\
while read line
do
rsync -az --delete /data/ rsync_backup@172.16.1.41::nfsbackup --password-
file=/etc/rsync.password
done
脚本说明:
for循环会定义一个条件,当条件不满足时停止循环
while循环:只要条件满足就一直循环下去
2.3.4 对脚本进行优化
#!/bin/bash
Path=/data
backup_Server=172.16.1.41
/usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data | while read line
do
if [ -f $line ];then
rsync -az $line --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
else
cd $Path &&\
rsync -az ./ --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
fi
done
在/data 目录先创建6个文件
[root@nfs01 data]# sh /server/scripts/inotify.sh &
[root@nfs01 data]# touch {1..6}.txt
在backup服务器上,已经时候同步过去了6个文件。
[root@backup ~]# ll /nfsbackup/
total 8
-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 1.txt
-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 2.txt
-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 3.txt
-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 4.txt
-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 5.txt
-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 6.txt
2.5 利用while循环语句编写的脚本停止方法(kill)
01. ctrl+z暂停程序运行,kill -9杀死
02. 不要暂停程序,直接利用杀手三剑客进行杀进程
说明:kill三个杀手不是万能的,在进程暂停时,无法杀死;kill -9 (危险)
2.5.1 查看后台都要哪些程序在运行
[root@nfs01 data]# jobs
[1]+ Running sh /server/scripts/inotify.sh &
2.5.2 fg将后台的程序调到前台来
[root@nfs01 data]# fg 1
sh /server/scripts/inotify.sh
2.6 进程的前台和后台运行方法:
fg -- 前台
bg -- 后台
2.6.1 脚本后台运行方法
01. sh inotify.sh &
02. nohup sh inotify.sh &
03. screen实现脚本程序后台运行
sh /server/scripts/inotify.sh &
nohup
nohup sh inotify.sh &
2.7 screen实现脚本程序后台运行 2.7.1 经过yum查找发现screen命令属于screen包
[root@test ~]# yum provides screen
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.7 kB 00:00
epel | 4.3 kB 00:00