文章

Lsyncd 本地双硬盘实时同步文件目录

lsyncd本地目录同步,有多种工作模式可以选择,本地目录cp,本地目录rsync,远程目录rsyncssh。本文内容为本地目录的同步配置

需求:本服务器有kod网盘,同时对于网盘资料进行多硬盘同步备份

源文件夹/var/www/html/

备份位置/data/html

1.手动或定时同步

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@akkun ~]# yum install rsync -y

# 默认增量更新 添加--detele表示同步删除增加文件

[root@akkun ~]# rsync -avz --delete /var/www/html/ /data/html/

#定时任务

[root@akkun ~]# echo "\*/5 \* \* \* \* rsync -avz --delete /var/www/html/ /data/html/ >/dev/null 2>&1" > /var/spool/cron/root

#去除定时任务的mail邮件提醒

[root@akkun ~]# echo "unset MAILCHECK" >> /etc/profile

2.自动lsyncd监控同步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
[root@akkun ~]# yum install lua lua-devel

[root@akkun ~]# rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

[root@akkun ~]# yum install lsyncd rsync -y

#开启lsyncd服务

[root@akkun ~]# /etc/init.d/lsyncd start

#设置lsyncd服务自启动

[root@akkun ~]# chkconfig lsyncd on

#配置conf

[root@akkun ~]# cat > /etc/lsyncd.conf << EOF

----

-- User configuration file for lsyncd.

--

-- Simple example for default rsync, but executing moves through on the target.

-- create by sh

-- For more examples, see /usr/share/doc/lsyncd\*/examples/

-- 本次启用II

-- I. 本地目录同步,direct:cp/rm/mv。 适用:500+万文件,变动不大

-- sync {

-- default.direct,

-- source = "/tmp/src",

-- target = "/tmp/dest",

-- delay = 1

-- maxProcesses = 1

-- }

-- II. 本地目录同步,rsync模式:rsync

sync {

default.rsync,

source = "/var/www/html/",

target = "/data/html/",

excludeFrom = "/etc/rsync\_exclude.lst",

-- exclude = "a.txt",

rsync = {

binary = "/usr/bin/rsync",

archive = true,

compress = false,

-- bwlimit = 2000

}

}

EOF

至此本地目录同步备份配置完成

本文由作者按照 CC BY 4.0 进行授权