rsync備份服務(上)

第1章 Rsync備份服務開篇介紹

1.1 Rsync介紹

1.1.1 什麼是rsync服務

Rsync是一款開源的、快速的、多功能的、可實現全量及增量的本地或遠程數據同步備份的優秀工具。而且能夠不進行改變原有數據的屬性信息,實現數據的備份遷移特性。Rsync軟件適用於Unix/Linux/window等多種操做系統平臺。html

  rsync服務軟件官方連接:http://www.samba.org/ftp/rsync/rsync.htmllinux

 

  man rsync 算法

  NAME   rsync a fast, versatile, remote (and local) file-copying toolshell

 

提示信息:vim

man  rsync  查看客戶端安全

man  rsyncd.conf 查看服務端配置bash

1.2 什麼是全量,什麼是增量

   全量複製:本地全部數據都進行傳輸複製,無論對端服務器是否有相同的數據,若是有進行覆蓋服務器

   增量複製:只備份同步變化的數據信息,對端服務器已經存在的數據,不進行傳輸複製ssh

1.3 Rsync命令使用介紹

1.3.1 rsync軟件 == scp命令

  準備測試環境socket

[root@backup ~]# mkdir /wuhuang

[root@backup ~]# touch /wuhuang/wuhuang-file{1..5}

[root@backup ~]# ls /wuhuang/

wuhuang-file1  wuhuang-file2  wuhuang-file3  wuhuang-file4  wuhuang-file5

 scp命令使用

    -r   表示遞歸傳輸數據

   -p   表示數據屬性信息不變

遠程傳輸文件:

[root@backup ~]# scp -rp /wuhuang/wuhuang-file1 10.0.0.31:/tmp

root@10.0.0.31's password:

wuhuang-file1                                  100%    0     0.0KB/s   00:00    

驗證

[root@nfs01 ~]# ll /tmp/

total 0

-rw-r--r--  1 root root 0 Jan 22 17:43 wuhuang-file1

遠程傳輸目錄:

[root@backup ~]# scp -rp /wuhuang/ 10.0.0.31:/tmp

root@10.0.0.31's password:

wuhuang-file5                                  100%    0     0.0KB/s   00:00    

wuhuang-file3                                  100%    0     0.0KB/s   00:00    

wuhuang-file4                                  100%    0     0.0KB/s   00:00    

wuhuang-file2                                  100%    0     0.0KB/s   00:00    

wuhuang-file1                                  100%    0     0.0KB/s   00:00    

驗證

[root@nfs01 ~]# ll /tmp/

total 4

drwxr-xr-x  2 root root 4096 Jan 22 17:43 wuhuang

-rw-r--r--  1 root root    0 Jan 22 17:43 wuhuang-file1

rsync命令使用:

    -r   表示遞歸傳輸數據

   -p   表示數據屬性信息不變

遠程傳輸文件:

[root@backup ~]# rsync -rp /wuhuang/wuhuang-file2 10.0.0.31:/tmp

The authenticity of host '10.0.0.31 (10.0.0.31)' can't be established.

RSA key fingerprint is 57:3f:64:68:95:4d:99:54:01:33:ab:47:a0:72:da:bf.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '10.0.0.31' (RSA) to the list of known hosts.

root@10.0.0.31's password:

驗證

[root@nfs01 ~]# ll /tmp/

total 0

-rw-r--r--  1 root root 0 Jan 22 18:01 wuhuang-file2

遠程傳輸目錄:

[root@backup ~]# rsync -rp /wuhuang/ 10.0.0.31:/tmp

root@10.0.0.31's password:

[root@nfs01 ~]# ll /tmp/

total 0

-rw-r--r--  1 root root 0 Jan 22 18:09 wuhuang-file1

-rw-r--r--  1 root root 0 Jan 22 18:09 wuhuang-file2

-rw-r--r--  1 root root 0 Jan 22 18:09 wuhuang-file3

-rw-r--r--  1 root root 0 Jan 22 18:09 wuhuang-file4

-rw-r--r--  1 root root 0 Jan 22 18:09 wuhuang-file5

說明:若是傳輸目錄時,目錄名稱後面有/(/wuhuang/), 表示將目錄下面的數據內容進行傳輸

[root@backup ~]# rsync -rp /wuhuang 10.0.0.31:/tmp

root@10.0.0.31's password:

[root@nfs01 ~]# ll /tmp/

total 4

drwxr-xr-x  2 root root 4096 Jan 22 18:10 wuhuang

-rw-r--r--  1 root root    0 Jan 22 18:09 wuhuang-file1

