ubuntu下使用rsync+inotify-tools實現檔案自動備份之一

來源:互聯網
上載者:User

    現在有個需求,A伺服器的某個路徑下的檔案,想備份到B伺服器下面。假設A的ip是 192.168.1.75,B的ip是 192.168.1.85。在ubuntu下面可以使用rsync軟體配合inotify工具一起來實現這個功能。

1 rsync主機端配置

    ubuntu16.03系統預設已經安裝了rsync軟體。需要配置rsync軟體。A是資料來源,在使用inotify-tools時需要將A配置成rsync從機。將B配置成rsync主機。在B的/etc路徑下建立rsyncd.conf設定檔,檔案內容如下:

# sample rsyncd.conf configuration file# GLOBAL OPTIONS#motd file=/etc/motdlog file=/var/log/rsyncd# for pid file, do not use /var/run/rsync.pid if# you are going to run rsync out of the init.d script.# The init.d script does its own pid file handling,# so omit the "pid file" line completely in that case.pid file=/var/run/rsyncd.pidsyslog facility=daemon#socket options=# MODULE OPTIONS[ftp]//ftp這個模組名要記住comment = public archivepath = /data    //這是同步路徑use chroot = no#max connections=10lock file = /var/lock/rsyncd# the default for read only is yes...read only = no    //這個地方要配置成no,不然往/data路徑下寫檔案會發生錯誤list = yesuid = rootgid = root#exclude = #exclude from = #include =#include from =auth users =lzj //lzj是rsyncd.secrets裡面設定的使用者secrets file = /etc/rsyncd.secretsstrict modes = yeshosts allow =192.168.1.75    //允許A主機訪問    #hosts deny =ignore errors = yesignore nonreadable = yestransfer logging = no#log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.timeout = 600refuse options = checksum dry-run        dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz

然後在/etc路徑下建立密碼檔案rsyncd.secrets,內容為:

lzj:123

再修改/ect/default/rsync檔案:

# defaults file for rsync daemon mode# start rsync in daemon mode from init.d script?#  only allowed values are "true", "false", and "inetd"#  Use "inetd" if you want to start the rsyncd from inetd,#  all this does is prevent the init.d script from printing a message#  about not starting rsyncd (you still need to modify inetd's config yourself).RSYNC_ENABLE=true# which file should be used as the configuration file for rsync.# This file is used instead of the default /etc/rsyncd.conf# Warning: This option has no effect if the daemon is accessed#          using a remote shell. When using a different file for#          rsync you might want to symlink /etc/rsyncd.conf to#          that file. RSYNC_CONFIG_FILE=/etc/rsyncd.conf# what extra options to give rsync --daemon?#  that excludes the --daemon; that's always done in the init.d script#  Possibilities are:#   --address=123.45.67.89(bind to a specific IP address)#   --port=8730(bind to specified port; default 873)RSYNC_OPTS=''# run rsyncd at a nice level?#  the rsync daemon can impact performance due to much I/O and CPU usage,#  so you may want to run it at a nicer priority than the default priority.#  Allowed values are 0 - 19 inclusive; 10 is a reasonable value.RSYNC_NICE=''# run rsyncd with ionice?#  "ionice" does for IO load what "nice" does for CPU load.#  As rsync is often used for backups which aren't all that time-critical,#  reducing the rsync IO priority will benefit the rest of the system.#  See the manpage for ionice for allowed options.#  -c3 is recommended, this will run rsync IO at "idle" priority. Uncomment#  the next line to activate this.# RSYNC_IONICE='-c3'# Don't forget to create an appropriate config file,# else the daemon will not start.

設定同步路徑/data的許可權:

        chown -R root:root /data

其中的root:root分別是rsyncd.conf所設定的uid和gid。

現在B端配置完成,運行下列命令啟動rsync服務:

    /etc/init.d/rsync start

查看rsync服務是否啟動:

    ps -e|grep rsync

    9677 ?        00:00:00 rsync

2 A端配置

    在A端建立rsync秘鑰檔案/etc/rsync.pass,內容如下:

123

注意只要寫密碼就行了。

然後修改秘鑰檔案的許可權:

    chmod 600 /etc/rsync.pass

建立同步資料夾,並對檔案夾授權:

    mkdir /data

  chown -R root:root /data

在A端安裝inotify-tools:

    sudo apt-get install inotify-tools

在A端建立shell指令碼 /sync.sh,內容如下:

src=/datadst=lzj@192.168.1.85::ftp /usr/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files        do    rsync -vzrtopg --delete  --progress --password-file=/etc/rsync.pass $src $dst        doneexit 0

授權並運行 /sync.sh:

    chmod +x /sync.sh

    ./sync.sh

觀察在A端的/data檔案夾下建立檔案或刪除檔案,B端的/data目錄是否有檔案變化。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.