天天看點

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

由于裝openstack,很多依賴的東西,而設計環境變量等,一旦搞錯,重裝系統,搞得很麻煩,想辦法備份一下系統。進行還原。方法如下:

tar -zcvpf /home/full-backup.tar.gz / --exclude=/mnt/ --exclude=/proc/ --exclude=/sys/ 這個指令是把根目錄下的所有内容備

tar -zcvpf /home/full-backup.tar.gz / --exclude=/mnt/ --exclude=/proc/ --exclude=/sys/

這個指令是把根目錄下的所有内容備份到full-bakup.tar.gz的備份文檔中

-z表示進行壓縮gzip格式

-c(建立一個備份文檔

-v顯示壓縮過程

-p保留檔案的權限

--directory‘指定不備份的檔案夾

增量備份

1.首先建立完全備份:tar -czvf -g zeng aa.tar.gz /var

待/var中的資料變化後

2.再做增量備份 : tar -czvf -g zeng aa.tar.gz.1 /var

差異備份

1. find /var -mtime -1 -print filelist

産生要備份的檔案的清單 -1表示是一天的時間

2.tar -czv -t filelist -f /backup.tar.gz

依據檔案清單進行備份. -t選項可以指定包含要備份的檔案的清單

參見:http://it.china-b.com/linux/xtgl/20090525/21931_1.html

另外參考:http://zhidao.baidu.com/question/204995317.html

1.==============================================

另外,這裡有比較完整的介紹和說明:http://wiki.ubuntu.org.cn/ubuntuhelp:backupyoursystem/tar/zh

tar -cvzf /backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys /

原文如下:(僅供學習,所有權歸原作者)

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

文章出處: 

<a href="https://help.ubuntu.com/community/backupyoursystem/tar">https://help.ubuntu.com/community/backupyoursystem/tar</a>

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

點選翻譯: 

title

譯注:文章已經經過改動。

<a href="http://wiki.ubuntu.org.cn/ubuntuhelp:backupyoursystem/tar/zh#.e7.94.a8tar.e6.9d.a5.e5.a4.87.e4.bb.bd.e7.b3.bb.e7.bb.9f">1 用tar來備份系統</a>

<a href="http://wiki.ubuntu.org.cn/ubuntuhelp:backupyoursystem/tar/zh#.e4.bb.8b.e7.bb.8d">1.1 介紹</a>

<a href="http://wiki.ubuntu.org.cn/ubuntuhelp:backupyoursystem/tar/zh#.e4.b8.ba.e5.a4.87.e4.bb.bd.e7.b3.bb.e7.bb.9f.e5.81.9a.e5.87.86.e5.a4.87">1.2 為備份系統做準備</a>

<a href="http://wiki.ubuntu.org.cn/ubuntuhelp:backupyoursystem/tar/zh#backing_up">1.3 backing up</a>

<a href="http://wiki.ubuntu.org.cn/ubuntuhelp:backupyoursystem/tar/zh#backup_over_a_network">1.3.1 backup over a network</a>

<a href="http://wiki.ubuntu.org.cn/ubuntuhelp:backupyoursystem/tar/zh#restoring">1.4 restoring</a>

<a href="http://wiki.ubuntu.org.cn/ubuntuhelp:backupyoursystem/tar/zh#restoring_over_a_network">1.4.1 restoring over a network</a>

<a href="http://wiki.ubuntu.org.cn/ubuntuhelp:backupyoursystem/tar/zh#reformatted_partitions">1.5 reformatted partitions</a>

<a href="http://wiki.ubuntu.org.cn/ubuntuhelp:backupyoursystem/tar/zh#restoring_grub">1.6 restoring grub</a>

<a href="http://wiki.ubuntu.org.cn/ubuntuhelp:backupyoursystem/tar/zh#additional_resources">1.7 additional resources</a>

<a></a>

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions
[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 如果你沒有正确使用程式來備份,那你的資料可能就會被你抹去了。是以請在讀完後再動手

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions
[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 just a quick note. you are about to back up your entire system. don't forget to empty your wastebasket, remove any unwanted files in your /home directory, and cleanup your desktop.

depending on why you're backing up, you might want to:

delete all your emails

clear your browser search history

wipe your saved browser personal details

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 if you are not worried about the security concerns, this step is not necessary. many users explicitly want backups of their email and browser settings.

unmount any external media devices, and remove any cds/dvds not needed for the backup process.

this will lessen the amount of exclusions you need to type later in the process.

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

sudo -s -h

go to the root of your file system:

cd /

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 we use the file system root in our example, but you use any target destination you want. you can use remote or removable drives as your backup destination.

create a backup of your system:

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 now, lets explain this a little bit:

'tar' is the program used to do a backup

c - create a new backup archive

v - verbose mode, tar will print what it's doing to the screen

z - compress the backup file with 'gzip' to make it smaller

f &lt;filename&gt; - specifies where to store the backup, /backup.tgz is the file used in this example

now come the directories we want to exclude. we don't want to backup everything since some directories aren't very useful to include.

make sure you don't include the file itself, or else you'll get weird results.

don't include the /mnt folder if you have other partitions mounted there.

if you have partitions in /mnt that require backup, you will need to exclude the folders you do not want backed up.&lt;

&gt;

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 <code>--exclude=/mnt/&lt;unwanted_partition&gt;</code>

make sure you don't have anything mounted in /media.

remove cds/dvds and removable media that you don't need backed up. you can selectively exclude directories in /media if you want removable devices backed up.

after all of the options is the directory we want to backup. since we want to backup everything we use / for the root directory.

if you want to exclude all other file systems you can use the <code>--one-file-system</code> option in addition to or instead of <code>--exclude</code>.

with the <code>--one-file-system</code> option, only the "local" file system is backed up. &lt;

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 the "local" file system is the file system you have specified, not the file systems mounted under it in the file hierarchy. use <code>df</code> to see which file systems you have mounted.

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

relax while tar creates a backup of your system. this make take awhile depending on the amount of data that is being backed up and the speed of your processor. when the process is complete you will have a file named backup.tgz in the root directory of your file system. this file may be burned to a cd/dvd, moved to another partition/drive, or even stored on another machine.

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 files that are bigger than 2gb are not supported by some implementations of iso9660 and may not be restorable. so don't simply burn a dvd with a huge .iso file on it. split it up using the command <code>split</code> or use a different way to get it onto the dvd. see <code>man split</code> for further information on <code>split</code>.

a possible workaround is the following:

sudo tar --create --bzip2 --exclude /tmp --one-file-system --sparse / | growisofs -use-the-force-luke -z /dev/hda=/proc/self/fd/0

note that this only backs up one file system. you might want to use <code>--exclude</code> instead of <code>--one-file-system</code> to filter out the stuff you don't want backed up. this assumes your dvd drive is /dev/hda. this will not create a mountable dvd. to restore it you will reference the device file:

sudo tar --extract --bzip2 --file /dev/hda

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 at the end of the process you might get a message along the lines of 'tar: error exit delayed from previous errors' or something, but in most cases you can just ignore that.

another workaround would be to bzip2 to compress your backup. bzip2 provides a higher compression ratio at the expense of speed. if compression is important to you, just substitute the <code>z</code> in the command with <code>j</code>, and change the file name to backup.tar.bz2.

that would make the command look like this:

it is possible to use netcat to transfer the backup between computers.

on the receiving end you'll setup netcat to write the backup file like this:

nc -l -p 1024 &gt; backup.tar.bz2

then you pipe the tar command without the <code>f</code> flag through netcat on the sending end like this:

tar -cvj &lt;all those other options&gt; / | nc -q 0 &lt;receiving host&gt; 1024

in the above commands 1024 is just a random port number, anything from 1024 and up should work.

if all goes well the backup will be piped through the network without touching the file system being read.

you can also use ssh:

tar zcvf - /home | ssh &lt;backuphost&gt; "( cat &gt; home_bkp.tar.gz )"

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 in this example:

the directory to backup is /home

the backup destination is home_bkp.tar.gz on the machine called &lt;backuphost&gt;.

the hyphen before /home tells tar to send output to stdout rather than to a file.

adding the 'p' option to tar would preserve file permissions.

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 please, for goodness sake, be careful here. if you don't understand what you are doing here you might end up overwriting stuff that is important to you, so please take care!

for the purpose of this tutorial we will assume your backup file is stored in the file system root. we will also assume that you have already gained superuser access through <code>sudo</code>.

restore your backup:

tar -xvpzf /backup.tgz -c /

if you used bz2:

tar -xvpjf backup.tar.bz2 -c /

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 a brief explanation:

the x option tells tar to extract the file.

the -c &lt;directory&gt; option tells tar to change to a specific directory before extracting. " / " in this example.

the p option preserves all file permissions. this is default action for tar when used by the superuser.

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 this will overwrite every single file on your partition with the one in the archive.

the restoration process may take awhile, depending on the size of the archive and the speed of your computer.

once the extraction is complete, re-create the directories which were excluded.

mkdir /proc /lost+found /mnt /sys

reboot and everything should be restored to the state of your system when you made the backup.

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 it may not be exactly the way it was when you made the backup, because files created after the backing up won't be deleted.

if you used nc to backup to another computer the commands to restore are:

on the receiving side:

nc -l -p 1024 | tar -xvpjf - -c /mnt/disk

on the sender side, the side that has the backup file:

cat backup.tar.bz2 | nc -q 0 &lt;receiving host&gt; 1024

the - character will tell tar to accept the input from stdin, the pipe.

the backup file will be expanded without being saved on the disk on the receiver, like when the backup was made.

<code>"-xvpjf</code> is for a .bz2 file, change <code>j</code> to <code>z</code> if you used a tar.gz backup.

if you had to format partitions, update the /etc/fstab and /boot/grub/menu.lst files after restoring the backup.

open a terminal and type

making note of the uuids, edit the /etc/fstab and /boot/grub/menu.lstfiles in the restored root partition.

change the uuids to match the results of your <code>blkid</code> command.

in most cases restoring grub should not be necessary.

if when you boot the system you are left on a grub shell, just do this

find /boot/grub/stage1

substitute the <code>?</code> with the results of your <code>find</code>command.

press esc to restart the system.

2.==============================

參見:https://help.ubuntu.com/community/backupyoursystem/tar

for this example, we will change directories to root. this is where the backup will be made. this is an arbitrary decision, you should create the backup elsewhere. for instance to a mounted external hard drive, another partition or disk connected internally, even a folder in your home directory could be used. in all cases, ensure the location your saving the archive to has enough space. simply use the cd command to navigate there.

the following is an exemplary command of how to archive your system.

to understand what is going on, we will dissect each part of the command.

tar - is the command that creates the archive. it is modified by each letter immediately following, each is explained bellow.

c - create a new backup archive.

v - verbose mode, tar will print what it's doing to the screen.

p - preserves the permissions of the files put in the archive for restoration later.

z - compress the backup file with 'gzip' to make it smaller.

f &lt;filename&gt; - specifies where to store the backup, backup.tar.gz is the filename used in this example. it will be stored in the current working directory, the one you set when you used the cd command.

--exclude=/example/path - the options following this model instruct tar what directories not to backup. we don't want to backup everything since some directories aren't very useful to include. the first exclusion rule directs tar not to back itself up, this is important to avoid errors during the operation. after, we proceed to exclude the /proc, /lost+found, /sys, /mnt, /media and /dev directories in root. /proc and /sys are virtual filesystems that provide windows into variables of the running kernel, so you do not want to try and backup or restore them. /dev is a tmpfs whose contents are created and deleted dynamically by udev, so you also do not want to backup or restore it.

it is important to note that these exclusions are recursive. this means that all folders located within the one excluded will be ignored as well. in the example, excluding the /media folder excludes all mounted drives and media there.

if there are certain partitions you wish to backup located in /media, simply remove the exclusion and write a new one excluding the partitions you don't want backed up stored within it. for example:

/ - after all options is the directory to backup. since we want to backup everything on the system we use / for the root directory. like exclusions, this recursively includes every folder below root not listed in the exclusions or other options.

see tips before operation for additional information.

once satisfied with the command, execute it and wait until it has completed. the duration of the operation depends on the amount of files and compression choses. once completed, check the directory you set to find the archive. in our example, backup.tar.gz would be located in the / directory once completed. this archive can then be moved to any other directory for long term storage.

note: at the end of the process you might get a message along the lines of 'tar: error exit delayed from previous errors' or something, but in most cases you can just ignore that.

to keep good records, you should include the date and a description of backup in the filename.

another option would be to use bzip2 to compress your backup instead of gzip. bzip2 provides a higher compression ratio at the expense of speed. if compression is important to you, just substitute the z in the command with j, and change the file name to .tar.bz2. the rest of this guides examples use gzip, make the subsequent changes to the examples before using them.

if you want to exclude all other mounts other than the current - by this i mean partitions mounted to directories - then use the --one-file-system option appended before the exclusion rules. this has the effect of stopping tar from crossing into any other mounts in any directory including /mnt or /media. for instance, many users create a separate mount for /home to keep user folders separate from root, adding this option to our original example would exclude home's contents entirely.

if you want to burn the archive to discs, or transfer them to a filesystem with a limited max filesize (say fat32 with a limit of 4gb per file) then you will have to split the file either during or after archive creation. a simple means is to use the split command. below are examples of both scenarios. more information than conveyed here can be found in the man pages of split, use man split in a terminal to read. ensure you keep these archives all together in a directory you label for extraction at a later date. once the archives are split to a desirable size, they can be burned one at a time to disc.

to split during creation

the first half until the pipe (|) is identical to our earlier example, except for the omission of the f option. without this, tar will output the archive to standard output, this is then piped to the split command.

-d - this option means that the archive suffix will be numerical instead of alphabetical, each split will be sequential starting with 01 and increasing with each new split file.

-b - this option designates the size to split at, in this example i've made it 3900mb to fit into a fat32 partition.

- - the hyphen is a placeholder for the input file (normally an actual file already created) and directs split to use standard input.

/name/of/backup.tar.gz. is the prefix that will be applied to all generated split files. it should direct to the folder you want the archives to end up. in our example, the first split archive will be in the directory /name/of/ and be named backup.tar.gz.01 .

to split after creation

here instead of using standard input, we are simply splitting an existing file designated by /path/to/backup.tar.gz .

to reconstitute the archive

reconstructing the complete archive is easy, first cd into the directory holding the split archives. then simply use cat to write all the archives into one and send over standard output to tar to extract to the specified directory.

the use of * as a wild card before and after tar.gz tells cat to start with first matching file and add every other that matches, a process known as catenation, how the command got its name.

afterwards, it simply passes all that through standard output to tar to be extracted into root in this example.

the command tar does not include network support within itself, but when used in conjunction with other programs this can be achieved. two common options are netcat (nc) and ssh.

the command nc is designed to be a general purpose networking tool. it sets up a simple connection between two networked machines. this connection survives until the user manually disconnects it, unlike normal connections such as tcp which terminate upon completion of a file.

receiving computer 

on the receiving end you'll setup netcat to write the backup file as in the following example. this command will setup a machine to receive standard input from network to port 1024 then write it to the file backup.tar.gz. the choice of port is entirely up to the user, as long as it is 1024 or larger. a simple example:

sending computer 

on the machine to be backed up, the tar command will be piped to nc which will then send the backup over the network to the port in question to be written in the file. take note, where it says &lt;receiving host&gt; replace with the name of the computer on the network. the f option was omitted since we are not writing to a local file, but moving the archive through standard output. the following is an example:

in the example:

the tar half of the command is the same as above, with the omission of the f option to pipe the archive via standard output to ssh and onto the networked computer.

ssh_backup.tar.gz is the name of the file that will be created on the machine indicated.

&lt;backuphost&gt; - should be replaced with the name of the computer in question on the network.

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 be careful! if you don't understand what you are doing here you might end up overwriting stuff that is important to you. if you are restoring the root directory while booted and using root, you will encounter trouble. operate such a restoration from a live cd, simply mount the drive in question and correct the path as needed.

for the purpose of this tutorial we will assume your backup file is stored in the directory /home/test and named backup.tar.gz. if you are restoring files to directories owned by root, gain superuser access as above by prefacing commands with sudo. ensure your archive is not stored in the directories to be restored, or it will be overwritten during the operation.

restore your backup

a brief explanation:

x - tells tar to extract the file designated by the f option immediately after. in this case, the archive is /home/test/backup.tar.gz

-c &lt;directory&gt; - this option tells tar to change to a specific directory before extracting. in this example, we are restoring to the root (/) directory.

[問題解決型]Linux利用tar備份系統Backing UpRestoringReformatted Partitions

 this will overwrite every single file and directory on the designated mount with the one in the archive. any file created after the archive, will have no equivalent stored in the archive and thus will remain untouched

allow the restoration the time it needs to complete. once extraction is completed, recreate directories that were not included in the original archive. in our example, these would include /proc, /lost+found, /sys, /mnt and /media. this can be done with the following command in our example:

once finished, reboot and everything should be restored to the state of your system when you made the backup.

this short guide, assumes you employed nc to make the original backup as described above.

ensure the disk has been mounted and use the following command to accept input over the network that will then be extracted to the path indicated. in this example, the directory /mnt/disk will be extracted to.

on the computer with the archive to send, use the following command:

a few comments:

the - character in the first command will tell tar to accept input from standard input rather than a file. in this case, input comes from the pipe.

the backup file will be expanded without being saved on the disk of receiving computer, the same as when the backup was made.

if you had to format partitions or are restoring to different discs, update the /etc/fstab and /boot/grub/menu.lst files after restoring the backup.

mount the reformatted partitions on a livecd.

making note of the uuids listed, edit the /etc/fstab and /boot/grub/menu.lst files in the restored root partition to match. use the following commands to edit the files respectively.

change the uuids to match the results of your blkid command.

use this up procedure for logical transfert to disk into vm system bootup with live-cd and create first your file system with one ext2/3 partition and swap partition.the procedure for restore raid1 or raid5 to vm engine request this additional step for regenerate image for upload with grub

if not available mkinitrd command use $sudo apt-get install initrd-tools.now edit grub.conf and change kernel boot parameter root= to reflectpartion without raid e.g. change from /dev/md0 to /dev/sda1, also change the initrd= line to /boot/initrd=noraid-x.x.x.img

now system is ready to next setup grub (step below) and re-boot of virtual machine without raid.

in most cases restoring grub should not be necessary since we are only restoring files.

轉載請注明出處:http://www.cnblogs.com/haochuang/ 8年it工作經驗,5年測試技術與管理,2年産品與項目管理,曾參與過雲計算\雲存儲\車聯網産品研發工作; 業餘自媒體人,有技術類垂直微信公衆号;如有招聘或求職方面需求,請mail to [email protected] ;或通過 qq:363573922 微網誌:@念槐聚 聯系;

繼續閱讀