DAY32
rsync 復制軟件應用和實踐
1、什么是rsync
Remote synchronization
開源的,高速的,數(shù)據(jù)復制(copy)的工具
2萤厅、作用
工作中需要定時/實時數(shù)據(jù)備份,不同機器不同機房之間
的數(shù)據(jù)備份這些都可以用rsync來完成
3靴迫、rsync的功能
全量復制和增量復制
cp屬于全量 不管變什么怎么變 全復制
增量復制
只復制有變化的(減少需要有參數(shù))
增量復制原理
使用 quick check算法 只對增量的部分進行復制
根據(jù)大小屬性的變化進行復制
2.x對比差異后復制 3.x一邊對比一邊復制
rsync 3種工作模式
1惕味、本地 local
rsync就是一個命令
a.把數(shù)據(jù)從一個地方復制到另一個地方 相當于cp
b.通過加參數(shù)可以實現(xiàn)刪除的功能 想當于rm
c.查看屬性信息的功能 相當于ls
實踐:
[root@backup ~]# rsync /etc/hosts /opt
[root@backup ~]# ls /opt
hosts
保持屬性 -zrtopg 除了inode都一樣
2、遠程shell模式
借助累次ssh這種隧道來傳輸數(shù)據(jù) 適合不同的機器
pull 拉:從遠端到本地
用戶@主機地址:源路徑? 本地
push 推:從本地到遠端
本地? ? ? 用戶@主機地址:源路徑
null和null/的區(qū)別 null是目錄和目錄下的內(nèi)容
null/只是目錄下的內(nèi)容不包含自身
3玉锌、守護進程模式
首先搭建服務端(要有守護進程) 然后才能在客戶端實現(xiàn)推拉
rsync命令參數(shù)
-avz 或者-vzrtopgDl
rsync 守護進程模式應用實踐
cat >/etc/rsyncd.conf<<EOF
#rsync_config_______________start
#created by oldboy
#site: http://www.oldboyedu.com
uid = rsync? 管理備份目錄的用戶
gid = rsync? 管理備份目錄的用戶組
use chroot = no? 安全功能名挥,數(shù)據(jù)是否鎖定到備份目錄
max connections = 200? 并發(fā)連接 最大連接數(shù)
timeout = 600? 超時時間
pid file = /var/run/rsyncd.pid? 進程號所在文件
lock file = /var/run/rsync.lock 鎖文件
log file = /var/log/rsyncd.log? 日志文件 查看報錯等
ignore errors? 忽略錯誤
read only = false? 不是緊緊只讀 可寫
list = false? ? ? ? 不允許列表
hosts allow = 172.16.1.0/24? 允許連接的主機
hosts deny = 0.0.0.0/32? ? ? 拒絕連接的主機? 工作中二選一
auth users = rsync_backup? ? 遠程虛擬連接用戶
secrets file = /etc/rsync.password? 密碼文件 格式 用戶名:密碼? 權限600
[backup]? ? ? ? ? [模塊] 遠程訪問使用模塊名訪問
comment = welcome to oldboyedu backup!? 說明注釋
path = /backup/? 服務端用于備份的目錄 rsync.rsync?
EOF
man rsyncd.cong 查看配置文件參數(shù)
配置密碼文件
[root@backup ~]# echo "rsync_backup:oldboy" > /etc/rsync.password
[root@backup ~]# chmod 600 /etc/rsync.password
[root@backup ~]# cat /etc/rsync.password
rsync_backup:oldboy
[root@backup ~]# ls -l /etc/rsync.password
-rw------- 1 root root 20 4月? 15 11:51 /etc/rsync.password
啟動和檢查rsync
rsync --demon
systemctl start rsyncd 7 特有
配置客戶端
方法1:認證密碼文件
[root@nfs01 ~]# echo "oldboy" > /etc/rsync.password
[root@nfs01 ~]# chmod 600 /etc/rsync.password
[root@nfs01 ~]# cat /etc/rsync.password
oldboy
[root@nfs01 ~]# ls -l /etc/rsync.password
-rw------- 1 root root 7 4月? 15 11:55 /etc/rsync.password
方法2:
[root@nfs01 ~]# echo ' export RSYNC_PASSWORD=oldboy' >>/etc/bashrc
[root@nfs01 ~]# tail -1 /etc/bashrc
export RSYNC_PASSWORD=oldboy
[root@nfs01 ~]# . /etc/bashrc
[root@nfs01 ~]# echo $RSYNC_PASSWORD
oldboy
守護進程模式 rsync語法
配置服務器端守護進程 實現(xiàn)數(shù)據(jù)傳送
服務器端守護進程? 客戶端使用命令
拉? rsync 參數(shù) [虛擬用戶]@主機地址::模塊名 本地路徑
? ? rsync 參數(shù) rsync://p[虛擬用戶]@主機地址/[模塊名]
推反過來
DAY33
實踐排除復制:
NFS01:
[root@nfs01 ~]# mkdir /data -p
[root@nfs01 ~]# cd /data
[root@nfs01 /data]# touch {a..d}
[root@nfs01 /data]# ls
a? b? c? d
--exclude 排除參數(shù)
例:排除 a b
[root@nfs01 /data]# rsync -avz /data/ --exclude=a --exclude=b? rsync_backup@172.16.1.41::backup
sending incremental file list
./
c
d
排除1到4
[root@nfs01 /data]# touch {1..5}
[root@nfs01 /data]# rsync -avz /data/ --exclude={1..4}? rsync_backup@172.16.1.41::backup
sending incremental file list
./
5
a
b
[root@nfs01 /data]# rsync -avz /data/ --exclude={1,3,a}? rsync_backup@172.16.1.41::backup
sending incremental file list
2
4
--exclude-from 從文件排除
[root@nfs01 /data]# rsync -avz /data/ --exclude-from=./paichu.txt? rsync_backup@172.16.1.41::backup
sending incremental file list
./
16
17
18
19
20
paichu.txt
實踐刪除:
rsync作為鏡像,相當于raid1主守,讓兩臺服務器目錄保持一致禀倔。
--delete 讓兩臺服務器目錄保持一致
推送:
rsync -avz --delete /data/? rsync_backup@172.16.1.41::backup
本地目錄有啥榄融,遠端就有啥。
注意:遠端目錄是不是東西更多蹋艺,多了東西會被刪除剃袍。提前備份backup對應的目錄。
rsync -avz --delete rsync_backup@172.16.1.41::backup /data/
遠端有啥捎谨。本地目錄就有啥,注意本地/data目錄憔维。把/data改成根涛救。提前注意備份本地/data
--partial 支持大文件斷點續(xù)傳
--bwlimit=KBPS 限速。
企業(yè)案例:
某上市公司业扒,白天高峰期某DBA人員從數(shù)據(jù)庫服務器通過rsync將上百GB數(shù)據(jù)復制到備份服務器检吆,導致數(shù)據(jù)庫庫服務器帶寬占滿,造成用戶無法訪問網(wǎng)站的悲劇程储。其實可以利用rsync限速功能蹭沛,將復制速度限制在剩余帶寬的1/3或者1/2,可能就不會出現(xiàn)故障了
1)最簡單的實現(xiàn)章鲤,可以在配置文件結尾加如下內(nèi)容(特殊底紋部分):
[root@backup ~]# cat /etc/rsyncd.conf
#rsync_config_______________start
#created by oldboy
#site: http://www.oldboyedu.com
uid = rsync
gid = rsync
use chroot = no
fake super = yes
max connections = 200
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = welcome to oldboyedu backup.
path = /backup/
[data]? ? ? ? #<==新模塊摊灭。
path = /data/? #<==新備份目錄。
#除了增加以上兩行外败徊,讀者也可以設定更多的獨立配置參數(shù)帚呼,例如:獨立虛擬用戶,獨立密碼文件等皱蹦。
2)建立目錄并授權煤杀。
[root@backup ~]# mkdir -p /data
[root@backup ~]# chown -R rsync.rsync /data
[root@backup ~]# ls -ld /data
drwxr-xr-x 2 rsync rsync 6 4月? 16 10:04 /data
3)重啟rsync服務(只要修改配置,就考慮重載服務)
[root@backup ~]# systemctl restart rsyncd
4)從客戶端訪問測試
[root@backup ~]# ls /data
etc
1沪哺、內(nèi)部人員產(chǎn)生的數(shù)據(jù)(定時備份足矣):
程序員開發(fā)代碼(他電腦上)==>代碼服務上(git/svn代碼版本管理)==>測試環(huán)境測試===>正式環(huán)境
原則上程序代碼可以不備份沈自。
運維人員,寫個定時任務辜妓,寫個備份腳本枯途,更改或增加服務配置rsyncd.conf,需要備份
運維人員修改配置(測試服務器)==>代碼服務器上(git/svn代碼版本管理)==>測試環(huán)境測試===>正式環(huán)境
原則上運維人員的變更可以不備份嫌拣。
2柔袁、用戶產(chǎn)生的數(shù)據(jù)(必須實時備份)
圖片、視頻等文件是放在存儲服務器上的异逐,任意時刻都可能傳上來捶索,必須實時備份。
文本(博客文章)灰瞻,放在數(shù)據(jù)庫里腥例,辅甥,任意時刻都可能發(fā)布,必須實時備份燎竖。
第一個里程碑:
41搭建好rsync服務璃弄,并在31、7上測試成功构回。
第二個里程碑 開發(fā)腳本打包備份
/backup 備份目錄
/var/spool/cron/root /etc/rc.local /server/scripts /var/html/www /app/logs 需要備份的內(nèi)容
模擬創(chuàng)建:
[root@nfs01 ~]# mkdir -p /server/scripts
[root@web01 ~]# mkdir -p /server/scripts /var/html/www /app/logs
web01:
[root@web01 ~]# mkdir -p /backup
[root@web01 ~]# ls -ld /backup/
drwxr-xr-x 2 root root 6 4月? 16 11:36 /backup/
[root@web01 /]# tar zcvhf /backup/bak_$(date +%F_%w).tar.gz /var/spool/cron/root /etc/rc.local /server/scripts /var/html/www /app/logs
tar: 從成員名中刪除開頭的“/”
/var/spool/cron/root
/etc/rc.local
/server/scripts/
/var/html/www/
/app/logs/
[root@web01 /]# ls /backup/
bak_2019-04-16_2.tar.gz
寫腳本:
[root@web01 /]# mkdir /server/scripts/ -p
[root@web01 /]# cd /server/scripts/
[root@web01 /server/scripts]# cat? /server/scripts/bak.sh
[root@web01 /server/scripts]# cat bak.sh
#!/bin/sh
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
export RSYNC_PASSWORD=oldboy
IP=$(hostname -i)
mkdir -p /backup/$IP
#1.打包
tar zchf /backup/$IP/bak_$(date +%F_%w).tar.gz /var/spool/cron/root /etc/rc.local /server/scripts /var/html/www /app/logs &>/dev/null &&\
#2.刪除
find /backup/ -type f -name "*.tar.gz" -mtime +7|xargs rm -f &&\
#3.推送
rsync -az /backup/ rsync_backup@172.16.1.41::backup &>/dev/null
[root@web01 /server/scripts]# /bin/sh /server/scripts/bak.sh
[root@web01 /server/scripts]# ls /backup/
bak_2019-04-16_2.tar.gz
定時任務:
[root@web01 /server/scripts]# crontab -e
[root@web01 /server/scripts]# crontab -l|tail -2
######back.....
00 00 * * * /bin/sh /server/scripts/bak.sh >/dev/null 2>&1
[root@web01 /server/scripts]# find /backup/ -type f -name "*.tar.gz" -mtime +7|xargs rm -f
確保備份完整夏块。
做flag和采集指紋
[root@nfs01 /server/scripts]# #采集人的指紋
[root@nfs01 /server/scripts]# md5sum oldboy.txt >zhiwen.log
[root@nfs01 /server/scripts]# cat zhiwen.log
348bd3ce10ec00ecc29d31ec97cd5839? oldboy.txt
[root@nfs01 /server/scripts]# #校驗
[root@nfs01 /server/scripts]# md5sum -c zhiwen.log
oldboy.txt: 確定
[root@web01 /server/scripts]# cat bak.sh
#!/bin/sh
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
export RSYNC_PASSWORD=oldboy
IP=$(hostname -i)
mkdir -p /backup/$IP
#1.打包
tar zchf /backup/$IP/bak_${IP}_$(date +%F_%w).tar.gz /var/spool/cron/root /etc/rc.local /server/scripts /var/html/www /app/logs &>/dev/null &&\
touch /backup/$IP/bak_${IP}_$(date +%F_%w).flag &&\
#采集指紋
find /backup/ -type f -name "*.tar.gz"|xargs md5sum >/backup/$IP/bak_${IP}_$(date +%F_%w).flag &&\
#2.刪除
find /backup/ -type f -name "*.tar.gz" -mtime +7|xargs rm -f &&\
#3.推送
rsync -az /backup/ rsync_backup@172.16.1.41::backup &>/dev/null
bak_172.16.1.7_2019-04-16_2.tar.gz?
bak_172.16.1.7_2019-04-17_3.tar.gz?
bak_172.16.1.7_2019-04-18_4.tar.gz?
bak_172.16.1.7_2019-04-16_2.tar.gz ?
DAY34
1、什么是NFS纤掸?
全稱 network file system 網(wǎng)絡文件系統(tǒng)
通過網(wǎng)絡存儲和組織文件的一種方法或機制脐供。
什么是文件系統(tǒng)?
2借跪、為什么要用共享存儲政己?
前端所有的應用服務器接收到用戶上傳的圖片、文件掏愁、視頻歇由,都會統(tǒng)一
的放到后端的存儲上。
為什么要共享果港?所有節(jié)點服務器都需要將內(nèi)容存到存儲上沦泌,取的統(tǒng)一來取。
3京腥、共享存儲的種類
單點存儲系統(tǒng)就是NFS赦肃,中小型企業(yè),阿里云服務的NAS服務公浪,OSS對象存儲他宛。
NFS性能不太高。
大型企業(yè)會用分布式存儲FastDFS欠气、Ceph厅各、GlsterFS、Mfs
大型存儲廠商:EMC预柒、Netapp 幾十萬队塘。
藝龍旅行網(wǎng) 存儲用的EMC 傳統(tǒng)企業(yè)。
硬件存儲:傳統(tǒng)企業(yè):穩(wěn)定宜鸯、2臺 雙主機頭 幾十塊硬盤 RAID10憔古。
BAT 曾經(jīng)用硬件,超貴淋袖、
阿里云 去IOE Ibm Orace EMC
4鸿市、NFS工作原理
NFS網(wǎng)絡文件系統(tǒng)
啟動NFS服務,而且還有啟動很多端口。
NFS功能焰情,需要很多服務陌凳。每個服務都有端口,而且經(jīng)常變化内舟。
如何讓客戶端找到這些端口呢合敦?就需要一個經(jīng)紀人(rpc服務)
NFS服務:
1、NFS服務(有很多進程和端口)验游,把自己的端口告訴RPC充岛。
2、RPC服務(對外固定端口111)
客戶端請求NFS服務批狱,先找RPC 111裸准,查到NFS的端口,發(fā)給客戶赔硫。
原理見圖:
5、安裝
服務器端和客戶端都要安裝:
yum install nfs-utils rpcbind -y
rpm -qa nfs-utils rpcbind
[root@nfs01 ~]# rpm -qa nfs-utils rpcbind
nfs-utils-1.3.0-0.61.el7.x86_64
rpcbind-0.2.0-47.el7.x86_64
啟動RPC
[root@nfs01 ~]# systemctl start rpcbind.service
[root@nfs01 ~]# systemctl enable rpcbind.service
看看有沒有注冊的端口
[root@nfs01 ~]# rpcinfo -p 127.0.0.1
? program vers proto? port? service
? ? 100000? ? 4? tcp? ? 111? portmapper
? ? 100000? ? 3? tcp? ? 111? portmapper
? ? 100000? ? 2? tcp? ? 111? portmapper
? ? 100000? ? 4? udp? ? 111? portmapper
? ? 100000? ? 3? udp? ? 111? portmapper
? ? 100000? ? 2? udp? ? 111? portmapper
啟動NFS
[root@nfs01 ~]# systemctl start nfs
[root@nfs01 ~]# systemctl enable nfs
[root@nfs01 ~]# rpcinfo -p 127.0.0.1
? program vers proto? port? service
? ? 100000? ? 4? tcp? ? 111? portmapper
? ? 100000? ? 3? tcp? ? 111? portmapper
? ? 100000? ? 2? tcp? ? 111? portmapper
? ? 100000? ? 4? udp? ? 111? portmapper
? ? 100000? ? 3? udp? ? 111? portmapper
? ? 100000? ? 2? udp? ? 111? portmapper
? ? 100024? ? 1? udp? 44521? status
? ? 100024? ? 1? tcp? 59204? status
? ? 100005? ? 1? udp? 20048? mountd
? ? 100005? ? 1? tcp? 20048? mountd
? ? 100005? ? 2? udp? 20048? mountd
? ? 100005? ? 2? tcp? 20048? mountd
? ? 100005? ? 3? udp? 20048? mountd
? ? 100005? ? 3? tcp? 20048? mountd
? ? 100003? ? 3? tcp? 2049? nfs
? ? 100003? ? 4? tcp? 2049? nfs
? ? 100227? ? 3? tcp? 2049? nfs_acl
? ? 100003? ? 3? udp? 2049? nfs
? ? 100003? ? 4? udp? 2049? nfs
? ? 100227? ? 3? udp? 2049? nfs_acl
? ? 100021? ? 1? udp? 55758? nlockmgr
? ? 100021? ? 3? udp? 55758? nlockmgr
? ? 100021? ? 4? udp? 55758? nlockmgr
? ? 100021? ? 1? tcp? 30472? nlockmgr
? ? 100021? ? 3? tcp? 30472? nlockmgr
? ? 100021? ? 4? tcp? 30472? nlockmgr
[root@nfs01 ~]# netstat -lntup|egrep "rpc|nfs"
tcp? ? ? ? 0? ? ? 0 0.0.0.0:20048? ? ? ? ? 0.0.0.0:*? ? ? LISTEN? ? ? 9516/rpc.mountd? ?
tcp? ? ? ? 0? ? ? 0 0.0.0.0:59204? ? ? ? ? 0.0.0.0:*? ? ? LISTEN? ? ? 9463/rpc.statd? ? ?
tcp6? ? ? 0? ? ? 0 :::20048? ? ? ? ? ? ? ? :::*? ? ? ? ? LISTEN? ? ? 9516/rpc.mountd? ?
tcp6? ? ? 0? ? ? 0 :::14450? ? ? ? ? ? ? ? :::*? ? ? ? ? LISTEN? ? ? 9463/rpc.statd? ? ?
udp? ? ? ? 0? ? ? 0 0.0.0.0:44521? ? ? ? ? 0.0.0.0:*? ? ? ? ? ? ? ? ? 9463/rpc.statd? ? ?
udp? ? ? ? 0? ? ? 0 0.0.0.0:20048? ? ? ? ? 0.0.0.0:*? ? ? ? ? ? ? ? ? 9516/rpc.mountd? ?
udp? ? ? ? 0? ? ? 0 0.0.0.0:695? ? ? ? ? ? 0.0.0.0:*? ? ? ? ? ? ? ? ? 9424/rpcbind? ? ? ?
udp? ? ? ? 0? ? ? 0 127.0.0.1:735? ? ? ? ? 0.0.0.0:*? ? ? ? ? ? ? ? ? 9463/rpc.statd? ? ?
udp6? ? ? 0? ? ? 0 :::20048? ? ? ? ? ? ? ? :::*? ? ? ? ? ? ? ? ? ? ? 9516/rpc.mountd? ?
udp6? ? ? 0? ? ? 0 :::695? ? ? ? ? ? ? ? ? :::*? ? ? ? ? ? ? ? ? ? ? 9424/rpcbind? ? ? ?
udp6? ? ? 0? ? ? 0 :::4835? ? ? ? ? ? ? ? :::*? ? ? ? ? ? ? ? ? ? ? 9463/rpc.statd
6盐肃、配置nfs
NFS配置文件 /etc/exports
[root@nfs01 ~]# man exports
EXAMPLE
? ? ? # sample /etc/exports file
? ? ? /? ? ? ? ? ? ? master(rw) trusty(rw,no_root_squash)
? ? ? /projects? ? ? proj*.local.domain(rw)
? ? ? /usr? ? ? ? ? ? *.local.domain(ro) @trusted(rw)
? ? ? /home/joe? ? ? pc001(rw,all_squash,anonuid=150,anongid=100)
? ? ? /pub? ? ? ? ? ? *(ro,insecure,all_squash)
? ? ? /srv/www? ? ? ? -sync,rw server @trusted @external(ro)
? ? ? /foo? ? ? ? ? ? 2001:db8:9:e54::/64(rw) 192.0.2.0/24(rw)
? ? ? /build? ? ? ? ? buildhost[0-9].local.domain(rw)
? ? ? 待共享的目錄? ? 訪問的主機(權限)
1)待共享的目錄 存東西的目錄 取東西的目錄例如:/data
2)訪問的主機爪膊,
? 172.16.1.7(web01)? ? 單個主機
? 172.16.1.0/24 網(wǎng)段
? 172.16.1.*? ? 網(wǎng)段
? master 主機名
3)()權限
? rw 可讀寫 read write
? ro 只讀 read only
? sync 寫到磁盤才算完成,安全 慢
? async 異步寫到遠程緩沖區(qū)砸王,快 不安全
? 一會再說推盛。。谦铃。耘成。
實踐:
[root@nfs01 ~]# tail -1 /etc/exports
/data 172.16.1.0/24(rw,sync)
[root@nfs01 ~]# mkdir -p /data
[root@nfs01 ~]# ls -ld /data
drwxr-xr-x 2 root root 51 4月? 16 10:24 /data
NFS默認用戶nfsnobody
[root@nfs01 ~]# grep nfsno /etc/passwd
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
[root@nfs01 ~]# chown -R nfsnobody.nfsnobody /data
[root@nfs01 ~]# ls -ld /data
drwxr-xr-x 2 nfsnobody nfsnobody 51 4月? 16 10:24 /data
重啟NFS
[root@nfs01 ~]# systemctl reload nfs #《==生產(chǎn)場景必須要實現(xiàn)平滑重啟。
[root@nfs01 ~]# exportfs -r
上述二者等價驹闰,選一個即可瘪菌。
[root@nfs01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24? #《==看到共享的目錄了
服務單本地掛載:
[root@nfs01 ~]# mount -t nfs 172.16.1.31:/data /mnt
[root@nfs01 ~]# df -h
文件系統(tǒng)? ? ? ? ? 容量? 已用? 可用 已用% 掛載點
/dev/sda3? ? ? ? ? 19G? 1.8G? 18G? 10% /
devtmpfs? ? ? ? ? 476M? ? 0? 476M? ? 0% /dev
tmpfs? ? ? ? ? ? ? 487M? ? 0? 487M? ? 0% /dev/shm
tmpfs? ? ? ? ? ? ? 487M? 7.6M? 479M? ? 2% /run
tmpfs? ? ? ? ? ? ? 487M? ? 0? 487M? ? 0% /sys/fs/cgroup
/dev/sda1? ? ? ? ? 253M? 136M? 118M? 54% /boot
tmpfs? ? ? ? ? ? ? 98M? ? 0? 98M? ? 0% /run/user/0
172.16.1.31:/data? 19G? 1.8G? 18G? 10% /mnt
[root@nfs01 ~]# touch /mnt/oldboy.txt
[root@nfs01 ~]# ls /mnt/
oldboy.txt
換到web01掛載
1)安裝
yum install nfs-utils rpcbind -y
rpm -qa nfs-utils rpcbind
2)啟動
[root@web01 ~]# systemctl start rpcbind
[root@web01 ~]# systemctl enable rpcbind
[root@web01 ~]# netstat -lntup|grep rpc
udp? ? ? ? 0? ? ? 0 0.0.0.0:775? ? ? ? ? ? 0.0.0.0:*? ? ? ? ? ? ? ? ? ? ? ? ? 11624/rpcbind? ? ?
udp6? ? ? 0? ? ? 0 :::775? ? ? ? ? ? ? ? ? :::*? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 11624/rpcbind? ? ?
[root@web01 ~]# netstat -lntup|grep rpcbind
udp? ? ? ? 0? ? ? 0 0.0.0.0:775? 0.0.0.0:*? ? 11624/rpcbind? ? ?
udp6? ? ? 0? ? ? 0 :::775? ? ? ? :::*? ? ? ? ? 11624/rpcbind? ? ?
[root@web01 ~]# ps -ef|grep rpcbind
rpc? ? ? 11624? ? ? 1? 0 12:17 ?? ? ? ? 00:00:00 /sbin/rpcbind -w
3)查看NFS服務器提供的共享目錄
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
nt_create: RPC: Program not registered
? ? ? ? ? ? ? ? ? 程序? 沒有? 注冊
RPC服務開啟了,但是NFS沒有告訴RPC服務端口嘹朗。
4)掛載測試
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /mnt
[root@web01 ~]# df -h
文件系統(tǒng)? ? ? ? ? 容量? 已用? 可用 已用% 掛載點
/dev/sda3? ? ? ? ? 19G? 1.8G? 18G? 10% /
devtmpfs? ? ? ? ? 476M? ? 0? 476M? ? 0% /dev
tmpfs? ? ? ? ? ? ? 487M? ? 0? 487M? ? 0% /dev/shm
tmpfs? ? ? ? ? ? ? 487M? 7.6M? 479M? ? 2% /run
tmpfs? ? ? ? ? ? ? 487M? ? 0? 487M? ? 0% /sys/fs/cgroup
/dev/sda1? ? ? ? ? 253M? 136M? 118M? 54% /boot
tmpfs? ? ? ? ? ? ? 98M? ? 0? 98M? ? 0% /run/user/0
172.16.1.31:/data? 19G? 1.8G? 18G? 10% /mnt
[root@web01 ~]# touch /mnt/oldgirl.txt
[root@web01 ~]# ls /mnt/
oldboy.txt? oldgirl.txt
課后作業(yè)
1师妙、backup客戶端掛載好
2、實現(xiàn)開機自動掛載(fstab里實現(xiàn),rc.local)
3屹培、項目實踐:
? web01 backup客戶端實現(xiàn)掛載到nfs
? NFS下面共享/backup 默穴,允許web01 backup客戶端(/backup)可讀寫.
? web01上傳圖片,backup上可以刪除web01上傳的圖片褪秀。
? ? ? NFS下面共享/data1,允許 web01 backup客戶端10網(wǎng)段只讀(data1)
? 實現(xiàn)開機自動掛載
DAY35
如何做事超出別人的滿意度蓄诽?
1、提前準備媒吗,提前試講(正式最近的模擬講課)仑氛,給別人感覺,不經(jīng)意的蝴猪。
2调衰、牛逼(提前準備 提前5年準備的)膊爪。
3、給自己設定150%目標嚎莉?
為什么fstab無法實現(xiàn)nfs掛載米酬?
回顧:開機啟動流程:
1、磁盤先啟動/etc/fstab
2趋箩、防火墻在前赃额。
3、網(wǎng)卡
NFS網(wǎng)絡文件系統(tǒng)叫确,通過網(wǎng)絡掛載跳芳,網(wǎng)絡沒起如何掛載?
又想在fstab里實現(xiàn)掛載竹勉、就用一個服務飞盆,延遲啟動。
延遲滿足:小孩給棉花糖的故事次乓,學會延遲滿足是成就自己的優(yōu)秀思維吓歇。
[root@nfs01 /data1]# systemctl start remote-fs.target
[root@nfs01 /data1]# systemctl enable remote-fs.target
Created symlink from /etc/systemd/system/multi-user.target.wants/remote-fs.target to /usr/lib/systemd/system/remote-fs.target.
[root@nfs01 /data1]# systemctl status remote-fs.target
● remote-fs.target - Remote File Systems
? Loaded: loaded (/usr/lib/systemd/system/remote-fs.target; enabled; vendor preset: enabled)
? Active: active since 四 2019-04-18 10:17:57 CST; 18s ago
? ? Docs: man:systemd.special(7)
4月 18 10:17:57 nfs01 systemd[1]: Reached target Remote File Systems.
C6:netfs服務
C7:remote-fs.target
3)()權限
? rw 可讀寫 read write
? ro 只讀 read only
? sync 寫到磁盤才算完成,安全 慢
? async 異步寫到遠程緩沖區(qū)票腰,快 不安全
? 一會再說城看。
? all_squash※ 不管客戶端什么用戶,到服務端都是nfsnobody
? anonuid=匿名用戶的UID
? anongid=匿名用戶的GID
[root@nfs01 ~]# cat /etc/exports
#oldboy shared dir at time
#/data 172.16.1.0/24(rw,sync) 10.0.0.0/24(ro)
/data 172.16.1.0/24(rw,sync)
/data1 10.0.0.0/24(ro)
[root@nfs01 ~]# cat /var/lib/nfs/etab
/data1 10.0.0.0/24(ro,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,ro,secure,root_squash,no_all_squash)
/data 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,no_all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,no_all_squash)
更改默認NFS默認用戶
項目實踐2:
? NFS共享的匿名用戶用www杏慰,使得客戶端上傳的圖片都是www用戶测柠,而不是匿名的nfsnobody。
? web01 backup客戶端實現(xiàn)掛載到nfs
? NFS下面共享/backup 缘滥,允許web01 backup客戶端(/backup)可讀寫.
? web01上傳圖片轰胁,backup上可以刪除web01上傳的圖片。
? ? ? NFS下面共享/data1,允許 web01 backup客戶端10網(wǎng)段只讀(data1)
? 實現(xiàn)開機自動掛載
1)nfs01服務端NFS完域、以及所有客戶端:
[root@nfs01 ~]# useradd -u 1111 www
[root@nfs01 ~]# id www
uid=1111(www) gid=1111(www) 組=1111(www)
2)服務端NFS特殊配置
[root@nfs01 ~]# tail -2 /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=1111,anongid=1111)
/data1 10.0.0.0/24(ro)
[root@nfs01 ~]# chown -R www.www /data
[root@nfs01 ~]# ls -ld /data
drwxr-xr-x 2 www www 70 4月? 18 10:05 /data
3)服務端NFS重啟
[root@nfs01 ~]# systemctl reload nfs
4)每個客戶端
mount -t nfs 172.16.1.31:/data /data
[root@web01 ~]# df -h
文件系統(tǒng)? ? ? ? ? 容量? 已用? 可用 已用% 掛載點
172.16.1.31:/data? 19G? 1.8G? 18G? 10% /data
[root@web01 /data]# touch new_web01.txt
[root@web01 /data]# ls -l
總用量 0
-rw-r--r-- 1 www www 0 4月? 16 10:24 ddddf
-rw-r--r-- 1 www www 0 4月? 16 10:23 dddfff
-rw-r--r-- 1 www www 0 4月? 18 11:01 new_web01.txt
-rw-r--r-- 1 www www 0 4月? 17 11:59 oldboy.txt
-rw-r--r-- 1 www www 0 4月? 17 12:30 oldgirl.txt
[root@nfs01 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Mar? 4 11:15:16 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=3a3a295f-88f8-456d-94dc-1a3eeb517c02 /? ? ? ? ? ? ? ? ? ? ? xfs? ? defaults? ? ? ? 0 0
UUID=fd2e0ca7-32be-425f-86a2-85c02b9ec5ea /boot? ? ? ? ? ? ? ? ? xfs? ? defaults? ? ? ? 0 0
UUID=79a3924b-739e-48dc-ab0c-0444b9ac6591 swap? ? ? ? ? ? ? ? ? ? swap? ? defaults,_netdev? 0 0
man mount
_netdev
? ? ? ? ? ? ? The? filesystem? resides on a device that requires network access (used to prevent the? system from attempting to mount these filesystems until the network has? been? enabled? on the system).
自學:fstab被破壞了如何修復软吐?
[root@web01 ~]# cat /etc/fstab
# /etc/fstab
# Created by anaconda on Mon Mar? 4 11:15:16 2019
UUID=3a3a295f-88f8-456d-94dc-1a3eeb517c02 /? ? ? ? ? ? ? ? ? ? ? xfs? ? defaults? ? ? ? 0 0
UUID=fd2e0ca7-32be-425f-86a2-85c02b9ec5ea /boot? ? ? ? ? ? ? ? ? xfs? ? defaults? ? ? ? 0 0
UUID=79a3924b-739e-48dc-ab0c-0444b9ac6591 swap? ? ? ? ? ? ? ? ? ? swap? ? defaults? ? ? ? 0 0
172.16.1.31:/data? ? ? ? ? ? ? ? ? ? ? ? /data? ? ? ? ? ? ? ? ? nfs? ? defaults,soft? ? ? ? 0 0
172.16.1.31:/data? ? ? ? ? ? ? ? ? ? ? ? /data? ? ? ? ? ? ? ? ? nfs? ? defaults,hard,intr? ? ? ? 0
mount -t nfs -o hard,intr,rsize=131072,wsize=131072 172.16.1.31:/data/ /mnt
NFS服務器出問題時候,客戶端重啟依然能夠啟動吟税,可以用如下列兩個方法:
defaults,soft
defaults,hard,intr
#/etc/fstab
客戶端掛載深入
[root@web01 ~]# cat /proc/mounts
debugfs /sys/kernel/debug debugfs rw,relatime 0 0
hugetlbfs /dev/hugepages hugetlbfs rw,relatime 0 0
/dev/sda1 /boot xfs rw,relatime,attr2,inode64,noquota 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
10.0.0.31:/data1 /mnt nfs4 rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=10.0.0.7,local_lock=none,addr=10.0.0.31 0 0
172.16.1.31:/data /data nfs rw,relatime,vers=3,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=172.16.1.31,mountvers=3,mountport=20048,mountproto=udp,local_lock=none,addr=172.16.1.31 0 0
rsize=131072,wsize=131072
hard
proto=tcp
tcp安全穩(wěn)定 udp無連接 FTP服務
Linux正在工作呢凹耙,文件系統(tǒng)只讀。
企業(yè)生產(chǎn)案例文件系統(tǒng)只讀故障/fstab故障肠仪。
自己找一下:
1肖抱、救援模式修復。
2异旧、單用戶意述,mount -o remount,rw /
2)安全加優(yōu)化的掛載方式如下:
mount -t nfs -o nosuid,noexec,nodev,noatime,nodiratime,intr,rsize=131072,wsize=131072 172.16.1.31:/data /mnt
自學:多塊網(wǎng)卡bond
課后作業(yè):項目實踐:
項目實踐2:
? 1.NFS共享的匿名用戶用www,使得客戶端上傳的圖片都是www用戶,而不是匿名的nfsnobody荤崇。
? web01 web02客戶端實現(xiàn)掛載到nfs
? NFS下面共享/backup 拌屏,允許web01 web02客戶端(/backup)可讀寫.
? web01上傳圖片,web02上可以刪除web01上傳的圖片术荤。
? 2.實現(xiàn)開機自動掛載
? 3.掛載的時候要優(yōu)化掛載倚喂。
DAY36
實時復制實踐:
前提:backup rsync服務端部署好。
1)部署NFS客戶端
[root@nfs01 ~]# echo 'export RSYNC_PASSWORD=oldboy' >>/etc/bashrc
[root@nfs01 ~]# source /etc/bashrc
[root@nfs01 ~]# echo $RSYNC_PASSWORD
oldboy
測試推送
[root@nfs01 ~]# rsync -avz /data rsync_backup@172.16.1.41::backup/
sending incremental file list
sent 164 bytes? received 25 bytes? 126.00 bytes/sec
total size is 0? speedup is 0.00
2)查看inotify支持情況
[root@nfs01 ~]# uname -r
3.10.0-957.5.1.el7.x86_64
[root@nfs01 ~]#? ls -l /proc/sys/fs/inotify/
總用量 0
-rw-r--r-- 1 root root 0 4月? 19 09:45 max_queued_events
-rw-r--r-- 1 root root 0 4月? 19 09:45 max_user_instances
-rw-r--r-- 1 root root 0 4月? 19 09:45 max_user_watches
3)安裝inotify-tools
yum install epel-release -y
yum install inotify-tools -y
[root@nfs01 ~]# rpm -ql inotify-tools|head -2
/usr/bin/inotifywait
/usr/bin/inotifywatch
[root@nfs01 ~]# rpm -qa inotify-tools
inotify-tools-3.14-8.el7.x86_64
4)命令參數(shù)和事件知識
略
5)測試實踐
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create /data
6)思考:實現(xiàn)數(shù)據(jù)復制
監(jiān)控哪些事件瓣戚?
增 改 刪 需要監(jiān)控
[root@nfs01 ~]# inotifywait -mrq --format '%w%f' -e close_write,delete /data
/data/a.txt
/data/a.txt
/data/a.txt
7)編寫腳本
mkdir /server/scripts -p
rsync -az --delete /data/ rsync_backup@172.16.1.41::backup
[root@nfs01 /server/scripts]# /bin/sh /server/scripts/monitor1.sh &
[2] 9199
[root@nfs01 /server/scripts]# tail -2 /etc/rc.local
########################
/bin/sh /server/scripts/monitor1.sh &
sersync實踐:
1)客戶端推送成功
rsync -az /data rsync_backup@172.16.1.41::backup
2)部署sersync服務(NFS)
wget https://github.com/wsgzao/sersync/blob/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
sersync_oldboy_64bit_20160928.tar.gz
[root@nfs01 /server/tools]# mkdir /application -p
[root@nfs01 /server/tools]# mv application/sersync /application/
[root@nfs01 /server/tools]# tree /application/
/application/
└── sersync
? ? ├── bin
? ? │?? └── sersync
? ? ├── conf
? ? │?? ├── confxml.xml
? ? │?? └── confxml.xml.ori
? ? ├── logs
? ? │?? └── rsync_fail_log.sh
? ? └── readme.txt
4 directories, 5 files
[root@nfs01 /server/tools]# cd /application/sersync/
[root@nfs01 /application/sersync]# ls
bin? conf? logs? readme.txt
[root@nfs01 /application/sersync]# cd conf/
[root@nfs01 /application/sersync/conf]# ls
confxml.xml? confxml.xml.ori
3)sersync配置文件:干兩件事:
1)完成監(jiān)控配置:
inotifywait -mrq --format '%w%f' -e createFolder,close_write,delete,moveFrom,moveTo /data
2)完整命令拼接:
rsync -az /data --timeout=100 rsync_backup@172.16.1.41::backup
4)啟動服務
[root@nfs01 /application/sersync/conf]# ../bin/sersync -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
參數(shù)-d:啟用守護進程模式
參數(shù)-r:在監(jiān)控前端圈,將監(jiān)控目錄與遠程主機用rsync命令推送一遍
c參數(shù)-n: 指定開啟守護線程的數(shù)量,默認為10個
參數(shù)-o:指定配置文件子库,默認使用confxml.xml文件
參數(shù)-m:單獨啟用其他模塊舱权,使用 -m refreshCDN 開啟刷新CDN模塊
參數(shù)-m:單獨啟用其他模塊,使用 -m socket 開啟socket模塊
參數(shù)-m:單獨啟用其他模塊仑嗅,使用 -m http 開啟http模塊
不加-m參數(shù)宴倍,則默認執(zhí)行同步程序
________________________________________________________________
[root@nfs01 /application/sersync/conf]# /application/sersync/bin/sersync -d -n 10 -o /application/sersync/conf/confxml.xml
最終
/application/sersync/bin/sersync -d
pkill sersync
二進制程序,不是yum安裝的仓技,所以不能systemctl start sersync
配置:systemctl start sersync啟動方案
https://blog.51cto.com/oldboy/2155931
[root@nfs01 /data]# tail -1 /etc/rc.local
/application/sersync/bin/sersync -d
配置:systemctl start sersync啟動方案
https://blog.51cto.com/oldboy/2155931
[root@nfs01 /data]# cat /etc/rc.d/init.d/sersync
#!/bin/bash
# chkconfig: 2345 21 81
# description: rsync service start and stop scripts
# Author: oldboy
# Organization: www.oldboyedu.com
start(){
? ? /application/sersync/bin/sersync -d -o /application/sersync/conf/confxml.xml &>/dev/null
}
stop(){
? ? killall sersync 2>/dev/null
}
case "$1" in
? ? start)
? ? ? ? start
? ? ? ? ;;
? ? stop)
? ? ? ? stop
? ? ? ? ;;
? ? restart)
? ? ? ? stop
? ? ? ? sleep 2
? ? ? ? start
? ? ? ? ;;
? ? *)
? ? ? ? echo $"Usage:$0 {start|stop|restart}"
? ? ? ? exit 1
esac
chmod +x /etc/rc.d/init.d/sersync
[root@nfs01 /data]# cat /usr/lib/systemd/system/sersync.service
[Unit]
Description=sersyncd service
After=network.target
[Service]
Type=forking? ? ? ? ?
ExecStart=/etc/rc.d/init.d/sersync start?
ExecReload=/etc/rc.d/init.d/sersync restart
ExecStop=/etc/rc.d/init.d/sersync stop? ?
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod +x /usr/lib/systemd/system/sersync.service
[root@nfs01 /data]# systemctl enable sersync.service
Created symlink from /etc/systemd/system/multi-user.target.wants/sersync.service to /usr/lib/systemd/system/sersync.service.
[root@nfs01 /data]# systemctl status sersync.service
● sersync.service - sersyncd service
? Loaded: loaded (/usr/lib/systemd/system/sersync.service; enabled; vendor preset: disabled)
? Active: inactive (dead)
[root@nfs01 /data]# systemctl stop sersync
[root@nfs01 /data]# ps -ef|grep sersync|grep -v grep
[root@nfs01 /data]# systemctl start sersync