Rsync+Inotify实现自动同步清华大学yum源到本地
本篇介绍Rsync+Inotify实现自动同步清华大学yum源到本地,需要同步的目录地址:https://mirrors.tuna.tsinghua.edu.cn/centos/7/os/x86_64/
1、安装Rsync服务
[root@myzdl ~]# yum install rsync
[root@myzdl ~]# rsync --list-only mirrors.tuna.tsinghua.edu.cn::centos/7/os/x86_64/
+==================================================+
| Tsinghua University |
| -= Open Source Mirror Site =- |
+--------------------------------------------------+
| Welcome to Tsinghua Open Source Mirror Site. |
| |
| This mirror is also available on HTTP. |
| HTTP [https://mirrors.tuna.tsinghua.edu.cn] |
| RSYNC [rsync://mirrors.tuna.tsinghua.edu.cn] |
| |
| If you have any issues, please contact us |
| via GitHub @ https://github.com/tuna/issues |
| or IRC at #tuna on freenode |
| or Fishroom at https://fishroom.tuna.moe/ |
| |
| Supported by |
| Tsinghua Information Technology Center |
| Maintained by |
| Tsinghua University TUNA Association |
| Service Provided by |
| nanomirrors |
| This storage server is sponsored by |
| Megvii Technology Inc. |
| |
| TUNA acknowledges the contribution from Megvii |
| to open source software and communities |
| |
+==================================================+
drwxr-xr-x 15 2020/04/22 18:58:47 .
-rw-r--r-- 29 2020/04/21 01:55:44 .discinfo
-rw-r--r-- 353 2020/04/21 02:00:59 .treeinfo
-rw-rw-r-- 14 2020/04/22 01:15:06 CentOS_BuildTag
-rw-rw-r-- 227 2017/08/30 22:33:06 EULA
-rw-rw-r-- 18,009 2015/12/10 06:35:46 GPL
-rw-rw-r-- 1,690 2015/12/10 06:35:46 RPM-GPG-KEY-CentOS-7
-rw-rw-r-- 1,690 2015/12/10 06:35:46 RPM-GPG-KEY-CentOS-Testing-7
drwxr-xr-x 3 2020/04/21 02:00:13 EFI
drwxr-xr-x 3 2020/04/21 02:00:13 LiveOS
drwxr-xr-x 10,072 2020/04/22 18:58:47 Packages
drwxr-xr-x 5 2020/04/21 02:00:49 images
drwxr-xr-x 11 2020/04/22 18:58:47 isolinux
drwxr-xr-x 12 2020/04/22 07:37:50 repodata
[root@myzdl ~]#
2、安装Inotify服务
inotify可以实时监控目录文件,当文件目录发生变化,则触动rsync同步文件。
inotify-tool有两个程序,分别是inotifywait和inotifywatch。
[root@myzdl ~]# yum install inotify-tools
[root@myzdl ~]# vim inotify.sh
#!/bin/bash
#This is rsync script on inotify.
export PATH=/bin:/usr/bin:/usr/local/bin
Desdir=mirrors.tuna.tsinghua.edu.cn::centos/7/os/x86_64/
Localdir=/var/ftp/Tsinghua/
inotifywait -mrq --timefmt '%y-%m-%d %H:%M' --format '%T %w%f %e' --event modify,create,move,delete,attrib, $Localdir | while read line
do
echo "$line" > /var/log/inotify.log 2>&1
/usr/bin/rsync -avz --delete --progress $Desdir $Localdir >>/var/log/sync.log 2>&1
done &
[root@myzdl ~]# mkdir .script
[root@myzdl ~]# mv inotify.sh .script/
[root@myzdl ~]# echo “/root/.script/inotify.sh” >>/etc/rc.local
[root@myzdl ~]# chmod o+x /etc/rc.d/rc.local
#同步成功如下,10G左右
[root@myzdl ~]# du -h /var/ftp/Tsinghua/*
4.0K /var/ftp/Tsinghua/CentOS_BuildTag
2.5M /var/ftp/Tsinghua/EFI/BOOT/fonts
8.3M /var/ftp/Tsinghua/EFI/BOOT
8.3M /var/ftp/Tsinghua/EFI
4.0K /var/ftp/Tsinghua/EULA
20K /var/ftp/Tsinghua/GPL
60M /var/ftp/Tsinghua/images/pxeboot
636M /var/ftp/Tsinghua/images
60M /var/ftp/Tsinghua/isolinux
491M /var/ftp/Tsinghua/LiveOS
9.0G /var/ftp/Tsinghua/Packages
29M /var/ftp/Tsinghua/repodata
4.0K /var/ftp/Tsinghua/RPM-GPG-KEY-CentOS-7
4.0K /var/ftp/Tsinghua/RPM-GPG-KEY-CentOS-Testing-7
[root@myzdl ~]#
[root@myzdl ~]# ls /var/ftp/Tsinghua/Packages/ | wc
10070 10070 401037