1. 程式人生 > 實用技巧 >[leetCode]131. 分割回文串

[leetCode]131. 分割回文串

前言

rsync命令是一個遠端資料同步工具,可通過LAN/WAN快速同步多臺主機間的檔案。rsync使用所謂的“rsync演算法”來使本地和遠端兩個主機之間的檔案達到同步,這個演算法只傳送兩個檔案的不同部分,而不是每次都整份傳送,因此速度相當快。 rsync是一個功能非常強大的工具,其命令也有很多功能特色選項,我們下面就對它的選項一一進行分析說明。

一、配置rsync源伺服器

1.1、關於rsync

1.1.1、一款快速增量備份工具

1.1.2、Remote Sync,遠端同步

1.1.3、支援本地複製,或者與其他SSH、rsync主機同步

1.1.4、官方網站:http://rsync.samba.org

.2、rsync同步源

指備份操作的遠端伺服器,也稱為備份源

1.3、基本思路

1.3.1、建立rsyncd.conf配置檔案、獨立的賬戶檔案

1.3.2、啟用rsync的--daemon模式

1.4、應用示例

1.4.1、使用者backuper,允許下行同步

1.4.2、操作的目錄為/var/www/html

1.5、配置檔案rsync.conf

1.5.1、需手動建立,語法類似於Samba配置

1.5.2、認證配置auth users、secrets file,不加則為匿名

1.6、rsync賬戶檔案

1.6.1、採用“使用者名稱:密碼”的記錄格式,每行一個使用者記錄

1.6.2、獨立的賬戶資料,不依賴於系統賬號

1.7、啟用rsync服務

通過--daemon獨自提供服務

二、使用rsync備份工具

2.1、rsync命令的用法

語法
rsync [選項] 原始位置 目標位置

常用選項
-a:         歸檔模式,遞歸併保留物件屬性,等同於-rlptgoD
-v:         顯示同步過程的詳細(verbose)資訊
-z:         在傳輸檔案時進行壓縮(compress)
-H:         保留硬連線檔案
-A:         保留ACL屬性資訊
-p:         保留檔案的許可權標記
-t:          保留檔案的時間標記
-g:         保留檔案的屬組標記
-o:         保留檔案的屬主標記
-delete:    刪除目標位置有而原始位置沒有的檔案
-checksum:  根據物件的校驗和來決定是否跳過檔案

2.2、配置源的兩種表示方法

1 格式1
2 使用者名稱@主機地址::共享模組名
3 
4 格式2
5 rsync://使用者名稱@主機地址/共享模組名

2.3、rsync源的免互動處理

使用 --password-file= 密碼檔案

三、rsync遠端同步部署

3.1、環境說明

3.2、配置rsync源伺服器A

①檢查rsync是否安裝

1 [root@rsync ~]# rpm -q rsync
2 rsync-3.0.9-18.el7.x86_64

②修改配置檔案

[root@rsync ~]# vi /etc/rsyncd.conf
uid = nobody    #去掉#
gid = nobody    #去掉#
use chroot = yes                   #禁錮在宿主目錄中
address = 20.0.0.10              #監聽地址
port 873                                    #埠號
pid file = /var/run/rsyncd.pid       #程序檔案位置
log file = /var/log/rsyncd.log         #日誌檔案位置
hosts allow = 20.0.0.0/24               #允許地址範圍
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2         #不再壓縮這幾種格式的檔案
[wwwroot]
path = /var/www/html                   #同步的目錄
read only = yes                        #只讀
auth users = backuper
secrets file = /etc/rsyncd_users.db      #使用者密碼存放位置

③建立backuper使用者的密碼檔案

1 [root@rsync ~]# vi /etc/rsyncd_users.db
2 backuper:abc123

④給密碼檔案設定執行許可權

1 [root@rsync ~]# chmod 600 /etc/rsyncd_users.db

⑤啟動rsync

[root@rsync ~]# rsync --daemon
[root@rsync ~]# netstat -anpt | grep rsync
tcp        0      0 20.0.0.10:873           0.0.0.0:*               LISTEN      55836/rsync  

⑥安裝httpd,有一個/var/www/html的共享目錄,也可以自己建立

1 [root@rsync ~]# yum -y install httpd
2 [root@rsync ~]# echo "this is test web" > /var/www/html/index.html

3.3、客戶機伺服器B測試

①格式一

[root@client ~]# rsync -avz [email protected]::wwwroot /opt
Password:
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  56.67 bytes/sec
total size is 17  speedup is 0.07
[root@client ~]# cat /opt/index.html          #檢視是否成功
this is test web  

