一、rsync:remove sync(遠程同步),數據備份工具,能夠實現本地同步和遠程同步,同步時能夠增量同步,只同步不相同的數據,也就是說只同步兩個文件不不一樣的部分,所以速度很是快:node
rsync的五種命令格式:注:src表示源文件 dest表示目標文件:shell
rsync options src dest #本地到本地bash
rsync options src user@ip地址:/目錄 #從本地到遠程ssh
rsync options user@ip地址:/目錄 dest #從遠程主機到本地工具
rsync options src user@ip地址::dest #從本地到遠端主機(兩個冒號,驗證方式不一樣)post
rsync options user@ip地址::src dest #從遠端主機到本地(兩個冒號,驗證方式不一樣)ui
註釋:@前面的user能夠省略,省略後則是以目標主機的當前用戶驗證,也能夠寫成普通用戶: rsync -av src xiaoxiao@IP地址:/目錄spa
options:命令行
-a:表示以遞歸方式傳輸文件,包含了-rlptgoD選項: -a--no-p日誌
-r:同步目錄時須要加此選項,相似於cp的-r選項:
-v:verbose,傳輸時可視化:
-l:保留軟鏈接,(由於沒同步軟鏈接的源文件,因此保留後也沒法使用):
-L:同步軟鏈接時,會把軟鏈接所指的源文件也同步過來:
-p:同步時保留源文件的權限:
-o:同步時保留源文g件的屬主信息(如www同步後仍是www,若不存在,則顯示UID):
-g:同步時保留源文件的屬組信息(GID):
-D:同步時保留文件的設備信息:
-t:同步時保留文件時間信息:
--delte:同步時會刪除DEST(目標目錄)裏STC(源目錄)沒有的文件:
--exclude:過濾指定文件,可以使用通配(--exclude "*.txt")
-P:顯示同步過程(更詳細,會顯示速率,傳輸百分比等):
-u:update,若是目標文件比源文件新,則不一樣步源文件過來(新舊根據mtime來判斷的):
-z:傳輸時壓縮文件:
示例1:同步本地文件或者目錄: rsync -av test.txt /tmp/123.txt
[root@localhost_001 ~]# rsync -av test.txt /tmp/123.txt #同步test.txt到tmp目錄下的123.txt sending incremental file list test.txt sent 89 bytes received 35 bytes 248.00 bytes/sec total size is 0 speedup is 0.00 [root@localhost_001 ~]# ls -ld /tmp/123.txt -rw-r--r-- 1 root root 0 8月 14 12:32 /tmp/123.txt [root@localhost_001 ~]# rsync -av test /tmp/test1 #同步test目錄到/tmp/目錄下test1 sending incremental file list created directory /tmp/test1 test/ test/123 test/2112 -> 123 test/bac.txt -> /tmp/ipt.txt sent 409 bytes received 143 bytes 1,104.00 bytes/sec total size is 15 speedup is 0.03 [root@localhost_001 ~]# ls -ld /tmp/test1/ drwxr-xr-x 3 root root 18 8月 14 12:34 /tmp/test1/
示例2:同步本地文件到遠程主機: rsync -av test.txt 192.168.149.130:/tmp/123.txt
rsync -av test.txt root@192.168.149.130:/tmp/123.txt
同步本機的test.txt文件到遠端主機192.168.149.130的tmp目錄下: [root@localhost_001 ~]# rsync -av test.txt root@192.168.149.130:/tmp/1231.txt root@192.168.149.130's password: sending incremental file list test.txt sent 89 bytes received 35 bytes 27.56 bytes/sec total size is 0 speedup is 0.00 在遠端主機130下查看: [root@localhost_002 ~]# ls -ld /tmp/1231.txt -rw-r--r-- 1 root root 0 8月 14 12:32 /tmp/1231.txt
註釋:執行遠程同步時,兩端主機均須要安裝rsync: yum install -y rsync
示例3:同步遠端主機文件到本地: rsync -av root@192.168.149.130:/tmp/1121.txt /tmp/test.txt
[root@localhost_001 ~]# rsync -av 192.168.149.130:/tmp/1231.txt /tmp/test.txt root@192.168.149.130's password: receiving incremental file list 1231.txt sent 43 bytes received 89 bytes 52.80 bytes/sec total size is 0 speedup is 0.00 [root@localhost_001 ~]# ls -ld /tmp/test.txt -rw-r--r-- 1 root root 0 8月 14 12:32 /tmp/test.txt
rsync同步時採用的是ssh協議,須要藉助其端口,有時若是端口發生改變,則須要加 -e 「ssh -p 56588」
同步本機文件/tmp/test.txt到遠端主機130的tmp目錄下,經過其ssh端口「52588」 [root@localhost_001 ~]# rsync -av -e "ssh -p 52588" /tmp/test.txt 192.168.149.130:/tmp/test1.txt root@192.168.149.130's password: sending incremental file list test.txt sent 89 bytes received 35 bytes 35.43 bytes/sec total size is 0 speedup is 0.00 在遠端主機130上查看其文件: [root@localhost_002 ~]# ls -ld /tmp/test1.txt -rw-r--r-- 1 root root 0 8月 14 12:32 /tmp/test1.txt
示例4:--delte:刪除目標目錄中 源文件沒有的文件:
[root@localhost_001 test]# ls /root/test #查看其源文件 123 2112 234 4356 567 bac.txt dir1 dir2 [root@localhost_001 test]# ls /tmp/test/ 123 2112 222 234 4356 567 bac.txt dir1 dir2 #查看其目標文件 [root@localhost_001 test]# rsync -av --delete /root/test/ /tmp/test/ #同步操做 sending incremental file list deleting 222 ./ sent 225 bytes received 28 bytes 506.00 bytes/sec total size is 15 speedup is 0.06
示例5:--exclude 過濾文件:指定文件不一樣步:
[root@localhost_001 ~]# ls test #查看文件,發現有.txt的文件存在: 123 2112 234 4356 567 bac.txt dir1 dir2 [root@localhost_001 ~]# rsync -av --exclude "*.txt" /root/test/ /tmp/test/ #過濾不包含*.txt sending incremental file list created directory /tmp/test ./ 123 2112 -> 123 234 4356 567 dir1/ dir2/ sent 355 bytes received 138 bytes 986.00 bytes/sec total size is 3 speedup is 0.01 [root@localhost_001 ~]# ls /tmp/test/ #再次查看文件,發現沒有txt的文件: 123 2112 234 4356 567 dir1 dir2 支持過濾多個選項,須要加上多個--exclude: [root@localhost_001 ~]# rsync -av --exclude "*.txt" --exclude "1*" /root/test/ /tmp/test/
示例4:同步軟鏈接:須要加大L同步其源文件,否則沒法使用:
在001機器上rsync同步到002機器上: [root@localhost_001 ~]# rsync -avL -e "ssh -p 52588" /root/test/ root@192.168.149.130:/tmp/test1/ root@192.168.149.130's password: bac.txt -> /tmp/ipt.txt dir1/ dir2/ sent 393 bytes received 146 bytes 154.00 bytes/sec total size is 15 speedup is 0.03
示例5:同步是若是目標文件比源文件新則不一樣步: -u update
001主機操做: [root@localhost_001 ~]# rsync -av -e "ssh -p 52588" /root/test.txt 192.168.149.130:/root/test1.txt root@192.168.149.130's password: sending incremental file list test.txt sent 101 bytes received 35 bytes 54.40 bytes/sec total size is 7 speedup is 0.05 002主機: [root@localhost_002 ~]# cat test1.txt dsjdfl;jsafkla;sjf;klsafjk;lsdfjkals;f sdfjksdfjs'ad fsaljflfasdf
示例6:顯示詳細的過程,如速率、百分比等: -p
[root@localhost_001 ~]# rsync -avP -e "ssh -p 52588" /root/test/ root@192.168.149.130:/tmp/test1/ root@192.168.149.130's password: sending incremental file list created directory /tmp/test1 ./ 123 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=7/9) 2112 -> 123 234 0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=5/9) 4356 0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=4/9) 567 0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=3/9) bac.txt -> /tmp/ipt.txt dir1/ dir2/ sent 393 bytes received 146 bytes 119.78 bytes/sec total size is 15 speedup is 0.03
二、客戶端鏈接服務端須要輸入密碼,若想在命令行中帶上密碼,能夠設定一個密碼文件:/etc/rsyncd.passwd(客戶端)
首先在服務端配置/etc/rsyncd.conf下制定驗證的用戶名和密碼所在的配置文件:
[root@localhost_001 ~]# cat /etc/rsyncd.conf |grep -v ^# port=8730 #指定端口 log file=/var/log/rsync.log #指定日記所在文件 pid file=/var/run/rsyncd.pid #PID所在目錄 address=192.168.149.129 #指定啓動了rsync服務的IP地址,默認是當前系統的全部IP地址: [test] #模塊名稱: path=/tmp/rsync #所在的路徑: use chroot=false #是否限制在此目錄: max connections=4 #最大鏈接數: read only=no #是否須要客戶端上傳文件: list=true #是否會在客戶端列出模塊名稱: uid=root #鏈接個人時候的用戶: gid=root #鏈接個人時候的組: auth users=test #制定登陸的用戶名 secrets file=/etc/rsyncd.passwd #制定密碼所在的配置文件: hosts allow=192.168.149.130 #容許那些IP地址或者是網段鏈接我:
如圖:在服務端新建文件:/etc/rsyncd.passwd:格式以下:
[root@localhost_001 test1]# cat /etc/rsyncd.passwd test:12345 用戶 :密碼
而後在客戶端操做推文件和拉文件均需輸入密碼:
[root@localhost_002 ~]# rsync -avL /tmp/test8/ --port=8730 test@192.168.149.129::test/test1/ Password: sending incremental file list ./ 1.txt sent 252 bytes received 40 bytes 34.35 bytes/sec total size is 539 speedup is 1.85 [root@localhost_002 ~]# rsync -avL --port=8730 test@192.168.149.129::test/test1/ /tmp/test8/ Password: receiving incremental file list sent 22 bytes received 202 bytes 29.87 bytes/sec total size is 539 speedup is 2.41
3:有時候咱們須要些在shell腳本里,須要自動化操做:在如上的基礎上,在客戶端新建一個配置文件:裏面只寫入密碼便可:
客戶端: [root@localhost_002 ~]# cat /etc/rsyncd_pass.txt 12345 而後在服務端操做: [root@localhost_002 ~]# rsync -avL /tmp/test8/ --port=8730 test@192.168.149.129::test/test1/ --password-file=/etc/rsyncd_pass.txt sending incremental file list ./ 55.txt sent 273 bytes received 40 bytes 56.91 bytes/sec total size is 539 speedup is 1.72
二、系統日記:配置文件以下:
/var/log/message:系統總日記:(各類各樣的都有)
/etc/loogrotate.conf:日記配置切割文件:
dmesg:硬件錯誤: -c : 清空內存的日記,並從新生成日記:
/var/log/dmesg:系統啓動的日記:
last:用於顯示用戶最近登陸信息,單獨執行last命令,他會讀取/var/log/wtmp(二進制的)的文件:並把該文件打印出來:
[root@localhost_002 ~]# last root pts/1 192.168.149.135 Tue Aug 21 13:03 still logged in root pts/1 192.168.149.135 Tue Aug 21 12:41 - 13:02 (00:21) root pts/0 192.168.149.135 Thu Aug 16 00:37 still logged in root pts/0 192.168.149.135 Wed Aug 15 23:48 - 00:37 (00:49) root pts/0 192.168.149.135 Wed Aug 15 22:27 - 23:48 (01:20) reboot system boot 3.10.0-693.el7.x Wed Aug 15 22:26 - 13:35 (5+15:08)
lastb:顯示用戶登陸錯誤的信息,可發現異常的登陸,單獨執行lastb命令,會讀取/var/log/btmp(二進制)的文件:列出登陸失敗的用戶名單:
[root@localhost_002 ~]# last root pts/1 192.168.149.135 Tue Aug 21 13:03 still logged in root pts/1 192.168.149.135 Tue Aug 21 12:41 - 13:02 (00:21) root pts/0 192.168.149.135 Thu Aug 16 00:37 still logged in root pts/0 192.168.149.135 Wed Aug 15 23:48 - 00:37 (00:49) root pts/0 192.168.149.135 Wed Aug 15 22:27 - 23:48 (01:20) reboot system boot 3.10.0-693.el7.x Wed Aug 15 22:26 - 13:35 (5+15:08)
logrotate:用於對日記的操做,對系統日記進行切割、壓縮和刪除,也能夠將日記發送到指定郵箱,能夠設定每日、每週刪除等,只是必須手動編輯配置文件:/etc/logrotate.conf
安裝logrotate工具:yum install -y logrotate crontabs
Logrotate可配置參數,可以使用man命令來查詢:
compress 經過gzip壓縮轉儲之後的日誌 nocompress 不壓縮 copytruncate 用於還在打開中的日誌文件,把當前日誌備份並截斷 nocopytruncate 備份日誌文件可是不截斷 create mode owner group 轉儲文件,使用指定的文件模式建立新的日誌文件 nocreate 不創建新的日誌文件 delaycompress 和 compress 一塊兒使用時,轉儲的日誌文件到下一次轉儲時才壓縮 nodelaycompress 覆蓋 delaycompress 選項,轉儲同時壓縮。 errors address 專儲時的錯誤信息發送到指定的Email 地址 ifempty 即便是空文件也轉儲,這個是 logrotate 的缺省選項。 notifempty 若是是空文件的話,不轉儲 mail address 把轉儲的日誌文件發送到指定的E-mail 地址 nomail 轉儲時不發送日誌文件 olddir directory 轉儲後的日誌文件放入指定的目錄,必須和當前日誌文件在同一個文件系統 noolddir 轉儲後的日誌文件和當前日誌文件放在同一個目錄下 prerotate/endscript 在轉儲之前須要執行的命令能夠放入這個對,這兩個關鍵字必須單獨成行 postrotate/endscript 在轉儲之後須要執行的命令能夠放入這個對,這兩個關鍵字必須單獨成行 daily 指定轉儲週期爲天天 weekly 指定轉儲週期爲每週 monthly 指定轉儲週期爲每個月 rotate count 指定日誌文件刪除以前轉儲的次數,0 指沒有備份,5 指保留5 個備份 tabootext [+] list 讓logrotate 不轉儲指定擴展名的文件,缺省的擴展名是:.rpm-orig, .rpmsave, v, 和 ~ size size 當日志文件到達指定的大小時才轉儲,bytes(缺省)及KB(sizek)或MB(sizem)
四、screen:虛擬終端,用戶能夠經過此命令同時運行多個終端或者命令行會話,並在其間自由切換,提供統一的會話管理:
不讓某個任務中斷:
安裝screen工具:yum install -y screen
語法:sreen 直接進入虛擬終端:
-ls:列出當前在運行的虛擬終端:
-r:後面加id,進入某個虛擬終端:
-S:後面跟名稱,虛擬終端名稱自定義:
按alt+a,再按d則退出須要screen:
[root@localhost_002 ~]# screen #進入第一個虛擬終端 [detached from 1949.pts-1.localhost_002] [root@localhost_002 ~]# screen #進入第二個虛擬終端 [detached from 1964.pts-1.localhost_002] [root@localhost_002 ~]# screen -S "testyou" #進入第三個自定義的虛擬終端 [detached from 1978.testyou] [root@localhost_002 ~]# [root@localhost_002 ~]# [root@localhost_002 ~]# screen -ls #查看當前的虛擬終端 There are screens on: 1978.testyou (Detached) 1964.pts-1.localhost_002 (Detached) 1949.pts-1.localhost_002 (Detached) 3 Sockets in /var/run/screen/S-root. [root@localhost_002 ~]# screen -r 1978 #進入id爲1978的虛擬終端 [detached from 1978.testyou] [root@localhost_002 ~]# screen -r testyou #也可根據名稱進入某一虛擬終端 [detached from 1978.testyou] 進入後能夠exit,退出虛擬終端 [root@localhost_002 ~]# screen -ls #查看還剩兩個虛擬終端 There are screens on: 1964.pts-1.localhost_002 (Detached) 1949.pts-1.localhost_002 (Detached) 2 Sockets in /var/run/screen/S-root.
擴展:xargs:用做替換工具,用於讀取輸入數據從新格式化後輸出:
[root@localhost_002 ~]# cat test 1 2 3 4 a b c d [root@localhost_002 ~]# cat test|xargs 1 2 3 4 a b c d [root@localhost_002 ~]# cat test|xargs -n3 1 2 3 4 a b c d [root@localhost_002 ~]# cat test|xargs -n2 1 2 3 4 a b c d