linux下rsync實現自動增量備份

來源:互聯網
上載者:User

需求:一台伺服器上內容,每天定時備份到遠程容災伺服器。為了節省空間的和提高傳輸效率,使用增量備份。
擴充需求:兩台伺服器即時單向、雙向同步某目錄下的所有資料。

環境:cent os 5(5.4 5.5 5.6測試通過),系統內建rsync。
1.安裝rsync(已經安裝可以忽略)

 代碼如下 複製代碼

wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.7.tar.gz
tar xvfz rsync-3.0.7.tar.gz
cd rsync-3.0.7
./configure --prefix=/usr/local/rsync
make && make install

2.配置rsync,建立一個rsyncd.conf檔案。

vim /etc/rsyncd.conf

輸入以下內容:

 代碼如下 複製代碼

uid=nobody
gid=nobody
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
[htdocs]
comment= backup htdocs
path=/opt/htdocs/view
ignore errors
read only = yes
host allow = *
host deny=*
secrets file=/etc/rsyncd.passwd
auth users=bakccad

修改:

 代碼如下 複製代碼

vim /etc/rsyncd.passwd

輸入:

 代碼如下 複製代碼
username:13577531

執行:

 代碼如下 複製代碼

chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsyncd.passwd

3.啟動rsync以伺服器端方式:

 代碼如下 複製代碼

/usr/local/rsync/bin/rsync --daemon

用戶端:
用戶端無需安裝,只需編輯密碼檔案輸入密碼:

 代碼如下 複製代碼

vim /etc/rsyncd.passwd

輸入:
13577531
執行:

 代碼如下 複製代碼

chmod 600 /etc/rsyncd.passwd

用戶端執行以下命令即可完成伺服器端到本地的同步:

 代碼如下 複製代碼

rsync -azP --delete bakccad@1.2.3.4::htdocs /opt/bakroot/rsyncwww/view --password-file=/etc/rsync.passwd

這裡命令中htdocs為伺服器端配置的名稱。後面路徑為儲存到的路徑。後面是密碼檔案所在,密碼檔案只需有密碼即可(客戶機如果使用user:passwd的形式反而會出現錯誤)。

附,如果你只是本身備份我們可以直接使用shell命令進行備份

在cron裡設定,每周一淩晨2點執行(每周一全備份,其餘時間增量備份)

 代碼如下 複製代碼

#vi backup.sh


Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#!/bin/bash

# define
dayofweek=`date "+%u"`
today=`date "+%Y%m%d"`
source=/data/
backup=/backup/

# action
cd $backup

if [ $dayofweek -eq 1 ]; then
  if [ ! -f "full$today.tar.gz" ]; then
    rm -rf snapshot
    tar -g snapshot -zcf "full$today.tar.gz" $source --exclude $sourceserver.log
  fi
else
  if [ ! -f "inc$today.tar.gz" ]; then
    tar -g snapshot -zcf "inc$today.tar.gz" $source --exclude $sourceserver.log
  fi
fi

這要就備份好資料在你指定目錄了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.