侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

系统文件备份,rsync

2023-12-11 星期一 / 0 评论 / 0 点赞 / 117 阅读 / 14696 字

1 安装备份工具软件 rsync [root@myvm soft]# yum install rsync.x86_64 2 工作模式 shell模式:就是执行指令,也叫本地模式 远程sh

1 安装备份工具软件 rsync

    [root@myvm soft]# yum install rsync.x86_64

 2 工作模式

    shell模式:就是执行指令,也叫本地模式
            远程shell模式,可以利用ssh协议作为数据传输的承载
            列表模式:仅仅列出源的内容 -nv
            服务器模式:rsync作为一个守护进程,可以接收客户端的数据传输请求,可以进行数据的获取和上载

参数:
                -n:测试模式对于命令的执行效果不缺定一定要使用此参数进行测试之后,在运行
                -v:显示详细过程信息
                -r:递归
                -q:静默模式
                -c:checksum文件校验
                -a:归档,保留文件属性
                -p:保留权限
                -t:保留时间戳
                -l:保留符号链接
                -g:保留属组
                -o:保留属主
                -D:保留设备文件
                -e ssh:使用ssh歇息作承载数据传输
                -z:压缩传输数据
                --progress:显示进度
                --stats:显示状态

                --exclude --exclude="filename|dirname"
                          --exclude="*.txt"
                          排除此指定内容之外的文件或目录都同步过去
                --delete 
                    保证源目录和目的目录里面内容的一致性,如果目的目录中自己多添加了会被删除


                rsync etc user@host:dst -rv
                rsync etc/ user@host:dst -rv
                以上两个指令执行的结果会不一样
                没有加上目录后面的斜线会同步目录本身过去
                加上目录后面的斜线之后会同步目录下面的所有文件或目录

    3 常用备份例子

        常用到的就是服务器模式,需要两台以上的机器,一个做服务器,其他的做客户机。

注意:这里所谓的服务器是只在备份这个动作中作为存储备份脚本的机器,客户机是指被备份的机器

   3.1 设置服务器

        安装软件rsync  xinetd

[root@myvm soft]# yum install rsync.x86_64 xinetd

        配置xinetd 的配置文件

[root@myvm soft]# vim /etc/xinetd.d/rsync# default: off# description: The rsync server is a good addition to an ftp server, as it /#       allows crc checksumming etc.service rsync{        disable = no                         # 原来是yes ,改为no        flags           = IPv6        socket_type     = stream        wait            = no        user            = root        server          = /usr/bin/rsync        server_args     = --daemon        log_on_failure  += USERID}

编写rsync的配置文件

[root@myvm soft]# vim /etc/rsyncd.confuid=nobodygid=nobodyuse chroot=nomax connections=5strict modes=yespid file=/var/run/rsyncd.pidlog=/var/log/rsyncd.log[data]path=/test                   # 设置自己服务器定共享目录ignore errors=yesread only=nowrite only=nohosts allow=192.168.125.0/24  # 设置可以接收客户机定地址或者地址段hosts deny = * list=falseuid=rootgid=rootauth users=tomsecrets file=/etc/rsyncd.passwd 

密码文件
                        vim /etc/rsyncd.passwd
                        tom:123456

守护进程启动
                        /etc/init.d/xinetd restart
                        netstat -anutlp|grep 873

客户端测试

[root@myvm tmp]# rsync -avxzRP /tmp/tmp [email protected]::dataPassword: sending incremental file list/tmp//tmp/tmp//tmp/tmp/10.txt           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1/4)sent 132 bytes  received 35 bytes  47.71 bytes/sectotal size is 0  speedup is 0.00上面刚刚建的/tmp/tmp/10.txt 推送到服务器上去了

总结:
                rsync -avzP  --delete /tmp/test [email protected]::data 拷贝test目录过去
                rsync -avzP  --delete /tmp/test/ [email protected]::data 老被test目录下的所有内容 不包含test目录本身
                rsync -avzRP  --delete /tmp/test [email protected]::data 会保留tmp/test目

客户端验证:

#客户端推送文件[root@myvm2 tmp]# rsync -avzRP /tmp [email protected]::dataPassword: sending incremental file list/tmp//tmp/1.txt          12 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)/tmp/yum.log           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=1/4)/tmp/.ICE-unix/sent 191 bytes  received 54 bytes  70.00 bytes/sectotal size is 12  speedup is 0.05# 服务端文件检查[root@myvm test]# lltotal 4drwxrwxrwt 3 root root 4096 Nov 15 22:29 tmp

 