-rw-r--r--  1 root root    0 Jan 22 18:09 wuhuang-file2

-rw-r--r--  1 root root    0 Jan 22 18:09 wuhuang-file3

-rw-r--r--  1 root root    0 Jan 22 18:09 wuhuang-file4

-rw-r--r--  1 root root    0 Jan 22 18:09 wuhuang-file5

說明:若是傳輸目錄時,目錄名稱後面沒有/(/wuhuang),表示將目錄自己以及目錄下面的內容進行傳輸

1.3.2 rsync軟件 == cp命令

cp命令本地複製數據

[root@backup ~]# cp -a /wuhuang/wuhuang-file1 /tmp

[root@backup ~]# ll /tmp/

total 0

-rw-r--r--  1 root root 0 Jan 22 17:59 wuhuang-file1

rsync命令本地複製數據

[root@backup ~]# rsync -a /wuhuang/wuhuang-file2 /tmp/

[root@backup ~]# ll /tmp/

total 0

-rw-r--r--  1 root root 0 Jan 22 17:59 wuhuang-file1

-rw-r--r--  1 root root 0 Jan 22 17:59 wuhuang-file2

1.3.3 rsync軟件 == rm命令

[root@backup ~]# ll /tmp/

total 0

-rw-r--r--  1 root root 0 Jan 22 17:59 wuhuang-file1

-rw-r--r--  1 root root 0 Jan 22 17:59 wuhuang-file2

[root@backup ~]# mkdir /null

[root@backup ~]# rsync -r --delete /null/ /tmp/

[root@backup ~]# ll /tmp/

total 0

rsync利用一個空目錄將目錄清空

[root@backup ~]# rsync -r --delete /null/ 10.0.0.31:/tmp/

root@10.0.0.31's password:

[root@nfs01 ~]# ll /tmp/

total 0

1.3.4 rsync軟件 == ls命令

[root@backup ~]# ls /wuhuang/

wuhuang-file1  wuhuang-file2  wuhuang-file3  wuhuang-file4  wuhuang-file5

[root@backup ~]# rsync /wuhuang/

drwxr-xr-x        4096 2018/01/22 17:59:26 .

-rw-r--r--           0 2018/01/22 17:59:26 wuhuang-file1

-rw-r--r--           0 2018/01/22 17:59:26 wuhuang-file2

-rw-r--r--           0 2018/01/22 17:59:26 wuhuang-file3

-rw-r--r--           0 2018/01/22 17:59:26 wuhuang-file4

-rw-r--r--           0 2018/01/22 17:59:26 wuhuang-file5

1.4 Rsync複製原理介紹

 Rsync軟件實現增量同步原理說明

 Rsync經過其獨特的「quick check」算法,它僅同步大小或者最後修改時間發生變化的文件或目錄,

 固然也可根據權限,屬主等屬性的變化同步,但須要指定相應的參數,甚至能夠實現只同步一個文件裏有變化的內容部分,因此,能夠實現快速的同步備份數據。

image.png 


 傳統的cp , scp 工具拷貝每次均爲完整的拷貝,而Rsync除了能夠完整拷貝外,還具備增量拷貝的功能,

所以,從同步數據的性能及效率上,Rsync工具更勝一籌。

  CentOS5rsync2.x比對方法,把全部的文件比對一遍,而後進行同步。

  CentOS6rysnc3.x比對方法,一邊比對差別,一邊對差別的部分進行同步。

1.5  Rsync軟件特性(7個特性)

1. 支持拷貝普通文件與特殊文件如連接文件,設備等。

2. 能夠有排除指定文件或目錄同步的功能,至關於打包命令tar的排除功能。

  #tar zcvf backup_1.tar.gz  /opt/data  -exclude=oldboy    

  說明:在打包/opt/data時就排除了oldboy命名的目錄和文件。

3. 能夠作到保持原文件或目錄的權限、時間、軟硬連接、屬主、組等全部屬性均不改變-p

4. 可實現增量同步,只同步發生變化的數據,所以數據傳輸效率很高(tar -N)。

tar  -N <日期格式或 --newer=<日期時間>:只將比指定日期更新的文件保存到備份文件裏

1.    # 將備份/home 目錄自 2008-01-29 以來修改過的文件

        # tar -N 2008-01-29 -zcvf /backups/inc-backup_$(date +%F).tar.gz /home

2.    # 將備份 /home 目錄昨天以來修改過的文件

        # tar -N $(date -d yesterday "+%F") -zcvf /backups/inc-backup_$(date +%F).tar.gz /home

        # tar -N $(date -d  -1day "+%F") -zcvf /backups/inc-backup_$(date +%F).tar.gz /home

