rsync服務器搭建

      rsync在進行文件備份時是如此的方便,以致於我以爲必須在本身的服務器上安裝它。這裏對rsync的服務器進行了簡單粗暴的搭建和配置(直接上代碼),對於細節不作深刻討論,可是能夠確定是,服務器必定能run起來,對於新手這纔是最重要的,不是嗎?mysql

一 什麼是rsync

  rsync,remote synchronize顧名思意就知道它是一款實現遠程同步功能的軟件,它在同步文件的同時,能夠保持原來文件的權限、時間、軟硬連接等附加信息。 rsync是用 「rsync 算法」提供了一個客戶機和遠程文件服務器的文件同步的快速方法,並且能夠經過ssh方式來傳輸文件,這樣其保密性也很是好,另外它仍是免費的軟件。git

  rsync 包括以下的一些特性:算法

  1. 能更新整個目錄和樹和文件系統;sql

  2. 有選擇性的保持符號鏈鏈、硬連接、文件屬於、權限、設備以及時間等;shell

  3. 對於安裝來講,無任何特殊權限要求;ubuntu

       4. 對於多個文件來講,內部流水線減小文件等待的延時;centos

       5. 能用rsh、ssh 或直接端口作爲傳輸入端口;bash

       6. 支持匿名rsync 同步文件,是理想的鏡像工具;服務器

二 搭建rsync服務器

1.rsync的安裝

        a. 源碼編譯安裝

          下載地址:https://rsync.samba.org/ssh

[root@yearnfar install]# tar xvf rsync-3.1.1.tar.gz
[root@yearnfar install]# cd rsync-3.1.1
[root@yearnfar rsync-3.1.1]# ./configure --prefix=/usr/local
[root@yearnfar rsync-3.1.1]# make && make install
[root@yearnfar rsync-3.1.1]# cd /usr/local/bin
[root@yearnfar bin]# ll|grep rsync
     -rwxr-xr-x. 1 root root 1368856 9月  21 12:01 rsync

        b. 軟件包安裝

sudo apt-get install rsync  ##注:在debian、ubuntu 等在線安裝方法;
yum install rsync           ##注:Fedora、Redhat 等在線安裝方法;
rpm -ivh rsync              ##注:Fedora、Redhat 等rpm包安裝方法;

                            ##其它Linux發行版,請用相應的軟件包管理方法來安裝。

2.rsync的配置

         rsync的主要有如下三個配置文件rsyncd.conf(主配置文件)、rsyncd.secrets(密碼文件)、rsyncd.motd(rysnc服務器信息)

服務器配置文件(/etc/rsyncd.conf),該文件默認不存在,請建立它。

  具體步驟以下:

[root@yearnfar local]# cd /etc/
[root@yearnfar etc]# mkdir rsync.d
[root@yearnfar etc]# cd rsync.d/
    
[root@yearnfar rsync.d]# touch rsyncd.conf
[root@yearnfar rsync.d]# touch rsyncd.secrets
[root@yearnfar rsync.d]# touch rsyncd.motd
[root@yearnfar rsync.d]# chmod 600 rsyncd.secrets    ##將rsyncd.secrets這個密碼文件的文件屬性設爲root擁有, 且權限要設爲600
[root@yearnfar rsync.d]# ll
總用量 0
-rw-r--r--. 1 root root 0 9月  21 12:14 rsyncd.conf
-rw-r--r--. 1 root root 0 9月  21 12:14 rsyncd.motd
-rw-------. 1 root root 0 9月  21 12:14 rsyncd.secrets

    如下是rsyncd.conf的配置

# Distributed under the terms of the GNU General Public License v2
# Minimal configuration file for rsync daemon
# See rsync(1) and rsyncd.conf(5) man pages for help
# This line is required by the /etc/init.d/rsyncd script
# pid file = /var/run/rsyncd.pid
port = 873
address = 115.28.34.xxx  #修改成本身的ip
uid = root
gid = root
use chroot = yes
read only = yes
#limit access to private LANs
hosts allow=*
hosts deny=*
max connections = 5
motd file = /etc/rsync.d/rsyncd.motd
#This will give you a separate log file
#log file = /var/log/rsync.log
#This will log every file transferred - up to 85,000+ per user, per sync
#transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
[mysql_backup]
path = /data/mysql_backup
list=yes
ignore errors
auth users = yearnfar
secrets file = /etc/rsync.d/rsyncd.secrets
comment = backup mysql
exclude = git/

    如下是rsyncd.secrets的配置

yearnfar:123456

    如下是rsyncd.motd的配置

++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the mike.org.cn rsync services!
           centos6.3 yearnfar
++++++++++++++++++++++++++++++++++++++++++++++

3.啓動腳本

[root@yearnfar rsync.d]# vi /etc/init.d/rsync 
#!/bin/bash
#
# rsyncd      This shell script takes care of starting and stopping
#             standalone rsync.
#
# chkconfig: - 99 50
# description: rsync is a file transport daemon
# processname: rsync
# config: /etc/rsync.d/rsyncd.conf
# Source function library
. /etc/rc.d/init.d/functions
RETVAL=0
rsync="/usr/local/bin/rsync"
prog="rsync"
CFILE="/etc/rsync.d/rsyncd.conf"
start() {
        # Start daemons.
        [ -x $rsync ] || \
            { echo "FATAL: No such programme";exit 4; }
        [ -f $CFILE ] || \
            { echo "FATAL: config file does not exist";exit 6; }
        echo -n $"Starting $prog: "
        daemon $rsync --daemon --config=$CFILE
        RETVAL=$?
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
        echo
        return $RETVAL
}
stop() {
        # Stop daemons.
        echo -n $"Stopping $prog: "
        killproc $prog -QUIT
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
        return $RETVAL
}
# call the function we defined
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload)
        stop
        start
        RETVAL=$?
        ;;
  status)
        status $prog
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|reload|status}"
        exit 2
esac
exit $RETVAL


[root@yearnfar rsync.d]# chmod +x /etc/init.d/rsync
[root@yearnfar rsync.d]# /etc/init.d/rsync start
     正在啓動 rsync:                                           [肯定]
     
[root@yearnfar rsync.d]# chkconfig --add /etc/init.d/rsync  ## 添加到開機啓動
[root@yearnfar rsync.d]# chkconfig --level 235 rsync on     ## 添加到開機啓動

4.測試鏈接

[root@yearnfar rsync.d]# rsync --list-only yearnfar@115.28.34.xxx::mysql_backup
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the mike.org.cn rsync services!
           centos6.3 yearnfar 
++++++++++++++++++++++++++++++++++++++++++++++
Password: 
drwxr-xr-x        4096 2015/09/20 15:24:06 .
drwxr-xr-x        4096 2015/09/21 04:00:01 201509
相關文章
相關標籤/搜索