inotify-tools:
                是为linux下inotify文件监控工具提供的一套c接口的开发接口库函数,同时还提供一些列的命令行工具,这些工具可以用来监控文件系统的事件,只要求linux内核支持inotify,inotifywait监控文件或目录的变化,inotifywatch统计文件访问次数
                ls /proc/sys/fs/inotify
                安装inotify-tools
                下载
                解压inotify-tools-3.14.tar.gz
                tar -xvf inotify-tools-3.14.tar.gz
                cd inotify-tools-3.14
                ./configure --prefix=/usr
                make
                make install

                ln -s /usr/lib/libinotifytools.so.0 /usr/lib64/
                inotifywait
                -h:帮助
                @:排除不需要监视的文件或目录
                -fromfile:从文件中读取需要监视的文件,排除的文件以@开头
                -m:接收到一个事件而不退出,无限期执行,默认是接收到一个事件就退出
                -d:和-m一样,还需要指定-outfile也意味着使用-syslog
                -o:输出事件到一个文件
                -s:输出错误信息到系统日志
                -r:监控目录下所有子目录
                -q:指定一次不会输出详细信息,指定二次除了致命错误不会输出任何信息
                -exclude:正则匹配要排除的文件
                -excludei:正则匹配要排除的文件忽略大小写
                -t:设置超时时间,0表示无限期执行下去
                -e:指定监控事件
                -c:输出csv格式
                -timefmt:时间格式化可以使用-format中%T格式
                -format:
                    %w:表示发生事件的目录
                    %f:表示发生事件的文件
                    %e:表示发生的事件
                    %Xe:事件以X分割
                    %T:-timefmt定义的时间格式

                inotifywatch:
                -h
                @
                -v输出详细信息
                -fromfile
                -z:输出表格的列和行
                -exclude
                -excludei
                -r
                -t
                -e
                -a:指定事件以升序排列
                -d:降序排列
                可监听事件:
                    access
                    modify
                    attrib
                    close_nowrite
                    close
                    open
                    moved_to
                    moved_from
                    move
                    create
                    delete
                    
    要监控/tmp/test目录及其内部所有文件上发生的create,delete,modify,close_write事件,则使用如下命令
    inotifywait -rm --timefmt '%Y-%m-%d %H:%M' --format '%T %w %e %f' -e create,delete,modify,close_write /tmp/test
    inotifywatch -vzr -e create,delete,modify,close_write,access,attrib /tmp/test
    inotifywatch -vzr -t 60 -e create,delete,modify,close_write,access,attrib /tmp/test

通过inotify+rsync进行实时有触发条件的同步
      前提需要安装inotify-tools和rsync的服务器模式
      #!/bin/bash
      #
      #
       
       inotifywait -rm --timefmt '%Y-%m-%d %H:%M' --format '%T %w  %f' -e     create,delete,modify,close_write,attrib /www|while read DATE TIME DIR FILE;do
      FILECHANGE=${DIR}${FILE}
      cd /www/
      rsync -avzRP --delete --progress --stats --password-file=/etc/r    syncd.passwd * --exclude-from="/var/log/rule.txt" [email protected]::data &
      echo "at ${TIME} on ${DATE} file $FILECHANGE was backup via rstnc" >> /var/log/rule.txt
      done

    为了让sh inotify.sh一直执行的方式
        supervisor推荐
        screen
        nohup


2.注意备份的时候是否需要打包压缩,是否需要本地异地都有备份,最好给备份文件加上一个标识以便还原的时候方便区分,根据实际情况考虑
编辑shell脚本:

#!/bin/bash### emai:[email protected]# 王紫龙## inotifywait -rm --timefmt '%Y-%m-%d %H:%M' --format '%T %w  %f' -e     create,delete,modify,close_write,attrib /www|while read DATE TIME DIR FILE;doFILECHANGE=${DIR}${FILE}cd /www/rsync -avzRP --delete --progress --stats --password-file=/etc/rsyncd.passwd * --exclude-from="/var/log/rule.txt" [email protected]::data &echo "at ${TIME} on ${DATE} file $FILECHANGE was backup via rstnc" >> /var/log/rule.txtdone

上面脚本中监控/www 目录

如果/www 中有任何变动,则/www目录下的所有文件都听不到服务器共享目录下

 

广告 广告

评论区