②格式二

刪除同步的檔案再測試
[root@client ~]# rm -rf /opt/index.html
[root@client ~]# cat /opt/index.html
cat: /opt/index.html: 沒有那個檔案或目錄

同步
[root@client ~]# rsync -avz rsync://[email protected]/wwwroot /opt
Password:
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  56.67 bytes/sec
total size is 17  speedup is 0.07
[root@client ~]# cat /opt/index.html
this is test web  

③免密方式登入

建立免密登入檔案
[root@client ~]# vi /etc/server.pass
[root@client ~]# chmod 600 /etc/server.pass
[root@client ~]# rm -rf /opt/index.html
[root@client ~]# rsync -avz --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt
receiving incremental file list
deleting rh/
./
index.html

sent 83 bytes  received 172 bytes  72.86 bytes/sec
total size is 17  speedup is 0.07
[root@client ~]# cat /opt/index.html
this is test web

四、rsync+inotify實時同步

4.1、rsync實時同步

4.1.1、定期同步的不足

①執行備份的時間固定,延遲明顯、實時性差

②當同步源長期不變化時,密集的定期任務是不必要的

4.1.2、實時同步的優點

①一旦同步源出現變化,立即啟動備份

②只要同步源無變化,則不執行備份

4.2、關於inotify

Linux核心的inotify機制

①從版本2.6.13開始提供

②可以從監控檔案系統的變動情況,並做出通知響應

③輔助軟體:inotify-tools

4.3、環境說明

4.4、配置rsync源伺服器A

①將只讀模式關閉

1 [root@rsync ~]# vi /etc/rsyncd.conf 
2 read only = no

②客戶端更改核心引數,指定佇列大小,最多監控例項數,每個例項最多監控檔案數

[root@rsync ~]# cd /var/run
[root@rsync run]# cat rsyncd.pid
14461
[root@rsync run]# kill 14461            #刪除pid檔案
[root@rsync run]# ll | grep rsync.pid
[root@rsync run]# rsync --daemon
[root@rsync run]# ll | grep rsyncd.pid
-rw-r--r--.  1 root           root              6 11月 12 13:19 rsyncd.pid

[root@rsync run]# cat rsyncd.pid
54028
[root@rsync run]# kill -9 54028        #不刪除pid檔案,導致啟動不了
[root@rsync run]# ll | grep rsyncd.pid
-rw-r--r--.  1 root           root              6 11月 12 13:19 rsyncd.pid
[root@rsync run]# rsync --daemon
[root@rsync run]# failed to create pid file /var/run/rsyncd.pid: File exists

[root@rsync run]# rm -rf rsyncd.pid    #刪除pid檔案再啟動
[root@rsync run]# rsync --daemon
[root@rsync run]# netstat -anpt | grep rsync
tcp        0      0 20.0.0.10:873           0.0.0.0:*               LISTEN      5406

4.5、客戶機伺服器B配置

①解壓縮inotify並安裝

inotifywait:用於持續監控,實時輸出結果

inotifywatch:用於短期監控,任務完成後再出結果

1 [root@client ~]# tar zxf inotify-tools-3.14.tar.gz 
2 [root@client ~]# cd inotify-tools-3.14/
3 [root@client inotify-tools-3.14]# ./configure 
4 [root@client inotify-tools-3.14]# make && make install

②測試inotifywait監控命令是否正常使用

 

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

③執行完上面最後一條命令後處於監控狀態,不能做輸入操作,在開啟一個新的終端裝置

