天天看點

rsync遠端同步備份工具

rsync遠端同步介紹

rsync(Remote Sync,遠端同步)是一個開源的快速備份工具,可以在不同主機之間鏡像同步整個目錄樹,支援增量備份,保持連結和權限,切采用優化的同步算法,傳輸前執行壓縮,是以非常适用于異地備份,鏡像伺服器等應用。

rsync官方站點是http://rsync.samba.org/,由Wayne Davsion進行維護。作為一種常用的備份工具,rsync往往是Linux和Unix系統預設安裝的基本元件之一。

在遠端同步任務中,負責發起rsync同步操作的客戶機稱為發起端,而負責響應來自客戶機的rsync同步操作的伺服器稱為同步源。在同步過程中,同步源負責提供文檔的原始位置,發起端應對改設定具有讀取權限。

rsync作為同步源時以守護程序運作,為其他客戶機提供備份源。配置rsync同步源需要建立配置檔案rsyncd.conf,建立備份賬号,然後将rsync程式以“--daemon”選項運作。

rsync的基本用法(加粗部分表示常用)

格式:rsync [選項] 原始位置 目标位置

-r:遞歸整個目錄樹

-l:用來備份連結檔案

-v:顯示同步過程詳細資訊

-a:歸檔模式,保留檔案權限屬性等。等同于”-rlptgoD”

-z:在傳輸檔案時進行壓縮

-p:保留檔案權限标記

-P:顯示更詳細過程

-t:保留檔案時間标記

-g:保留檔案屬組标記

-o:保留檔案屬主标記

-H:保留硬連接配接檔案

-A:保留ACL屬性

-D:保留裝置檔案及其它特殊檔案

--delete:删除目标位置有而原始位置沒有的檔案

--checksum:根據校驗和來決定是否跳過檔案

首先確定如果要做rsync+inotify實時同步的話,要檢視系統核心。Linux核心從2.6.13版本開始提供了inotify通知接口,用來監控檔案系統的各種變化情況。

[root@root ~]# uname -r

2.6.32-431.el6.x86_64

注意:root使用者是服務端lisi使用者是用戶端

第一步:安裝xinetd和rsync。由于rsync是非常實用的備份工具是以Linux都是預設安裝好的,并啟動服務。

[root@root ~]# rpm -ivh /mnt/Packages/xinetd-2.3.14-39.el6_4.x86_64.rpm 

[root@root ~]# rpm -q rsync

rsync-3.0.6-9.el6_4.1.x86_64

[root@root ~]# vi /etc/xinetd.d/rsync 啟用xinetd

        disable = no   将yes改為no

[root@root ~]# service xinetd start

正在啟動 xinetd:                                          [确定]

[root@root ~]# netstat -natp | grep 873  rsync端口是tcp的873

tcp        0      0 :::873                      :::*      LISTEN      28033/xinetd      

基于安全性考慮,對于rsync的同步源最好僅允許以隻讀方式做同步。另外,同步可以采用匿名的方式,隻要将其中的“auth users”和“secrets file”配置記錄去掉就可以。

第二步:為備份賬戶建立資料檔案。由于賬号資訊采用明文存放,是以應調整檔案權限,避免賬号資訊洩露。

[root@root ~]# useradd benet   

[root@root ~]# echo "123" | passwd --stdin benet  密碼指定123

給使用者對/var/www/html/設定權限

[root@root ~]# ls /var/www/html/

[root@root ~]# setfacl -R -m user:benet:rwx /var/www/html/

[root@root ~]# setfacl -R -m default:benet:rwx /var/www/html/

[root@root ~]# getfacl /var/www/html/   檢視權限

getfacl: Removing leading '/' from absolute path names

# file: var/www/html/

# owner: root

# group: root

user::rwx

user:benet:rwx

group::r-x

mask::rwx

other::r-x

default:user::rwx

default:user:benet:rwx

default:group::r-x

