搭建rsync服務器,實現文件備份同步

1、服務器端rsync的安裝和配置mysql

  1. 服務器上的rsync安裝git

    在此已本地虛擬機192.168.1.113爲服務器作例子,客戶端192.168.1.114做爲備份同步的客戶端。
    sql

    直接yum安裝:shell

    yum install rsynccentos

  2. rsync的配置


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

服務器配置文件,該文件默認不存在,請建立它.如今我在/etc目錄下建立rsync.d目錄,再建立服務器

rsyncd.conf、rsyncd.secrets、rsyncd.motd測試

  具體步驟以下:ui

[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的配置spa

# 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的配置

tjj:123456

ps:tjj是登錄用戶,後面是密碼

如下是rsyncd.motd的配置

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

++++++++++++++++++++++++++++++++++++++++++++++


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 tjj@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

2、在客戶機(192.168.1.114)上同步服務器

  1. 也要裝上rsync,直接yum install rsync便可

  2. 編寫密碼文件

    vi /home/psw/rsync.secrets

    chmod 600 /home/psw/rsync.secrets(必須賦予600權限)

  3. 編寫連接備份的腳本sh

    vi /home/shell/rsync.sh

  chmod+x /home/shell/rsync.sh

     ./home/shell/rsync.sh

ps:sync -avzP --delete --password-file=密碼文件  tjj@119.146.203.245::mysql_backup 備份目錄

 4.同步成功

相關文章
相關標籤/搜索