rsync命令及單向實時同步部署

rsync命令

概述

  • 數據鏡像備份工具,支持快速完全備份和增量備份的工具,支持本地複製,遠程同步等,類似於scp命令;
  • 同步之前需要先登錄進行用戶身份認證,一般使用兩種協議進行同步:ssh協議和rsync協議
特性
  • 能更新整個目錄樹和文件系統
  • 有選擇性的保留符號鏈接,硬鏈接,文件屬性,權限,設備以及時間等;
  • 安裝時無特殊權限要求
  • 傳輸多個文件時效率更高
  • 能用ssh或自定義端口作爲傳入入口端口
工作原理
  • 兩個概念:原地址src,目標地址dst,以及以哪一方爲基準。
  • ssh登錄驗證使用ssh協議作爲基礎,root用戶身份認證,然後進行數據同步
  • rsync登錄驗證:使用rsync 協議進行用戶身份認證(非系統用戶),然後進行數據同步
  • 數據同步方式:推送(上傳)、拉取(下載)
數據備份 拉下載
數據恢復 推上傳
NFS服務器/filesrc
rsync服務器/filedst
rsync服務器/filedst
NFS服務器/filesrc

實驗演示

1、ssh協議數據同步:將NFS服務器數據備份到rsync服務器

下行同步(下載)
格式:
# rsync -avz 目標地址:/目標目錄/* /本地目錄
示例:
[root@yzl2 ~]# rsync -avz [email protected]:/filesrc/* /filedst/
...
-a: 歸檔模式,遞歸併保留對象屬性
-v: 顯示同步過程
-z: 在傳輸文件時進行壓縮
上行同步(上傳)
格式:
# rsync -avz /本地目錄/* 服務器地址:/服務器目錄
示例:
[root@yzl2 filedst]# rsync -avz /filedst/* [email protected]:/filesrc/
注意:
# 生產環境儘量使用單獨創建用戶,避免權限溢出
創建用來做數據同步的用戶,並給予用戶對目錄的相應權限,一般使用ACL設置權限
[root@yzl2 filedst]# useradd t1
[root@yzl2 filedst]# setfacl -m u:t1:rwx /filedst
[root@yzl2 filedst]# getfacl /filedst/
getfacl: Removing leading '/' from absolute path names
# file: filedst/
# owner: root
# group: root
user::rwx
user:t1:rwx
group::r-x
mask::rwx
other::r-x

2、rsync協議數據同步(單向同步)

搭建rsync服務
  • 創建主配置文件(/etc/rsyncd.conf)
[root@yzl1 etc]# vim rsyncd.conf
    address=192.168.154.128			# rsync服務綁定IP
    port 873						# 默認服務端口873
    log file = /var/log/rsyncd.log	# 日誌文件位置
    pid file = /var/run/rsyncd.pid	# 進程號文件位置
    [web]							# 共享名:用來連接是寫在url上的
      comment = web directory backup	# 共享描述話語 
      path = /filesrc				# 實際共享目錄
      read only = no				# 是否僅允許只讀
      dont compress = *.gz *.bz2	# 哪些文件類型不進行壓縮
      auth users = c1				# 登錄用戶名(非系統用戶,需自行創建)
      secrets file = /etc/rsyncd_users.db # 認證所需賬戶密碼文件(需自行創建-同上)
  • 創建認證所需賬戶密碼文件
[root@yzl1 etc]# vim /etc/rsyncd_users.db
	c1:123456
[root@yzl1 etc]# chmod 600 /etc/rsyncd_users.db 	# 必須修改權限,否則登錄報錯
  • 啓動服務
[root@yzl1 etc]# rsync --daemon
[root@yzl1 etc]# ss -antulp |grep 873
tcp    LISTEN     0      5      192.168.154.128:873                   *:*                   users:(("rsync",pid=6031,fd=3))
  • 設置映射用戶對共享目錄有權限®
[root@yzl1 filesrc]# setfacl -m u:nobody:rwx /filesrc
下行同步(下載)/ 上行同步(上傳)
格式:
rsync -avz rsync://用戶名@服務器地址/共享模塊名  /本地目錄
示例:
[root@yzl2 filedst]# rsync -avz rsync://c1@yzl1/web /filedst/		# 已經寫好/etc/hosts文件
拓展:
# --delete 刪除本地比服務器多出來的文件(即源地址沒有,但目標地址裏多餘)
[root@yzl2 filedst]# rsync -avz --delete rsync://c1@yzl1/web /filedst/

拓展:rsync協議免密登錄
  • export RSYNC_PASSWORD=虛擬用戶密碼(客戶端生成)
[root@yzl1 filedst]# export RSYNC_PASSWORD="123456" 

3、rsync+inotify實時同步

  • 定期同步缺點:執行備份時間不固定,延期明顯,實時性差;當同步源長期不變時,定期同步會浪費資源
  • 實時同步優點:實時性!節省資源。
inotify簡介
  • 監控文件系統,及時嚮應用程序相應的事件警告,要求內核2.6.13以上

  • 兩個監控命令:

    • inotifywait:用於持續監控,實時輸出結果(常用)
    • inotifywatch:用於短期監控,任務完成後再出結果

    https://sourceforge.net/projects/inotify-tools/下載鏈接

