一個系統管理員,數據備份是必不可少,在Linux系統下數據備份的工具不少,其中重點介紹就是rsync工具,rsync不只能夠遠程同步數據,還能夠本地同步數據,且不會覆蓋之前的數據在已經存在的數據狀況下,而是先判斷已經存在的數據和新的數據差別,只有不一樣的時候纔會把不一樣的部分覆蓋。vim
如下舉個例子;bash
[root@ask-02 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd服務器sent 1428 bytes received 31 bytes 2918.00 bytes/sec
total size is 1354 speedup is 0.93ssh
能夠看到上列中把/etc/passwd複製到了tmp目錄下並更名爲1.txt。ide
關於rsync的命令格式工具
rsync命令各項的含義以下遞歸
經常使用選項示例演示rem
首先建立須要演示的文件;同步
[root@ask-02 ~]# mkdir rsync-Z
[root@ask-02 ~]# cd rsync-Z/
[root@ask-02 rsync-Z]# mkdir test1
[root@ask-02 rsync-Z]# cd test1/
[root@ask-02 test1]# touch 1 2 3 /root/123.txt
[root@ask-02 test1]# ln -s /root/123.txt ./123.txt
[root@ask-02 test1]# ls -l
總用量 0
-rw-r--r--. 1 root root 0 1月 30 16:33 1
lrwxrwxrwx. 1 root root 13 1月 30 16:34 123.txt -> /root/123.txt
-rw-r--r--. 1 root root 0 1月 30 16:33 2
-rw-r--r--. 1 root root 0 1月 30 16:33 3it
使用-av選項;
[root@ask-02 rsync-Z]# rsync -av test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
123.txt -> /root/123.txt
2
3sent 209 bytes received 75 bytes 568.00 bytes/sec
total size is 13 speedup is 0.05
[root@ask-02 rsync-Z]# ls -l /tmp/test2/
總用量 0
-rw-r--r--. 1 root root 0 1月 30 16:33 1
lrwxrwxrwx. 1 root root 13 1月 30 16:34 123.txt -> /root/123.txt
-rw-r--r--. 1 root root 0 1月 30 16:33 2
-rw-r--r--. 1 root root 0 1月 30 16:33 3
以上示例中我給目錄後都加了斜槓,這是由於若是你不加斜槓,命令會自動建立一個test2目錄,而後將test1目錄放進test2中去,加了斜槓便解決這個問題,因此之後要養成加斜槓的習慣。
使用-L選項;
[root@ask-02 rsync-Z]# rm -rf /tmp/test2
[root@ask-02 rsync-Z]# rsync -avL test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
123.txt
2
3sent 223 bytes received 91 bytes 628.00 bytes/sec
total size is 0 speedup is 0.00
[root@ask-02 rsync-Z]# ls -l /tmp/test2/
總用量 0
-rw-r--r--. 1 root root 0 1月 30 16:33 1
-rw-r--r--. 1 root root 0 1月 30 16:33 123.txt
-rw-r--r--. 1 root root 0 1月 30 16:33 2
-rw-r--r--. 1 root root 0 1月 30 16:33 3
上列加上-L選項便把SRC中軟鏈接的目標文件複製到DST。
使用--delete選項;
[root@ask-02 rsync-Z]# touch /tmp/test2/new.txt
[root@ask-02 rsync-Z]# ls /tmp/test2/
1 123.txt 2 3 new.txt
[root@ask-02 rsync-Z]# rsync -avL --delete test1/ /tmp/test2/
sending incremental file list
./
deleting new.txtsent 67 bytes received 15 bytes 164.00 bytes/sec
total size is 0 speedup is 0.00
[root@ask-02 rsync-Z]# ls !$
ls /tmp/test2/
1 123.txt 2 3
若是在DST增長文件,而SRC當中沒有這些文件,同步時加上--delete選項後就會刪除新增的文件
使用--exclude選項;
[root@ask-02 rsync-Z]# touch test1/4
[root@ask-02 rsync-Z]# ls test1/
1 123.txt 2 3 4
[root@ask-02 rsync-Z]# rsync -avL --exclude="4" test1/ test2/
sending incremental file list
created directory test2
./
1
123.txt
2
3sent 223 bytes received 91 bytes 628.00 bytes/sec
total size is 0 speedup is 0.00
[root@ask-02 rsync-Z]# ls test2
1 123.txt 2 3**
該選項還支持匹配字符(*)使用;
[root@ask-02 rsync-Z]# ls test1
1 123.txt 2 3 345.txt 4 567.txt
[root@ask-02 rsync-Z]# rsync -avL --exclude="*.txt" test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
2
3
4sent 221 bytes received 91 bytes 624.00 bytes/sec
total size is 0 speedup is 0.00
[root@ask-02 rsync-Z]# ls /tmp/test2/
1 2 3 4
使用-P(--progress)選項;
[root@ask-02 rsync-Z]# rm -rf /tmp/test2/
[root@ask-02 rsync-Z]# rsync -avP test1/ /tmp/test2/
sending incremental file list
created directory /tmp/test2
./
1
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=6/8)
123.txt -> /root/123.txt
2
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=4/8)
3
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=3/8)
345.txt
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=2/8)
4
0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=1/8)
567.txt
0 100% 0.00kB/s 0:00:00 (xfer#6, to-check=0/8)sent 372 bytes received 132 bytes 1008.00 bytes/sec
total size is 13 speedup is 0.03
加上-P觀察同步過程狀態。
使用-u選項;
[root@ask-02 rsync-Z]# vim /tmp/test2/567.txt
[root@ask-02 rsync-Z]# cat /tmp/test2/567.txt
hello world
[root@ask-02 rsync-Z]# rsync -avPu test1/ /tmp/test2/
sending incremental file list
./sent 119 bytes received 15 bytes 268.00 bytes/sec
total size is 14 speedup is 0.10
[root@ask-02 rsync-Z]# cat test1/567.txt[root@ask-02 rsync-Z]# cat /tmp/test2/567.txt
hello world
加上-u選項後,能夠看到目標文件的心內容不會被源目錄文件的覆蓋。
前面介紹到的rsync的5中命令格式中,第二種和三種(使用一個冒號)就屬於經過ssh的方式備份數據,這種方式其實就是讓用戶登陸到遠程服務器上執行rsync的任務。
[root@ask-02 rsync-Z]# rsync -av /etc/passwd 192.168.74.128:/tmp/test1.txt
root@192.168.74.128's password:
sending incremental file list
passwdsent 1428 bytes received 31 bytes 224.46 bytes/sec
total size is 1354 speedup is 0.93
若是你的另一臺服務器尚未安裝rsync工具須要先進行安裝
[root@ask-01 ~]# yum install -y rsync
這時你一個cat如下目標服務器上/tmp/下的test1.txt文件,這裏名字是自定義的,你能夠命名爲你熟知的名字
[root@ask-01 ~]# cat /tmp/test1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
固然還能夠從遠程將文件發送到本機上
[root@ask-02 rsync-Z]# rsync -avP 192.168.74.128:/tmp/test1.txt /tmp/cs1.txt
root@192.168.74.128's password:
receiving incremental file list
test1.txt
1354 100% 1.29MB/s 0:00:00 (xfer#1, to-check=0/1)sent 30 bytes received 1436 bytes 101.10 bytes/sec
total size is 1354 speedup is 0.92
一樣的你能夠cat如下本機/tmp/目錄下的cs1.txt文件檢查一下。
若是你的另一臺服務器端口並非22,那麼你能夠加個選項來操做
[root@ask-02 rsync-Z]# rsync -avP -e "ssh -p 22" /etc/passwd/ 192.168.74.128:/tmp/cs1.txt
加上-e選項後面跟遠程命令設定端口。