default:mask::rwx

default:other::r-x

第三步:rsync備份操作

[root@root ~]# cp -R /boot/grub/ /var/www/html/ 拷貝一份測試檔案到需要被備份的目錄下

[root@root ~]# ls /var/www/html/  檢視目錄

grub

遠端ssh有兩種方式,第一種需要知道對方使用者密碼,另一種就是通過ssh-keygen生成秘鑰。

第一種輸入密碼遠端ssh登陸(預設密碼驗證方式)

[root@root ~]# ssh 192.168.100.200  遠端ssh

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

RSA key fingerprint is c5:0c:2a:f9:56:53:0a:28:f1:60:c9:a7:37:0c:8c:bc.

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

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

[email protected]'s password: 輸入對方使用者密碼

Last login: Fri Aug 28 13:58:24 2015 from 192.168.100.1

虛拟使用者

[root@lisi ~]# mkdir /web

[root@lisi ~]# rsync -azP --delete [email protected]:/var/www/html/ /web/

從伺服器上的使用者備份檔案到用戶端

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

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

[email protected]'s password: 輸入使用者benet密碼

有部分内容省略··· ···

rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1505) [generator=3.0.6]

rsync: send_files failed to open "/var/www/html/grub/grub.conf": Permission denied (13)

注釋:/boot/grub/grub.conf 這個檔案在發送時失敗,原因是缺少權限。可以把檔案權限改為644,再次執行就可以解決問題。

[root@lisi ~]# ls /web

[root@lisi ~]# exit

logout

Connection to 192.168.100.200 closed.

[root@root ~]# cd /var/www/html/grub/

[root@root grub]# ls -l grub.conf

-rw-------+ 1 root root    761 8月  28 14:31 grub.conf

[root@root grub]# chmod 644 grub.conf  添權重限

[root@root grub]# ls -l grub.conf 

-rw-r--r--+ 1 root root 761 8月  28 14:31 grub.conf

[root@root grub]# ssh 192.168.100.200再次遠端到用戶端

[email protected]'s password: 

Last login: Fri Aug 28 14:33:37 2015 from 192.168.100.100

第二種通過生成密鑰方式進行無密碼登入

[root@lisi ~]# ssh-keygen  生成密鑰對

[email protected]'s password:  輸入benet使用者密碼 

[root@lisi ~]# rsync -azP --delete [email protected]:/var/www/html/ /web/再次備份

[email protected]'s password: 

receiving incremental file list

grub/grub.conf

         761 100%  743.16kB/s    0:00:00 (xfer#1, to-check=11/18)

sent 31 bytes  received 878 bytes  259.71 bytes/sec

total size is 255280  speedup is 280.84

可以通過編輯腳本進行執行備份操作

[root@lisi ~]# vim /opt/rsync.sh

#!/bin/bash

rsync -az --delete [email protected]:/var/www/html/ /web/

[root@lisi ~]# chmod +x /opt/rsync.sh 

[root@lisi ~]# rm -rf /var/www/html/

[root@lisi ~]# rm -rf /web/*

[root@lisi ~]# ls /web/

[root@lisi ~]# cd /opt/

[root@lisi opt]# ./rsync.sh 

[root@lisi opt]# ls /web/

計劃備份

[root@lisi opt]# crontab -e

01 3 * * * /opt/rsync.sh &  每天3點10分運作腳本

配置rsync伺服器及需要備份的目錄,不使用系統使用者

[root@root grub]# vim /etc/rsyncd.conf

uid = nobody

gid = nobody

use chroot = yes   禁锢在源目錄

address = 192.168.100.100  監聽位址

port 873  監聽端口

max connections = 5  

log file = /var/log/rsyncd.log   日志檔案位置

pid file = /var/run/rsyncd.pid   存放程序ID的檔案位置

lock file = /var/run/rsync.lock 

hosts allow = 192.168.100.0/24  允許通路的客戶機位址

[wwwroot]

    path = /var/www/html/

    comment = Document Root of mail.benet.com

    read only = yes   是否為隻讀

    dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z  同步時不再壓縮的檔案類型

    auth users = back   授權賬戶

    secrets file = /etc/rsyncd_users.db   存放賬戶資訊的資料檔案

根據上述操作,建立賬号資料檔案,添加一行使用者記錄,以冒号分隔,虛拟賬戶名稱”back”密碼”abc123”

[root@root grub]# grep db /etc/rsyncd.conf 

    secrets file = /etc/rsyncd_users.db

[root@root grub]# vim /etc/rsyncd_users.db

back:abc123  設定密碼

[root@root grub]# ssh 192.168.100.200

Last login: Fri Aug 28 14:45:18 2015 from 192.168.100.100

[root@lisi ~]# rsync -azP [email protected]::wwwroot /web/

Password:   輸入back密碼

sent 58 bytes  received 440 bytes  110.67 bytes/sec

total size is 255280  speedup is 512.61

[root@lisi ~]# rm -rf /web/*  删除備份檔案

Password: 

./

@ERROR: auth failed on module wwwroot

rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]

