centos8下的文件现步处理 — lsyncd

分类:Linux |

准备工作:

1. 在远端被同步的服务器上开启ssh无密码登录.

2. 安装服务  yum install rsync lsyncd asciidoc   或  dnf install rsync lsyncd asciidoc

3. 创建日志目录:  mkdir -p /srv/logs/sync/lsyncd

以下为参考的配置文件:

vim /etc/lsyncd.conf

----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
--
-- sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}
settings{
    logfile = "/srv/logs/sync/lsyncd/lsyncd.log",
    statusFile = "/srv/logs/sync/lsyncd/lsyncd.stat",
    statusInterval =1,
}
sync{
    default.rsync,
    source="/srv/www/xxx",
    target="10.1.1.1:/srv/www/xxx",
    excludeFrom = "/etc/lsyncd.exclude",
    init=false,
    rsync     = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        verbose   = true
    }
}

vim /etc/lsyncd.exclude

**runtime**
/data/logs
*/logs/*
**.swp**


systemctl enable lsyncd

systemctl start lsyncd

systemctl status lsyncd



其它:

错误:

sent 587860 bytes  received 74 bytes  391956.00 bytes/sec
total size is 634266  speedup is 1.08
Tue Jan  2 14:39:25 2018 Normal: Finished a list after exitcode: 0
Tue Jan  2 19:47:43 2018 Normal: --- TERM signal, fading ---
Tue Jan  2 19:50:23 2018 Error: Terminating since out of inotify watches.
Consider increasing /proc/sys/fs/inotify/max_user_watches
Sat Jan  6 11:21:33 2018 Normal: --- TERM signal, fading ---

表示文件监控超限,

cat /proc/sys/fs/inotify/max_user_watches

默认是   8192

echo 65535000 >  /proc/sys/fs/inotify/max_user_watches

修改:

vim /etc/sysctl.conf

添加:

# Controls the maximum inotify watches per user
fs.inotify.max_user_watches = 65535000

保存:

sysctl -p

重启服务:

systemctl restart lsyncd


1.1 lsyncd 简介

Lsyncd使用文件系统事件接口(inotify或fsevents)来监视对本地文件和目录的更改。Lsyncd将这些事件整理几秒钟,然后生成一个或多个进程以将更改同步到远程文件系统。

默认同步方法是rsync Lsyncd是一种轻量级的实时镜像解决方案。Lsyncd相对容易安装,不需要新的文件系统或块设备。Lysncd不会妨碍本地文件系统性能,可以通过配置文件实现细粒度的自定义。自定义操作配置甚至可以从头开始编写,从shell脚本到用Lua语言编写的代码。

1.2 环境准备

rsync服务端:默认就有rsync无需安装

lsyncd客户端:安装lsyncd软件


*/5 * * * *  /srv/crontab/chmodandchown.sh
find /srv/www/xxx -user root|xargs chmod 755
find /srv/www/xxx -user root|xargs chown www.www


30 2 * * * /bin/sh /srv_ext/backup/mysqlbackup.sh

#!/bin/bash
DBUSER=''        # 数据库用户名
DBPASSWD=''          #数据库密码,无密码为空
DBHOST='IP' #备份数据库地址
DBNAME=''      #备份数据库名字
DBDAY=17 #保留多少天的数据库备份
WEBDIR='/srv/www/xxx' ####等同网站根目录
BACKPATH='/srv_ext/backup'
DB_BACKUP_SQL=$DBNAME-$(date +%F_%H).sql
cd $BACKPATH/"$DATE"/
mysqldump -u$DBUSER -p$DBPASSWD -h$DBHOST $DBNAME --single-transaction --master-data=2 --default-character-set='utf8' -R -q --force >$DB_BACKUP_SQL
tar -zcvf $BACKPATH/"$DATE"/web$(date +%F_%H).tar.gz $WEBDIR >/dev/null 2>&1
crontab -uwww -l  > $BACKPATH/"$DATE"/crontab_www.md
crontab -uroot -l  > $BACKPATH/"$DATE"/crontab_root.md
zip "$DB_BACKUP_SQL".zip $DB_BACKUP_SQL
rm -rf $DB_BACKUP_SQL
##########################################################################3
# 删除之前的超期的备份文件
DATE=$(date +%F)        #固定时间格式,请勿修改
DBDEL=$(date -d"$DBDAY days ago" +%Y-%m-%d)     #固定时间格式,请勿修改
test -d $BACKPATH/"$DATE" || mkdir -p $BACKPATH/"$DATE"
rm -rf $BACKPATH/$DBDEL

数据库添加主从同步: 

grant replication slave on *.* to 'mysync'@'%' identified by 'xxx';
show master status;
change master to master_host='10.0.xx.xx',master_user='mysync',master_password='xxx',master_log_file='mysql-bin.000028',master_log_pos=618102869;