備份與恢復,尚觀Da y

 本週的課程安排:

    週一:    備份於恢復
    週二:  DNS 服務器的搭建 , ntp服務器的搭建
    週三:    apache服務的搭建 ,dhcp
    週四:    samba服務器的搭建, kernel編譯
    週五:    sendmail服務器的搭建


=======================================

第一、備份與恢復
    
    dump/restore
    rsync
    tar cp

    1、dump/restore備份與恢復

        dump的特點:
                a、只能針對整個分區分區
                b、備份的時候支持基本壓縮
                c、支持鏡像備份和增量備份
                d、能夠支持絕大部分文件類型備份,包括設備文件,他能保留原來文件的屬性


        備份級別:
            鏡像備份--全備份,稱之爲 0 級別,備份的內容最完整;
            增量備份:1-9級別,每個解別遞增都會相對於前一個級別的內容變化備份下來

        備份週期: 從0 --高級別--0 的一個過程


        使用方法:
            第一次備份肯定是需要備份鏡像:
            dump -0u /dev/sda15 -f /tmp/test/sda15_0_full.dump
                #  -0u 指定是完整備份

            接着就是增量備份:
            (刪除部分文件)
            dump -1u /dev/sda15 -f /tmp/test/sda15_1_add.dump
                #  -1u 指定是級別1的增量備份
            (格式化後再存放一些新文件*.onf)
            dump -3u /dev/sda15 -f /tmp/test/sda15_3_add.dump


        數據恢復:
            (恢復之前先把當前狀態的數據備份一下,免得負責任)
            1、要重新格式化你準備恢復的那個分區
            2、重新掛載
            3、鏡像備份恢復:
                cd /恢復路徑
                restore -r -f /tmp/test/sda15_0_full.dump
                    #  -r 重建參數,就是恢復的意思
                    #  -f 指定備份文件的路徑
                restore -r -f /tmp/test/sda15_1_add.dump
                restore -r -f /tmp/test/sda15_3_add.dump
            注意:恢復必須先鏡像備份恢復,然後再到增量備份,恢復增量備份的時候不能跳級    
            
            
        查看備份集裏的文件列表:
        restore -t -f ./sda15_0_full.dump  


        提取文件:
            a、非交互式的提取
        restore  -x -f sda15_0_full.dump /grub   <---提取備份文件裏的一個文件夾
        restore  -x -f sda15_0_full.dump /grub/grub.conf

            b、交互式的提取