[root@lisi ~]# rsync -azP [email protected]::wwwroot /web/  開始備份

[root@lisi ~]# ls /web/ 

Grub

在192.168.100.200上輸入變量,無需互動密碼

[root@lisi ~]# export RSYNC_PASSWORD=abc123  在192.168.100.100上輸入變量,無需互動密碼

··· ···省略

sent 352 bytes  received 148758 bytes  298220.00 bytes/sec

total size is 255280  speedup is 1.71

進行rsync+inotify實時同步

[root@lisi ~]# cd /proc/sys/fs/inotify/

[root@lisi inotify]# ls

max_queued_events  max_user_instances  max_user_watches

[root@lisi inotify]# vim /etc/sysctl.conf  配合inotify觸發備份、調整核心參數

# Kernel sysctl configuration file for Red Hat Linux

# Controls the maximum number of shared memory segments, in pages

kernel.shmall = 4294967296

fs.inotify.max_queued_events = 16384  監控事件的隊列

fs.inotify.max_user_instances = 1024   最多監控執行個體數

fs.inotify.max_user_watches = 1048576 每個執行個體最多監控檔案數

[root@lisi inotify]# sysctl -p  立即生效

[root@root grub]# mkdir /abc  建立檔案夾

[root@root grub]# cd /abc

通過軟體工具直接将所需安裝包移動到用戶端中然後進行解壓縮。

[root@root abc]# ls

inotify-tools-3.14.tar.gz

[root@root abc]# tar zxvf inotify-tools-3.14.tar.gz 解壓改軟體

[root@root abc]# mv inotify-tools-3.14 /opt

[root@root abc]# cd /opt/inotify-tools-3.14/

[root@root inotify-tools-3.14]# yum -y install gcc gcc-c++  手工編譯安裝

[root@root inotify-tools-3.14]# ./configure

[root@root inotify-tools-3.14]# make && make install

進行監測

[root@root inotify-tools-3.14]#inotifywait -mrq -e modify,create,move,delete /var/www/html/

Inotifywait可監控、modify:修改、 create:建立、 move:移動、 delete:删除等操作。

在另打開一個連接配接,并建立一個檔案

<a href="http://s3.51cto.com/wyfs02/M02/73/01/wKiom1XxdFexjnyVAAAwqjrp-To080.jpg" target="_blank"></a>

在回到監測的連接配接中,檢視。你可以對裡面進行建立、删除等操作。

<a href="http://s3.51cto.com/wyfs02/M02/73/01/wKiom1XxdFfg5lhWAABz7fdMu2Q701.jpg" target="_blank"></a>

本文轉自白羊IT51CTO部落格,原文連結:http://blog.51cto.com/000011211684/1693621,如需轉載請自行聯系原作者