Linux_Rsync远程同步备份服务器

目录

Remote Sync

功能
1. 可以镜像保存整个目录树和文件系统
2. 很容易做到保持原来文件的权限,时间,属主,软硬连接等
3. 可使用rcp,ssh等方式来传输文件,也可直接通过socket来进行连接
4. 支持匿名传输
General use for remote backup and backup localhost file into remote host(auto-periodical execute)
Distinction of backup and copy:
1. backup:Update the data.
2. copy:Move all data to other space.
RSync可以实现增量备份,而且可以同步更新数据,实时备份。RSync主机同步网络YUM源,本地局域网同步RSync主机YUM源。

同步的类型

本地模式

将a目录内的文件通过rsync到另一个b目录
example:

rsync -av test /tmp

Attention:
a. /tmp/:不将tmp目录备份,只备份tmp目录下的内容
b. /tmp:将tmp目录及其以下的内容完全备份

远程模式

底层是使用SSH协议
example:

rsync -av /tmp root@GoalHostIP:/root

RSync列表模式

example:

rsync -a IP:cisco

RSync 服务模式:

Rsync同步源、SSH源 –> 备份文件的源主机

Setup RSync service

step1. Create RSync service configuration file by manual
vim /etc/rsyncd.conf

        #RSync configuration file
        #Welcome file
        motd file = /etc/rsyncd.motd
        read>list = yes
        uid=root
        gid=root
        use chroot = no
        max connections = 5
        log file = /var/log/rsyncd.log
        pid file = /var/run/rsyncd.pid
        lock file = /var/run/rsyncd/lock
        #Specify share directory
        [wwwroot]
        path = /var/www/html
        readonly = yes
        auth users = jmilk
        #user authentication file, store userName and password
        secrets file = /etc/rsyncd.db
        comment = rsync directory

step2. Create password file

echo "jmilk:fanguiju" >> /etc/rsyncd.db
chmod 600 /etc/rsyncd.db

step3. Start rsync service

rsync --deamon

How to use the rsync commands

rsync指令选项

    -av  同步并且显示详细信息
    -z     在传输备份是进行压缩
    --delete 将目的位置中有而源位置中没有的文件删除
    --password-file=/etc/server  指定存放密钥对的位置
    -H   保留硬链接

example:

#use rsync source:
rsync -avzH --delete backuper@:ip::wwwroot  /var/www/html  --> ::shareDirectory
#use ssh source in the client:
rsync -avzH root@RSyncServerIP:/syncDirectory    /localhostBackupDirecttory

SSH Source create Key Pair:

ssh-keygen -t rsa
ssh-copy-id root@RSyncServerIP

RSync Source create Key Pair:
vim /etc/rsyncd.conf

RSYNC_PASSWORD="pwd123"

RSync backup:

rsync -avzH -b --backup-dir=old root@SyncServerIP:/syncDirectory    /localhostBackupDirectory
        #old --> 只是备份有修改过的文件到该目录中,并且创建在本地指定的/localostBackupDirectory目录中
        #-b --> backup mode
rsync -avzH -b --backup-dir='date+ "%Y%M%B%H%M%S"' root@SyncServerIP:/syncDirectory    /localhostBackupDirectory #在每次更新后都生成一个只包含修改部分的备份文件
rsync -avzH -b --backup-dir='date+ "%Y%M%B%H%M%S"' --exclude=up root@SyncServerIP:/syncDirectory    /localhostBackupDirectory #将备份目录中包含up的文件或子目录排除不备份

inotify+rsync Real-time sync

inotify机制:监控文件系统的变化
Software:inotofy-tools(安装在RSyncServer)
inotify kernel parameter:

max_queue_events:监控队列大小
max_user_instances:最多监控例数
max_user_watches:每个实例最多的监控个数

Setup inotify

tar zxvf inotify-tools -C /usr/local
cd /usr/local/inotify-tools
./confugure && make && make install
inotifywait -mrq -e modify,create,move,delete  /data/ --exclude=/data/up/
        #wait 持续监控
        #-e     指定监控事件的类型
        #--exclude  过滤不想监控的目录或内容

BUG:编译安装的过程中可能会出现执行以上指令后出现: libinotifytools.so.o:cannot open shared object file error
解决办法:ln -S /usr/local/lib/libinotifytools.so.o /usr/lib64

原文地址:https://www.cnblogs.com/jmilkfan-fanguiju/p/11825203.html