inotify部署
[root@yzl1 ~]# yum -y install gcc*
[root@yzl1 ~]# tar -xvf inotify-tools-3.14.tar.gz
[root@yzl1 ~]# cd inotify-tools-3.14
[root@yzl1 inotify-tools-3.14]# ./configure && make && make install
inotifywait命令格式
格式:
inotifywait -mrq -e 監控動作1,監控動作2  /監控目錄  & #放入後臺
示例:
[root@yzl1 inotify-tools-3.13]# inotifywait -wrq -e create,modify,delete /filesrc/
# -m: 時鐘保持事件監聽狀態
# -r: 遞歸查詢目錄
# -q: 只打印監控事件的信息
監控動作: modify,create,attrib(權限),move,delete

[root@localhost filesrc]# inotifywait -mrq -e create,delete,modify /filesrc
/filesrc/ CREATE 1.txt
/filesrc/ DELETE 1.txt

利用rsync+inotifywait結合腳本實現單向實時同步
[root@localhost filesrc]# vim ~/src.sh
    #!/bin/bash
    a="inotifywait -mrq -e create,delete /filesrc"
    b="rsync -avz /filesrc/* [email protected]:/filedst"
    $a | while read directory event file	# 固定寫法,while判斷是否接收到監控記錄
    do
            $b
    done
# 注意:用戶登錄時要求免密碼驗證!!!
    
[root@localhost filesrc]# bash ~/src.sh &
[2] 10729
[root@localhost filesrc]# touch t2.sh
[root@localhost filesrc]# sending incremental file list
t1.sh
t2.sh

sent 167 bytes  received 56 bytes  148.67 bytes/sec
total size is 84  speedup is 0.38

  • 後臺執行腳本,服務器在filesrc目錄下添加文件時,屏幕能實時輸出修改信息,並且客戶端同步實時數據備份
  • 注意需要關閉防火牆和selinux
拓展:調整inotify監控的文件數量
調整inotify內核參數(/etc/sysctl.conf)
mak_queue_events 監控隊列大小
mak_user_instances 最多監控實例數
max_user_watches 每個實例最多監控文件數

問題:

  • 段錯誤(吐核)
[root@yzl2 inotify-tools-3.13]# inotifywait -wrq -e create,modify,delete /filedst/
段錯誤(吐核)
產生段錯誤的原因主要有:
解引用空指針
訪問不可訪問的內存空間(如內核空間)
訪問不存在的內存地址
試圖寫一個只讀內存空間(如代碼段)
棧溢出(函數遞歸調用)
使用未初始化的指針(定義時沒有初始化或者已經回收)

[root@yzl1 filesrc]# ulimit -a
core file size          (blocks, -c) 0		# 此處值爲0,表示不允許吐核
data seg size           (kbytes, -d) unlimited
...
[root@yzl1 filesrc]# ulimit -c unlimited	# 修改大小爲無限制
[root@yzl1 filesrc]# ulimit -a          
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
...
再執行inotifywait,再次吐核。
之後 ls 就會出現core.8966文件,數字後綴不同機器會不一樣,這個不用在意。這個就是核心轉儲文件,我們成功通過操作查看到了。
[root@yzl1 filesrc]# ls
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt  core.8966  test

# gdb  可執行文件名 + 核心轉儲文件名
[root@yzl1 filesrc]# gdb inotifywait core.8966 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-94.axs7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redflag-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /usr/local/bin/inotifywait...done.
[New LWP 8966]
Core was generated by `inotifywait -mrq -e create,delete /filesrc/'.
Program terminated with signal 11, Segmentation fault.
#0  0x00007f9456421b9f in __libc_start_main (main=0x401430 <main>, 
    argc=5, argv=0x7ffead55f0d8, init=<optimized out>, 
    fini=<optimized out>, rtld_fini=<optimized out>, 
    stack_end=0x7ffead55f0c8) at libc-start.c:239
239		    afct->preinit (&head->l_audit[cnt].cookie);
Missing separate debuginfos, use: debuginfo-install glibc-2.17-157.axs7.1.x86_64
(gdb) bt
#0  0x00007f9456421b9f in __libc_start_main (main=0x401430 <main>, 
    argc=5, argv=0x7ffead55f0d8, init=<optimized out>, 
    fini=<optimized out>, rtld_fini=<optimized out>, 
    stack_end=0x7ffead55f0c8) at libc-start.c:239
#1  0x0000000000401af4 in _start ()

[root@yzl1 ~]# find / -type f -name libc-start.c
/root/glibc-2.18/csu/libc-start.c
/root/glibc-2.18/sysdeps/unix/sysv/linux/powerpc/libc-start.c
[root@yzl1 ~]# vim +239g /root/glibc-2.18/csu/libc-start.c
...
236       for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
237         {
238           if (afct->preinit != NULL)
239             afct->preinit (&head->l_audit[cnt].cookie);
240 
241           afct = afct->next;
242         }
...
  • 這裏出現的這個問題沒有解決,但是總算是找到出錯原因了,7.3系統默認的glibc版本庫是2.17,因爲之前做測試需要,臨時升級到2.18,導致glibc庫在升級過程中修改了內置參數,此處出現吐核錯誤,我目前的技術水平暫時無法解決這個吐核錯誤,但是將glibc版本恢復到默認的2.17之後,此問題可以避免了!
  • 另外升級glibc到2.18,可能會導致ssh-copy-id命令也丟失(不完全測試),升級慎用。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章