設為首頁收藏本站

Hippies

 找回密碼
 立即註冊

掃一掃,訪問微社區

搜索
熱搜: 活動 交友 discuz
查看: 2779|回復: 2
打印 上一主題 下一主題

Linux 系統備份與還原

[複製鏈接]
  • TA的每日心情
    開心
    2016-2-27 23:29
  • 簽到天數: 27 天

    [LV.4]偶爾看看III

    438

    主題

    611

    帖子

    705

    積分

    高級會員

    Rank: 4

    積分
    705
    跳轉到指定樓層
    樓主
    發表於 2015-6-30 11:51:33 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式 简体中文繁體中文
    本帖最後由 kevinhu 於 2015-6-30 12:01 編輯

    搞Linux系統有一段時間了,一直都沒好好的思考系統的備份與還原問題,前一陣子處理過把系統整搬移到別顆硬碟的處理,但畢竟不是屬於正常的備份還原的處理方式,因此,想想還是要有個常態的作業規則才行,所以就在網上搜了一下,看看有沒有些文章可以來參考的,簡單的查了下,有鳥哥的教學這算是最完整的學習了,不過我想要的是能夠速成點的,又找了下,找到了另一個保哥的網站,把他在Linux上的備份還原用Linux的基本指令來完成,這正是我想要的方式,與是就照做了一下,基本上還算順利完成,當然還是要把一些路徑調整成符合自己的Server的環境情況才行。
    為了方便日後備忘,除了把鏈接留下外,還抄了部分重點的文件,供日後備忘也供有興趣的人學習參考。
    保哥的 Linux 備份/還原筆記(支援甲機備份,乙機還原)
    原文節錄入下

    Backup shell

    #!/bin/bash
    rnd=$RANDOM
    date=`date +%y%m%d`
    backup_to="/mnt/backup"
    echo -n "Today is $date. Writing /tmp/$rnd-exclude-file-list ... "
    echo "lost+found"                    > /tmp/$rnd-exclude-file-list
    echo "/mnt/*"                       >> /tmp/$rnd-exclude-file-list
    echo "/media/*"                     >> /tmp/$rnd-exclude-file-list
    echo "/proc/*"                      >> /tmp/$rnd-exclude-file-list
    echo "/dev/*"                       >> /tmp/$rnd-exclude-file-list
    echo "/sys/*"                       >> /tmp/$rnd-exclude-file-list
    echo "/tmp/*"                       >> /tmp/$rnd-exclude-file-list
    echo "/var/spool/squid/*"           >> /tmp/$rnd-exclude-file-list
    echo "done."
    echo ""

    #----------------------------------------------------
    datetime=`date "+%Y-%m-%d %H:%M:%S"`
    echo "Backup job started at $datetime"

    #####################################################
    tar -cf $backup_to/$date-full-backup.tar / --totals --absolute-names \
        --ignore-failed-read --exclude-from=/tmp/$rnd-exclude-file-list
    if [ "${?}" != 0 ] ; then    echo "Backup failed."
    fi
    rm -f /tmp/$rnd-exclude-file-list

    #####################################################
    datetime=`date "+%Y-%m-%d %H:%M:%S"`
    echo "Backup job ended at $datetime"
    echo ""
    #----------------------------------------------------
    datetime=`date "+%Y-%m-%d %H:%M:%S"`
    echo "Compressing job started at $datetime"
    #####################################################
    gzip --best --rsyncable $backup_to/$date-full-backup.tar
    if [ "${?}" != 0 ] ; then
        echo "Gzip failed."
    fi
    gzip --test $backup_to/$date-full-backup.tar.gz
    if [ "${?}" != 0 ] ; then
        echo "Check the compressed file integrity failed."
    fi
    #####################################################
    datetime=`date "+%Y-%m-%d %H:%M:%S"`
    echo "Compressing job ended at $datetime"


    Restore 程序

    • 先安裝 Ubuntu 8.04 LTS Server,用最基本的安裝即可
    • 系統正常開機後,先安裝 nfs-common 將 NFS Client 架設起來
      apt-get install nfs-common
    • 掛載我之前備份檔案所在的 NFS 目錄
      mkdir /mnt/backupmount 10.0.0.99:/home/nfs /mnt/backup
    • 執行還原指令,但需保留 /etc/fstab 的設定,因為重新安裝之後硬碟的 UUID 不一樣
      tar -zx --totals --absolute-names --preserve --exclude=/etc/fstab \    -f /mnt/backup/full-backup.tar.gz
      換成較短的指令如下:
      tar -zx -P --preserve --exclude=/etc/fstab -f /mnt/backup/full-backup.tar.gz
    • 查看 /etc/fstab 的內容,將 root 磁碟的 UUID 複製下來
      # /etc/fstab: static file system information.## <file system> <mount point>   <type>  <options>       <dump>  <pass>proc            /proc           proc    defaults        0       0# /dev/sda1UUID=38e447ec-dd3e-4c02-8035-78ca438d2432 /               ext3    relatime,errors=remount-ro 0       1# /dev/sda5UUID=d790099e-a149-4372-977b-52a16a870386 none            swap    sw              0       0/dev/scd0       /media/cdrom0   udf,iso9660 user,noauto,exec,utf8 0       0/dev/fd0        /media/floppy0  auto    rw,user,noauto,exec,utf8 0       0
    • 最後編輯 /boot/grub/menu.lst 檔案,將檔案中所有 UUID 都更換成從 /etc/fstab 中查到的 UUID=38e447ec-dd3e-4c02-8035-78ca438d2432

    請尊重原作者,節錄部分為依原作者的文章部分複製,如有需要完整原文章請至原作者網站向作者洽詢。



    最近訪問 頭像模式 列表模式
  • TA的每日心情
    開心
    2016-2-27 23:29
  • 簽到天數: 27 天

    [LV.4]偶爾看看III

    438

    主題

    611

    帖子

    705

    積分

    高級會員

    Rank: 4

    積分
    705
    沙發
     樓主| 發表於 2016-4-2 21:35:22 | 只看該作者 简体中文繁體中文
    如果要進行整個硬碟系統的備份的話,底下這篇是很適合參考的文章

    How To Do A Full Backup
    Using The tar Command



    It's a good idea to do a full backup of the hard-drive after the initial installation as well as when you finally get your server set up the way you want. Having a snapshot of your system right after the initial installation gives you something to revert back to should you want to reconfigure your server without starting from scratch. Linux has many backup utilities but the old standard is still the favorite of admins because of the flexibility offered by its myriad of options.

    tar commands can become quite complex. It's easier to enter the command in a text file and make it a shell script. We also need to create a directory to hold the backup file. We'll use a separate directory so we can exclude that directory from the backup (we don't want tar trying to backup a file it's in the process of creating). Enter the following commands:


    cd /
    mkdir backups
    cd backups

    Then use the nano editor to create our shell script file with the command:

    nano fullserver.sh

    and enter the following command into it (and don't miss that period at the end of the command):

    tar -cvpf /backups/fullbackup.tar --directory=/ --exclude=proc
    --exclude=sys --exclude=dev/pts --exclude=backups .

    The above command is on multiple lines for readability. Enter everything on the same line when entering the command in the editor. Once entered, exit the editor (Ctrl-X) saving the file.

    The c option creates the backup file.

    The v option gives a more verbose output while the command is running. This option can also be safely eliminated.

    The p option preserves the file and directory permissions.

    The f option needs to go last because it allows you to specify the name and location of the backup file which follows next in the command (in our case this is the /backups/fullbackup.tar file).

    The --directory option tells tar to switch to the root of the file system before starting the backup.

    We --exclude certain directories from the backup because the contents of the directories are dynamically created by the OS. We also want to exclude the directory that contains are backup file.

    Many tar references on the Web will give an exclude example as:

    --exclude=/proc

    However, this will often result in excluded directories still being backed up because there should not be the leading / (slash) character in front of the directory names. If you were to use the above example, tar would back up the proc directory which would result in errors saying that files had changed since they were read.

    In addition to holding your backup file, the /backups directory now holds your script file also. You have to make your script executable before you can run it. To do that and run it enter the following two commands:


    chmod 750 /backups/fullserver.sh
    ./backups/fullserver.sh

    To restore your "tar ball" you need to copy it to the top-most point in the file tree that it backed up. In our case the --directory option told tar to start backing up from the root of the file system so that's where we would want to copy the tar ball to. Then simply replace the c option with the x parameter line so:

    tar -xvpf /fullbackup.tar

    Having a backup file is nice but not of much use if the hard-drive itself fails. If your server is running the wu-ftpd FTP server daemon, you should FTP the tar ball off of your server so that it is stored on a different system.

    Note that with the use of the --exclude statements when doing the backup, the tar backup file (tar ball) isn't something you could use to do a "bare metal" restore (because the excluded directories would be missing). However, it does work very well if you do an initial vanilla install of the OS and then un-tar your tar ball on top of that.

    Saving Space With Compression

    You can compress your backup files to save storage space. Because a lot of Linux files are text files the space savings can be significant. Enough, perhaps, to be able to fit your archive file on a CD or DVD.

    To enable the compression you have to add the z switch to both the tar-ing and untar-ing commands. You also have to change the file name to indicate the compression. Our tar-ing command becomes:

    tar -zcvpf /backups/fullbackup.tar.gz --directory=/ --exclude=proc
    --exclude=sys --exclude=dev/pts --exclude=backups .

    Likewise, our untar-ing command becomes:

    tar -zxvpf /fullbackup.tar.gz

    原文出處 How To Do A Full Backup Using The tar Command
  • TA的每日心情
    開心
    2016-2-27 23:29
  • 簽到天數: 27 天

    [LV.4]偶爾看看III

    438

    主題

    611

    帖子

    705

    積分

    高級會員

    Rank: 4

    積分
    705
    板凳
     樓主| 發表於 2016-4-2 21:39:18 | 只看該作者 简体中文繁體中文
    還有這篇也很好,告訴要如何對Linux全系統的備份方法

    Full System Backup with tar
    您需要登錄後才可以回帖 登錄 | 立即註冊

    本版積分規則

    小黑屋|手機版|Archiver|Hippies 手作皮革工作坊  

    GMT+8, 2024-12-22 17:10 , Processed in 0.103723 second(s), 17 queries , Apc On.

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回復 返回頂部 返回列表