3.    # 添加文件到已經打包的文件

        # tar -rf all.tar *.gif

   說明:這條命令是將全部.gif的文件增長到all.tar的包裏面去。-r是表示增長文件的意思。

5. 可使用rcp,rsh,ssh等方式來配合進行隧道加密傳輸文件(rsync自己不對數據加密)

6. 能夠經過socket(守護進程方式)傳輸文件和數據(服務端和客戶端)*****。重點掌握

7. 支持匿名的或認證(無需系統用戶)的進程模式傳輸,可實現方便安全的進行數據備份及鏡像。

1.6 Rsync的企業工做場景說明

1) 利用定時任務實現數據備份(crond+rsync)

2) 利用實時同步方式實現數據備份(inotify/sersync+rsync)

第2章  Rsync的工做方式介紹與實踐

       SYNOPSIS

2.1  Local:  rsync [OPTION...] SRC... [DEST]

   本地數據備份方式,相似cp命令

   rsync          ---數據備份命令

   [OPTION...]    ---指定備份數據命令參數

   SRC           ---本地要備份的數據信息

   [DEST]        ---將要備份的數據保存到什麼位置

拷貝本地文件。當SRCDEST路徑信息都不包含有單個冒號":"分隔符時就啓動這種工做模式。如:rsync -a /data /backup

[root@backup ~]# rsync -a /etc/hosts /wuhuang/

[root@backup ~]# ll /wuhuang/

total 4

-rw-r--r-- 1 root root 372 Jan 19 11:55 hosts

 2.2  Access via remote shell:

       Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]

       Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

   實現遠程備份方式,相似scp命令

   Pull

   rsync             ---數據備份命令

   [OPTION...]       ---指定備份數據命令參數

   [USER@]HOST:  ---定義以什麼身份從相應主機上,拉取數據信息

                    (若是沒有[USER@],表示以當前用戶身份登陸到遠程主機,拉取數據)

   SRC...            ---將要從遠端服務拉取的數據信息(文件或目錄)

   [DEST]           ---將拉取過來的數據,保存到本地路徑信息

使用一個遠程shell程序(rshssh)來實現將本地機器的內容拷貝到遠程機器。當DEST路徑地址包含單個冒號":"分隔符時啓動該模式。如:rsync -avz *.c foo:src

 

backup服務器上的數據傳輸到NFS01上

[root@backup ~]# rsync -avz /wuhuang/wuhuang-file1 10.0.0.31:/opt

root@10.0.0.31's password:

sending incremental file list

wuhuang-file1

 

sent 74 bytes  received 31 bytes  23.33 bytes/sec

total size is 0  speedup is 0.00

[root@nfs01 ~]# ls /opt/

rh  wuhuang-file1

 Push

   rsync             ---數據備份命令

   [OPTION...]       ---指定備份數據命令參數

   SRC...            ---將本地服務器上數據信息(文件或目錄),推送到遠端

   [USER@]HOST:   ---定義以什麼身份向相應主機上,推送數據信息

                    (若是沒有[USER@],表示以當前用戶身份登陸到遠程主機,推送數據)

   DEST            ---將本地數據推送到遠程服務器的路徑信息

使用一個遠程shell程序(rshssh)來實現將遠程機器的內容拷貝到本地機器。當SRC地址路徑包含單個冒號":"分隔符時啓動該模式。如:rsync -avz foo:src/bar /data

[root@nfs01 ~]# touch /tmp/wh.txt

[root@nfs01 ~]# ll /tmp/

total 0

-rw-r--r-- 1 root root 0 Jan 22 20:24 wh.txt

[root@backup ~]# rsync -avz 10.0.0.31:/tmp/ /wuhuang/

root@10.0.0.31's password:

receiving incremental file list

./

wh.txt

 

sent 33 bytes  received 85 bytes  21.45 bytes/sec

total size is 0  speedup is 0.00

[root@backup ~]# ll /wuhuang/

total 4

-rw-r--r-- 1 root root 372 Jan 19 11:55 hosts

-rw-r--r-- 1 root root   0 Jan 22 20:24 wh.txt

-rw-r--r-- 1 root root   0 Jan 22 20:18 wuhuang-file1

-rw-r--r-- 1 root root   0 Jan 22 20:18 wuhuang-file2

-rw-r--r-- 1 root root   0 Jan 22 20:18 wuhuang-file3

-rw-r--r-- 1 root root   0 Jan 22 20:18 wuhuang-file4