[root@dns test]# restore  -i -f sda15_0_full.dump  <---  -i  交互式
restore > ?
restore > ls
restore > add vmlinuz-2.6.28.10   <---把這個文件放到提取列表裏
restore > add grub/*
restore > ls
.:
 System.map                     initrd-2.6.28.10.img
 System.map-2.6.18-128.el5      lost+found/
 System.map-2.6.18-128.el5PAE   restoresymtable
 System.map-2.6.28.10           symvers-2.6.18-128.el5.gz
 System.map-2.6.28.10.old       symvers-2.6.18-128.el5PAE.gz
 config-2.6.18-128.el5          vmlinuz-2.6.18-128.el5
 config-2.6.18-128.el5PAE       vmlinuz-2.6.18-128.el5PAE
*grub/                         *vmlinuz-2.6.28.10     <----打 * 號的代表放在提取列表了
 initrd-2.6.18-128.el5.img      vmlinuz-2.6.28.10.old
 initrd-2.6.18-128.el5PAE.img

restore > extract     《--把文件提取到你運行命令之前的目錄裏
You have not read any volumes yet.
Unless you know which volume your file(s) are on you should start
with the last volume and work towards the first.
Specify next volume # (none if no more volumes): 1   <---踢去的文件是否來自單獨的備份文件
restore: cannot create symbolic link ./grub/menu.lst->./grub.conf: File exists
set owner/mode for '.'? [yn] n  
restore > quit   

        
        
        備份到遠程機器:把A 機器的內容備份到 B 機器
        
            a、配置ssh登錄密鑰
                
ssh-keygen -t dsa
ssh-keygen -t rsa
cat id_dsa.pub > authorized_keys
cat id_rsa.pub >> authorized_keys
scp /root/.ssh/authorized_keys  10.1.1.141:/root/.ssh/


            b、備份操作

RSH=/usr/bin/ssh  rdump -0u  /dev/sda15 -f [email protected]:/tmp/sda15_0_full.dump




    2、rsync
    軟件包: rsync.i386

    a、普通使用
        rsync -azvP  /tmp/test   10.1.1.141:/tmp  <---把test目錄同步過去
        rsync -azvPR  /tmp/test   10.1.1.141:/  <---把相對目錄建立起來
                 --delete  支持增量備份
        缺點: 使用系統帳號的密碼、沒有訪問控制,沒有權限控制,沒有日誌記錄

    b、以服務的形式
        
        1、配置文件: /etc/rsyncd.conf

uid = nobody      <---執行同步時候的身份,指定的時候必須注意,注意文件是否有權限去操作
gid = nobody       <---
use chroot = no   <--- no 代表執行操作的時候可以切換到別的目錄 ,yes就是不能,應爲鎖定了工作目錄
max connections = 4  <---最大連接數
syslog facility = local5 <---指定 產生 syslog日誌信息的設備
pid file = /var/run/rsyncd.pid  <---服務運行時候的PID 存放的地方


[ftp]  《---模塊定義,名字隨便
path = /var/ftp/pub  <---模塊對應的目錄,準備要同步(備份)的目錄
comment = whole ftp area (approx 6.1 GB)  <---註釋


        2、修改服務啓動參數
        rsync的啓動依靠 xinetd 啓動,對應啓動文件 /etc/xinetd.d
 vim /etc/xinetd.d/rsync
service rsync
{
        disable = no   《---把默認值yes改成no 就代表啓用rsync服務
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}

        3、重新啓動服務,讓配置生效
    
service xinetd restart
驗證是否成功打開這個服務:
netstat -ntl | grep 873



        4、測試
用另外一臺電腦去遠程同步服務器上的數據到本地
 rsync -azvP [email protected]::ftp  /backup


例子2:
關於use chroot = no的實現配置:
1. vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no  
max connections = 4
syslog facility = local5
pid file = /var/run/rsyncd.pid


[ftp]
path = /var/ftp/pub
uid = nobody
gid = nobody


2. 創建測試使用的相關文件
echo "can you rsync me ?" > /1111.txt
cd /var/ftp/pub
ln -s /1111/txt  ./linkfile


3. 重啓 service xinetd restart


另外一臺機器測試:rsync -azvPL [email protected]::ftp  /backup  # -L參數就是把軟鏈接文件指向的文件的內容讀取並同步到本地,同步之後文件名字保留軟連接的名字
        效果: cat linkfile   <---看到的是遠程機器 /1111.txt的內容



如果把use chroot = no改成 yes ,並重啓服務,你再用客戶端運行:
    rsync -azvPL [email protected]::ftp  /backup
    就會發現同步linkfile文件失敗



例子3:增加用戶驗證
1、配置文件
uid = nobody
gid = nobody
use chroot = yes
max connections = 4
syslog facility = local5
pid file = /var/run/rsyncd.pid


[ftp]
path = /var/ftp/pub
auth users = tom,bean  <--之允許tom這個用戶登錄並且同步數據,注意:tom,bean必須是系統存在的
secrets file = /etc/rsync.passwd
uid = nobody
gid = nobody

2、創建密碼文件
vim /etc/rsync.passwd
tom:123
bean:123456

chmod 600 !$  <---注意權限問題

3、service xinetd restart

4、測試

例子4:訪問控制,權限控制
uid = nobody
gid = nobody
use chroot = yes
max connections = 4
syslog facility = local5
pid file = /var/run/rsyncd.pid


[ftp]
path = /var/ftp/pub
auth users = tom,bean
read only = yes  <---只讀,也就是隻允許你把數據拿走,但不能把數據同步上來
secrets file = /etc/rsync.passwd
uid = nobody
gid = nobody

重啓服務並測試
[root@station8 backup]# rsync -azvP  /backup  [email protected]::ftp  
Password:
building file list ... done
ERROR: module is read only  <--同步失敗


修改一下參數:read only = no


重啓服務並測試:
    如果 /var/ftp/pub 是755權限就同步失敗,因爲nobody沒有權限去寫
    如果 /var/ftp/pub 是777權限就同步成功

例子5:訪問控制--限制網絡

uid = nobody
gid = nobody
use chroot = yes
max connections = 4
syslog facility = local5
pid file = /var/run/rsyncd.pid


[ftp]
path = /var/ftp/pub
auth users = tom,bean
read only = no
secrets file = /etc/rsync.passwd
uid = nobody
gid = nobody
hosts allow = 10.1.1.0/255.255.255.0,192.168.20.0/255.255.255.0
hosts deny = *

測試


例子6:日誌記錄

uid = nobody
gid = nobody
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
transfer logging = yes
log file = /var/log/rsync.log
log format = %t  %a %m  %f  %b  <---日誌格式 man rsyncd.conf

[ftp]
path = /var/ftp/pub
auth users = tom,bean
read only = no
secrets file = /etc/rsync.passwd
uid = nobody
gid = nobody


參數的說明:
uid / gid  如果遇到權限問題,要麼就修改遇到問題的文件的屬性,要麼就修改 uid 或者 gid ,可以放到局部參數裏設定



客戶端命令使用技巧:

    增量備份:rsync -azvP --delete [email protected]::ftp  /backup

    免去手工輸入密碼:
touch /mypwd
chmod 600 /mypwd
echo "123"  > /mypwd

rsync -azvP --delete --password-file=/mypwd  [email protected]::ftp  /backup


第二、高級文件搜索

    whereis  which locate  find

    locate <---快速定位包含指定關鍵字的路徑,第一運行需要生成數據庫文件: updatedb
    

    find 路徑  [options]

        find /etc   -name "httpd.conf"
        find /etc   -name "*.conf"
[root@dns test]# find /tmp/test  -iname "new1.txt"  《-不區分大小寫
/tmp/test/new1.txt
/tmp/test/new1.tXt    

        find /dev  -type s    
        find /usr/bin  -type l -name "z*"
        find /usr/bin  -type l -name "z*" -exec ls {} /;
        find /etc -perm 400  《--按權限去找文件
        find /etc  -size 40k
        find /etc  -size 1M   <--- 1G
        find /etc  -size +40k  《  大於 40k
        find /etc  -size 40k -name "*.cf" -exec cat {} /;
        find /etc  -mtime -1  <--- 單位:天, -1 一個小時之內修改內容
        find /etc  -ctime +1  <--- 單位:天, +1 一個小時之前,但24小之內屬性修改國的文件
                -atime

[root@dns test]# find ./ -mtime +1  < 修改時間超過 N+1 天
[root@dns test]# find ./ -mtime -1  < 修改時間在一天之內
[root@dns test]# find ./ -mtime +0  < 修改時間超過N+1天

        find /etc  -mmin -1   <---單位是分鐘,-1  一分鐘之內修改的文件 -amin  -cmin

stat new1.txt        
Access: 2009-11-02 11:21:41.000000000 +0800   《--讀取文件內容或執行文件的時候會修改
Modify: 2009-11-02 11:21:41.000000000 +0800   《--修改文件內容的時候才變化
Change: 2009-11-02 11:21:41.000000000 +0800

----
Access: 2009-11-02 11:36:02.000000000 +0800
Modify: 2009-11-02 11:21:41.000000000 +0800
Change: 2009-11-02 11:21:41.000000000 +0800
[root@dns test]# echo "new content"  > new1.txt
Access: 2009-11-02 11:36:02.000000000 +0800
Modify: 2009-11-02 11:38:20.000000000 +0800  <---修改文件內容的時候才變化,這裏改了
Change: 2009-11-02 11:38:20.000000000 +0800  <---這裏也改了  ,只要inode信息變化了,這裏的時間也會變化

        inode table 什麼情況變化?
            修改文件內容、權限、文件名、用戶組、爲他創建硬鏈接、改變擁有者

發佈了44 篇原創文章 · 獲贊 2 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章