天天看點

rsync 同步指令

rsync同步指令

    同步操作

rsync [選項..] 源目錄 目标目錄

    同步與複制的差異

複制:完成拷貝源到目标

同步:增量拷貝,隻傳輸變化過的資料

    本地同步

rsync [選項...] 本地目錄1 本地目錄2 #同步整個檔案夾

rsync [選項...] 本地目錄1/ 本地目錄2 #隻同步目錄的資料

操作選項

 -n : 測試同步過程,不做實際修改

 --delete:删除目标檔案夾多餘的文檔

 -a :歸檔模式,相當于-rlptgoD

 -v :顯示詳細操作資訊

 -z :傳輸過程中啟動壓縮/解壓

[root@test ]# mkdir /abc

[root@test ]# mkdir /test

[root@test ]# cp  /etc/passwd /etc/fstab /etc/shadow /etc/group /abc

[root@test ]# rsync -avz /abc /test

 sending incremental file list

 abc/

 abc/fstab

 abc/group

 abc/passwd

 abc/shadow

 sent 2596 bytes  received 92 bytes  5376.00 bytes/sec

 total size is 5551  speedup is 2.07

[root@test ]# ls /test

 abc

[root@test ]# rsync -avz /abc/ /test

 ./

 fstab

 group

 passwd

 shadow

 sent 2581 bytes  received 91 bytes  5344.00 bytes/sec

 total size is 5551  speedup is 2.08

 abc  fstab  group  passwd  shadow

[root@test ]# echo 123 >> /abc/group 

 sent 668 bytes  received 31 bytes  1398.00 bytes/sec

 total size is 5555  speedup is 7.95

[root@test ]# touch /test/test.txt

[root@test ]# rsync -avz --delete /abc/ /test

 deleting abc/shadow

 deleting abc/passwd

 deleting abc/group

 deleting abc/fstab

 deleting abc/

 deleting test.txt

 sent 87 bytes  received 15 bytes  204.00 bytes/sec

 total size is 5555  speedup is 54.46

rsync+ssh同步

下行:rsync [...] user@host:遠端目錄

上行:rsync [...] 本地目錄 user@host:遠端目錄

[root@test  ~]# ls /abc/

 fstab  group  passwd  shadow

[root@test  ~]# rsync -avz --delete /abc/ [email protected]:/opt/

 [email protected]'s password: 

 deleting rh/

    passwd

 sent 2590 bytes  received 91 bytes  766.00 bytes/sec

[root@pc207 ~]# ls /opt/

實時同步

1.密碼驗證取消,采用公鑰 私鑰 驗證

[root@test  ~]# ssh-keygen

 Generating public/private rsa key pair.

 Enter file in which to save the key (/root/.ssh/id_rsa): 

 Enter passphrase (empty for no passphrase): 

 Enter same passphrase again: 

  Your identification has been saved in /root/.ssh/id_rsa.

 Your public key has been saved in /root/.ssh/id_rsa.pub.

 The key fingerprint is:

 45:3d:84:d0:ac:08:4b:9b:84:a5:b4:fc:47:eb:7f:98 [email protected]

 The key's randomart image is:

 +--[ RSA 2048]----+

 |  .o.   .+.+.    |

 | o.o+    .+ o    |

 |  +o =.. ..  .   |

  |   .+.....           |

 |    . o S        |

 |     o           |

 |      .  o       |

 |       .E .      |

  |        ..            |

 +-----------------+

[root@test  ~]# ls /root/.ssh #公私鑰存放位址

 id_rsa  id_rsa.pub  known_hosts

[root@test  ~]# ssh-copy-id [email protected]

[root@pc207 ~]# ls /root/.ssh

 authorized_keys

2.驗證

[root@test  ~]# ssh [email protected]

 Last login: Thu Nov 16 20:07:55 2017 from 192.168.4.254

[root@pc207 ~]# exit

3.inotify實時監控目錄,内容是否變化

  inotify-tools-3.13.tar.gz 

[root@test  ~]# rm -rf /opt/*

[root@test  ~]# tar -xf /tools/inotify-tools-3.13.tar.gz -C /opt/

[root@test  ~]# ls /opt/

inotify-tools-3.13

[root@test  ~]# cd /opt/inotify-tools-3.13/

[root@test  inotify-tools-3.13]# ./configure 

[root@test  inotify-tools-3.13]# make

[root@test  inotify-tools-3.13]# make install

[root@test  inotify-tools-3.13]# inotifywait 

No files specified to watch!

   inotify基本用法

inotifywait [選項 ] 目标檔案

  常用選項

-m:

-r

-q

-e

[root@test  /]# inotifywait -mrq /opt/

/opt/ CREATE 1.txt

/opt/ OPEN 1.txt

/opt/ ATTRIB 1.txt

/opt/ CLOSE_WRITE,CLOSE 1.txt

/opt/ CREATE 2.txt

/opt/ OPEN 2.txt

/opt/ ATTRIB 2.txt

/opt/ CLOSE_WRITE,CLOSE 2.txt

^C

[root@test  /]# 

Shell腳本

[root@test  ~]# vim /root/look.sh

#!/bin/bash

while inotifywait -rqq /abc

do

 rsync -az --delete /abc/ [email protected]:/opt/

done

[root@test  ~]# chmod +x /root/look.sh

[root@test  ~]# /root/look.sh &

[1] 19200

本文轉自 Xuenqlve 51CTO部落格,原文連結:http://blog.51cto.com/13558754/2059275,如需轉載請自行聯系原作者