-rw-r--r-- 1 root root   0 Jan 22 20:18 wuhuang-file5

  

image.png 

2.3  Access via rsync daemon:  重點

         Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]

               rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]

         Push: rsync [OPTION...] SRC... [USER@]HOST::DEST

               rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

   實現守護進程方式,進行數據備份同步:

   Pull

   rsync                    ---數據備份命令

   [OPTION...]           ---指定備份數據命令參數

   [USER@]HOST::    ---指定認證用戶身份信息,從相應主機,拉取數據信息

   SRC...                    ---指定一個模塊信息

   [DEST]                  ---將拉取過來的數據,保存到本地路徑信息

從遠程rsync服務器中拷貝文件到本地機。當SRC路徑信息包含"::"分隔符時啓動該模式。如:rsync -av root@192.168.78.192::www /databack

www 爲一個模塊

   Push

   rsync                      ---數據備份命令

   [OPTION...]            ---指定備份數據命令參數

   SRC...                     ---將本地服務器上數據信息(文件或目錄),推送到遠端

   [USER@]HOST::     ---指定認證用戶身份信息,將本地主機數據,推送到遠端

   DEST                      ---指定一個模塊信息

 從本地機器拷貝文件到遠程rsync服務器中。當DEST路徑信息包含"::"分隔符時啓動該模式。如:rsync -av /databack root@192.168.78.192::www

www 爲一個模塊

第3章 rsync守護進程模式部署步驟

3.1 服務端部署

3.1.1 第一個里程:檢查軟件是否存在

[root@backup ~]# rpm -qa|grep rsync

rsync-3.0.6-12.el6.x86_64

若是rsync軟件不存在 yum install -y rsync

3.1.2  第二個里程:編寫rsync軟件的配置文件

vim /etc/rsyncd.conf       ---rsyncd.confrsync服務的默認配置文件,默認並不存在

image.png 

#rsync_config

#created by HQ at 2017

##rsyncd.conf start##

    

uid = rsync

gid = rsync

use chroot = no

max connections = 200

timeout = 300

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

ignore errors

read only = false

list = false

hosts allow = 172.16.1.0/24

hosts deny = 0.0.0.0/32

auth users = rsync_backup

secrets file = /etc/rsync.password

[backup]

comment = "backup dir by oldboy"

path = /backup

參考資料:http://man.linuxde.net/rsync

3.1.3 第三個里程碑:建立備份目錄管理用戶

[root@backup ~]# vim /etc/rsyncd.conf

[root@backup ~]# useradd -s /sbin/nologin -M rsync

[root@backup ~]# id rsync

uid=501(rsync) gid=501(rsync) groups=501(rsync)

3.1.4 第四個里程碑:建立認證用戶密碼文件

建立密碼文件,採用這種方式不能使用系統用戶對客戶端進行認證,因此須要建立一個密碼文件,其格式爲username:password」,用戶名能夠和密碼能夠隨便定義,最好不要和系統賬戶一致,同時要把建立的密碼文件權限設置爲600保證安全,密碼不泄露。

[root@backup ~]# echo "rsync_backup:wuhuang123" >/etc/rsync.password

[root@backup ~]# chmod 600 /etc/rsync.password

[root@backup ~]# ll /etc/rsync.password

-rw------- 1 root root 23 Jan 22 20:51 /etc/rsync.password

3.1.5 第五個里程碑:建立備份目錄,並進行受權

在備份或恢復時,服務端要容許客戶端有寫入權限,不然也不能在客戶端直接對服務端進行恢復

[root@backup ~]# mkdir /backup

[root@backup ~]# chown -R rsync.rsync /backup/

[root@backup ~]# ll /backup/ -d

drwxr-xr-x 2 rsync rsync 4096 Jan 22 20:56 /backup/

3.1.6 第六個里程碑:啓動rsync守護進程服務

[root@backup ~]# rsync --daemon

[root@backup ~]# ps -ef|grep rsync

root       1870      1  0 20:59 ?        00:00:00 rsync --daemon

root       1879   1228  0 21:00 pts/0    00:00:00 grep rsync

[root@backup ~]#  netstat -lntup|grep rsync

tcp        0      0 0.0.0.0:873            0.0.0.0:*               LISTEN      1870/rsync          

tcp        0      0 :::873                 :::*                     LISTEN      1870/rsync

3.2 客戶端部署

3.2.1 進行測試傳輸備份數據