另一端終端檔案測試
[root@client html]# touch 12
[root@client html]# touch 13
[root@client html]# rm -rf b.txt
監控段檢視
[root@client ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html
/var/www/html/ MOVED_FROM c.txt
/var/www/html/ MOVED_TO cc.txt
/var/www/html/ CREATE 12
/var/www/html/ CREATE 13

④客戶端上編寫指令碼,將inotify監控和rsync遠端同步結合起來

 

[root@client inotify-tools-3.14]# vi /opt/inotify.sh
#!/bin/bash
INOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"
RSYNC="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/  [email protected]::wwwroot/"
$INOTIFY | while read DIRECTORY EVENT FILE   #逐條讀取監控記錄
do
        if [ $(pgrep rsync | wc -l) -le 0 ];then
            $RSYNC
        fi
done

[root@client inotify-tools-3.14]# chmod +x /opt/inotify.sh
[root@client inotify-tools-3.14]# cd /opt/

注:兩邊的同步目錄許可權都設定777
[root@rsync run]# chmod 777 /var/www/html/
[root@client html]# chmod 777 /var/www/html/

執行指令碼
[root@client opt]# ./inotify.sh

⑤在客戶端/var/www/html目錄下建立檔案,檢視源端/var/www/html目錄是否同步到

客戶端/var/www/html
[root@client html]# vi a.txt
[root@client html]# touch a.txt
[root@client html]# touch b.txt
[root@client html]# touch c.txt
[root@client html]# touch d.txt
檢視源端/var/www/html目錄。新建的和原來就存在的檔案都成功同步,且屬主屬組均為nobody使用者
[root@rsync ~]# cd /var/www/html
-rw-r--r--. 1 nobody nobody   0 11月 12 16:08 a.txt
-rw-r--r--. 1 nobody nobody   0 11月 12 16:08 b.txt
-rw-r--r--. 1 nobody nobody  11 11月 12 12:30 cc.txt
-rw-r--r--. 1 nobody nobody   8 11月 12 12:11 c.html
-rw-r--r--. 1 nobody nobody   0 11月 12 16:08 c.txt
-rw-r--r--. 1 nobody nobody   0 11月 12 16:08 d.txt
drwxrwxrwx. 2 nobody nobody 172 11月 12 15:17 html
-rwxrwxrwx. 1 root   root    21 11月 12 11:07 index.html

五、注意事項

5.1、同步時的注意項

使用如下命令進行上行同步(上傳)時,本地目錄最後要加上/,否則會將目錄同步到對方目標資料夾下,而不僅僅是同步本地目錄下的檔案

①將上面的指令碼源端目錄/var/www/html/改成/var/www/html

[root@client opt]# vi /opt/inotify.sh
#!/bin/bash
INOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html"
RSYNC="rsync -azH --delete --password-file=/etc/server.pass /var/www/html  [email protected]::wwwroot"
$INOTIFY | while read DIRECTORY EVENT FILE   #逐條讀取監控記錄
do
        if [ $(pgrep rsync | wc -l) -le 0 ];then
            $RSYNC
        fi
done

執行指令碼
[root@client opt]# ./inotify.sh

②在客戶端/var/www/html目錄下建立檔案,檢視源端/var/www/html目錄會發現直接同步的時目錄

 

html/
-rw-r--r--. 1 nobody nobody   0 11月 12 15:08 3
-rw-r--r--. 1 nobody nobody   0 11月 12 16:08 a.txt
-rw-r--r--. 1 nobody nobody   0 11月 12 16:08 b.txt
-rw-r--r--. 1 nobody nobody  11 11月 12 12:30 cc.txt
-rw-r--r--. 1 nobody nobody   8 11月 12 12:11 c.html
-rw-r--r--. 1 nobody nobody   0 11月 12 16:08 c.txt
-rw-r--r--. 1 nobody nobody   0 11月 12 16:08 d.txt
html不加/
[root@server2 html]# touch 111
[root@server2 html]# touch 222
[root@server2 html]# touch 333
[root@server2 html]# 
伺服器上顯示
drwxrwxrwx. 2 nobody nobody 4096 11月 12 16:23 html
-rwxrwxrwx. 1 root   root     21 11月 12 11:07 index.html
[root@rsync html]# cd html
[root@rsync html]# ll
-rw-r--r--. 1 nobody nobody  0 11月 12 16:23 111
-rw-r--r--. 1 nobody nobody  0 11月 12 16:23 222
-rw-r--r--. 1 nobody nobody  0 11月 12 16:23 333

5.2、kill pid號和kill -9 pid號的區別

[root@rsync ~]# cd /var/run
[root@rsync run]# cat rsyncd.pid
14461
[root@rsync run]# kill 14461            #刪除pid檔案
[root@rsync run]# ll | grep rsync.pid
[root@rsync run]# rsync --daemon
[root@rsync run]# ll | grep rsyncd.pid
-rw-r--r--.  1 root           root              6 11月 12 13:19 rsyncd.pid

[root@rsync run]# cat rsyncd.pid
54028
[root@rsync run]# kill -9 54028        #不刪除pid檔案,導致啟動不了
[root@rsync run]# ll | grep rsyncd.pid
-rw-r--r--.  1 root           root              6 11月 12 13:19 rsyncd.pid
[root@rsync run]# rsync --daemon
[root@rsync run]# failed to create pid file /var/run/rsyncd.pid: File exists

[root@rsync run]# rm -rf rsyncd.pid    #刪除pid檔案再啟動
[root@rsync run]# rsync --daemon
[root@rsync run]# netstat -anpt | grep rsync
tcp        0      0 20.0.0.10:873           0.0.0.0:*               LISTEN      5406