特殊:Badtitle/NS100:BackupYourSystem/TAR
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/BackupYourSystem/TAR }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/af | • {{#if: UbuntuHelp:BackupYourSystem/TAR|Afrikaans| [[::BackupYourSystem/TAR/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/ar | • {{#if: UbuntuHelp:BackupYourSystem/TAR|العربية| [[::BackupYourSystem/TAR/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/az | • {{#if: UbuntuHelp:BackupYourSystem/TAR|azərbaycanca| [[::BackupYourSystem/TAR/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/bcc | • {{#if: UbuntuHelp:BackupYourSystem/TAR|جهلسری بلوچی| [[::BackupYourSystem/TAR/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/bg | • {{#if: UbuntuHelp:BackupYourSystem/TAR|български| [[::BackupYourSystem/TAR/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/br | • {{#if: UbuntuHelp:BackupYourSystem/TAR|brezhoneg| [[::BackupYourSystem/TAR/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/ca | • {{#if: UbuntuHelp:BackupYourSystem/TAR|català| [[::BackupYourSystem/TAR/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/cs | • {{#if: UbuntuHelp:BackupYourSystem/TAR|čeština| [[::BackupYourSystem/TAR/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/de | • {{#if: UbuntuHelp:BackupYourSystem/TAR|Deutsch| [[::BackupYourSystem/TAR/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/el | • {{#if: UbuntuHelp:BackupYourSystem/TAR|Ελληνικά| [[::BackupYourSystem/TAR/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/es | • {{#if: UbuntuHelp:BackupYourSystem/TAR|español| [[::BackupYourSystem/TAR/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/fa | • {{#if: UbuntuHelp:BackupYourSystem/TAR|فارسی| [[::BackupYourSystem/TAR/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/fi | • {{#if: UbuntuHelp:BackupYourSystem/TAR|suomi| [[::BackupYourSystem/TAR/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/fr | • {{#if: UbuntuHelp:BackupYourSystem/TAR|français| [[::BackupYourSystem/TAR/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/gu | • {{#if: UbuntuHelp:BackupYourSystem/TAR|ગુજરાતી| [[::BackupYourSystem/TAR/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/he | • {{#if: UbuntuHelp:BackupYourSystem/TAR|עברית| [[::BackupYourSystem/TAR/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/hu | • {{#if: UbuntuHelp:BackupYourSystem/TAR|magyar| [[::BackupYourSystem/TAR/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/id | • {{#if: UbuntuHelp:BackupYourSystem/TAR|Bahasa Indonesia| [[::BackupYourSystem/TAR/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/it | • {{#if: UbuntuHelp:BackupYourSystem/TAR|italiano| [[::BackupYourSystem/TAR/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/ja | • {{#if: UbuntuHelp:BackupYourSystem/TAR|日本語| [[::BackupYourSystem/TAR/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/ko | • {{#if: UbuntuHelp:BackupYourSystem/TAR|한국어| [[::BackupYourSystem/TAR/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/ksh | • {{#if: UbuntuHelp:BackupYourSystem/TAR|Ripoarisch| [[::BackupYourSystem/TAR/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/mr | • {{#if: UbuntuHelp:BackupYourSystem/TAR|मराठी| [[::BackupYourSystem/TAR/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/ms | • {{#if: UbuntuHelp:BackupYourSystem/TAR|Bahasa Melayu| [[::BackupYourSystem/TAR/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/nl | • {{#if: UbuntuHelp:BackupYourSystem/TAR|Nederlands| [[::BackupYourSystem/TAR/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/no | • {{#if: UbuntuHelp:BackupYourSystem/TAR|norsk| [[::BackupYourSystem/TAR/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/oc | • {{#if: UbuntuHelp:BackupYourSystem/TAR|occitan| [[::BackupYourSystem/TAR/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/pl | • {{#if: UbuntuHelp:BackupYourSystem/TAR|polski| [[::BackupYourSystem/TAR/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/pt | • {{#if: UbuntuHelp:BackupYourSystem/TAR|português| [[::BackupYourSystem/TAR/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/ro | • {{#if: UbuntuHelp:BackupYourSystem/TAR|română| [[::BackupYourSystem/TAR/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/ru | • {{#if: UbuntuHelp:BackupYourSystem/TAR|русский| [[::BackupYourSystem/TAR/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/si | • {{#if: UbuntuHelp:BackupYourSystem/TAR|සිංහල| [[::BackupYourSystem/TAR/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/sq | • {{#if: UbuntuHelp:BackupYourSystem/TAR|shqip| [[::BackupYourSystem/TAR/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/sr | • {{#if: UbuntuHelp:BackupYourSystem/TAR|српски / srpski| [[::BackupYourSystem/TAR/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/sv | • {{#if: UbuntuHelp:BackupYourSystem/TAR|svenska| [[::BackupYourSystem/TAR/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/th | • {{#if: UbuntuHelp:BackupYourSystem/TAR|ไทย| [[::BackupYourSystem/TAR/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/tr | • {{#if: UbuntuHelp:BackupYourSystem/TAR|Türkçe| [[::BackupYourSystem/TAR/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/vi | • {{#if: UbuntuHelp:BackupYourSystem/TAR|Tiếng Việt| [[::BackupYourSystem/TAR/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/yue | • {{#if: UbuntuHelp:BackupYourSystem/TAR|粵語| [[::BackupYourSystem/TAR/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/zh | • {{#if: UbuntuHelp:BackupYourSystem/TAR|中文| [[::BackupYourSystem/TAR/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/zh-hans | • {{#if: UbuntuHelp:BackupYourSystem/TAR|中文(简体)| [[::BackupYourSystem/TAR/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:BackupYourSystem/TAR | UbuntuHelp:BackupYourSystem/TAR | {{#if: | :}}BackupYourSystem/TAR}}/zh-hant | • {{#if: UbuntuHelp:BackupYourSystem/TAR|中文(繁體)| [[::BackupYourSystem/TAR/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:BackupYourSystem/TAR|:BackupYourSystem/TAR|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :BackupYourSystem/TAR/zh | | {{#ifexist: BackupYourSystem/TAR/zh | | {{#ifeq: {{#titleparts:BackupYourSystem/TAR|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:BackupYourSystem/TAR|1|-1|}} | zh | | }}
- title
Backup your system using TAR
This wiki article has been edited to meet the needs of wiki users. To view the complete content see the original forum post. |
Improper usage of any archival program can cause unintended data loss. Please read the entire tutorial before proceeding. |
Introduction
This page is part of the BackupYourSystem series of Howtos.
This guide to backup your system using tar to create compressed archives is based upon a post from the Ubuntu Community Forums written by Heliode. See the original forum thread for discussion: http://www.ubuntuforums.org/showthread.php?t=35087 . |
Preparing for backup
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
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.
Backing up
- Some directories require root or superuser permissions to successfully backup. Gain superuser access by opening a terminal and entering:
sudo -s -H
- Go to the root of your file system:
cd /
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:
tar -cvzf /backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys /
- 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 <filename> - 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.<
>
--exclude=/mnt/<unwanted_partition>
|
- 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
--one-file-system
option in addition to or instead of--exclude
. - With the
--one-file-system
option, only the "local" file system is backed up. <
>
The "local" file system is the file system you have specified, not the file systems mounted under it in the file hierarchy. Use df to see which file systems you have mounted.
|
tar -cvzf /backup.tgz --one-file-system --exclude=/lost+found --exclude=/backup.tgz /
- 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.
- 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
split
or use a different way to get it onto the DVD. Seeman split
for further information onsplit
. - 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
--exclude
instead of--one-file-system
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
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
z
in the command withj
, and change the file name to backup.tar.bz2.
That would make the command look like this:
tar -cvpjf /backup.tar.bz2 --exclude=/proc --exclude=/lost+found --exclude=/backup.tar.bz2 --exclude=/mnt --exclude=/sys /
Backup over a network
- 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 > backup.tar.bz2
- Then you pipe the tar command without the
f
flag through netcat on the sending end like this:
tar -cvj <all those other options> / | nc -q 0 <receiving host> 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 <backuphost> "( cat > home_bkp.tar.gz )"
- In this example:
- The directory to backup is /home
- The backup destination is home_bkp.tar.gz on the machine called <backuphost>.
- 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.
Restoring
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 sudo
.
- Restore your backup:
tar -xvpzf /backup.tgz -C /
- If you used bz2:
tar -xvpjf backup.tar.bz2 -C /
- A brief explanation:
- The x option tells tar to extract the file.
- The -C <directory> 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.
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.
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. |
Restoring over a network
- If you used nc to backup to another computer the commands to restore are:
- On the receiving side:
- Mount the disk (if you are running from a LiveCD)and type:
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 <receiving host> 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.
"-xvpjf
is for a .bz2 file, changej
toz
if you used a tar.gz backup.
Reformatted Partitions
- If you had to format partitions, update the /etc/fstab and /boot/grub/menu.lst files after restoring the backup.
- Mount the reformatted partitions on a LiveCD.
- Open a terminal and type
blkid
- Making note of the UUIDs, edit the /etc/fstab and /boot/grub/menu.lst files in the restored root partition.
sudo nano /mnt/disk/etc/fstab
sudo nano /mnt/disk/boot/grub/menu.lst
Change the UUIDs to match the results of yourblkid
command.
Restoring GRUB
- 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
root (hd?,?)
setup (hd?)
substitute the ?
with the results of your find
command.
press esc to restart the system.