[root@nfs01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup

Password:

sending incremental file list

hosts

 

sent 201 bytes  received 27 bytes  24.00 bytes/sec

total size is 372  speedup is 1.63

[root@backup ~]# ll /backup/

total 4

-rw-r--r-- 1 rsync rsync 372 Jan 19 11:55 hosts

3.3  rsync守護進程模式數據傳輸原理

image.png 

第4章 殺進程的三種方式

4.1 kill

kill命令用來刪除執行中的程序或工做。kill可將指定的信息送至程序。預設的信息爲SIGTERM(15),可將指定程序終止。若仍沒法終止該程序,可以使用SIGKILL(9)信息嘗試強制刪除程序。程序或工做的編號可利用ps指令或job指令查看。

   kill殺手:針對進程號,殺死進程,而且會提示進程已殺死

   kill 進程號

4.1.1 語法

kill(選項)(參數)

4.1.2 選項

-a:當處理當前進程時,不限制命令名和進程號的對應關係;

-l <信息編號>:若不加<信息編號>選項,則-l參數會列出所有的信息名稱;

-p:指定kill 命令只打印相關進程的進程號,而不發送任何信號;

-s <信息名稱或編號>:指定要送出的信息;

-u:指定用戶。

4.1.3 參數

進程或做業識別號:指定要刪除的進程或做業。

4.1.4 實例

列出全部信號名稱:

[root@backup ~]# kill -l

 1) SIGHUP  2) SIGINT  3) SIGQUIT  4) SIGILL  5) SIGTRAP

 6) SIGABRT  7) SIGBUS  8) SIGFPE  9) SIGKILL 10) SIGUSR1

11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM

16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP

21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ

26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR

31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3

38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8

43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13

48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12

53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7

58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2

63) SIGRTMAX-1 64) SIGRTMAX

只有第9種信號(SIGKILL)才能夠無條件終止進程,其餘信號進程都有權利忽略,下面是經常使用的信號:

  HUP     1    終端斷線

   INT     2    中斷(同 Ctrl + C

   QUIT    3    退出(同 Ctrl + \

   TERM   15    終止

   KILL    9    強制終止

   CONT   18    繼續(與STOP相反, fg/bg命令)

   STOP   19    暫停(同 Ctrl + Z

 

先用ps查找進程,而後用kill殺掉:

[root@backup ~]# ps -ef | grep vim

root      3268  2884  0 16:21 pts/1    00:00:00 vim install.log

root      3370  2822  0 16:21 pts/0    00:00:00 grep vim

[root@backup ~]# kill 3268

[root@backup ~]# kill 3268

-bash: kill: (3268) - 沒有那個進程

4.2 killall

killall命令使用進程的名稱來殺死進程,使用此指令能夠殺死一組同名進程。咱們可使用kill命令殺死指定進程PID的進程,若是要找到咱們須要殺死的進程,咱們還須要在以前使用ps等命令再配合grep來查找進程,而killall把這兩個過程合二爲一,是一個很好用的命令。

  killall殺手:針對進程名,殺死進程,而且會提示進程已殺死

  killall sshd

 4.2.1 語法

killall(選項)(參數)

4.2.2 選項

-e:對長名稱進行精確匹配;

-l:忽略大小寫的不一樣;

-p:殺死進程所屬的進程組;

-i:交互式殺死進程,殺死進程前須要進行確認;

-l:打印全部已知信號列表;

-q:若是沒有進程被殺死。則不輸出任何信息;

-r:使用正規表達式匹配要殺死的進程名稱;

-s:用指定的進程號代替默認信號「SIGTERM」;

-u:殺死指定用戶的進程。

4.2.3 參數

進程名稱:指定要殺死的進程名稱。

4.2.4 實例

殺死全部同名進程

[root@backup ~]# killall vi

4.3 pkill

 pkill殺手:針對進程名,殺死進程,可是一個模糊的殺手,殘暴的殺手(不會有提示信息)

 pkill sshd

[root@backup ~]# ps -ef |grep sh

root        536      2  0 17:57 ?        00:00:00 [flush-8:0]

root       1188      1  0 17:57 ?        00:00:00 /usr/sbin/sshd

root       1229   1188  0 17:58 ?        00:00:00 sshd: root@pts/0

root       1231   1229  0 17:58 pts/0    00:00:00 -bash

root       1734   1188  0 19:54 ?        00:00:00 sshd: root@pts/1

root       1736   1734  0 19:54 pts/1    00:00:00 -bash

root       1908   1231  1 21:28 pts/0    00:00:00 grep sh

[root@backup ~]# pkill sh

此命令同時殺死了以上進程

相關文章
相關標籤/搜索