个人工具

“UbuntuHelp:BackupYourSystem/TAR”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
第1行: 第1行:
 
{{From|https://help.ubuntu.com/community/BackupYourSystem/TAR}}
 
{{From|https://help.ubuntu.com/community/BackupYourSystem/TAR}}
 
{{Languages|UbuntuHelp:BackupYourSystem/TAR}}
 
{{Languages|UbuntuHelp:BackupYourSystem/TAR}}
 +
#title
 +
== Backup your system using TAR ==
 +
{|border="1" cellspacing="0"
 +
|{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=note.png%7D%7D This wiki article has been edited to meet the needs of wiki users. To view the complete content see [[http://ubuntuforums.org/showpost.php?p=175981&postcount=1|the original forum post]].
 +
|}
 +
{|border="1" cellspacing="0"
 +
|{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=stop.png%7D%7D  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 [[UbuntuHelp:BackupYourSystem|BackupYourSystem]] series of Howtos.
 
This page is part of the [[UbuntuHelp:BackupYourSystem|BackupYourSystem]] series of Howtos.
'''Note: Use at your own risk. It is recommended that you read the whole page before doing anything'''
+
{|border="1" cellspacing="0"
This guide to backup your system using tar to create compressed archives was taken from the post on the Ubuntu Forum written by Heliode. See the thread for discussion: http://www.ubuntuforums.org/showthread.php?t=35087
+
|{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png%7D%7D 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 .
== Introduction ==
+
|}
Hi, and welcome to the Heliode guide to successful backing-up and restoring of a Linux system!
+
=== Preparing for backup ===
Most of you have probably used Windows before you started using Ubuntu. During that time you might have needed to backup and restore your system. For Windows you would need proprietary software for which you would have to reboot your machine and boot into a special environment in which you could perform the backing-up/restoring (programs like Norton Ghost).
+
{|border="1" cellspacing="0"
During that time you might have wondered why it wasn't possible to just add the whole c:\ to a big zip-file. This is impossible because in Windows, there are lots of files you can't copy or overwrite while they are being used, and therefore you needed specialized software to handle this.
+
|{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconHint2.png%7D%7D 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.  
Well, I'm here to tell you that those things, just like rebooting, are Windows Crazy Things (tm). There's no need to use programs like Ghost to create backups of your Ubuntu system (or any Linux system, for that matter). In fact; using Ghost might be a very bad idea if you are using anything but ext2. Ext3, the default Ubuntu partition, is seen by Ghost as a damaged ext2 partition and does a very good job at screwing up your data.
+
|}
== Preparing the Ground ==
+
* Depending on why you're backing up, you ''might'' want to:
Just a quick note... you're about to back up your entire system.  So don't forget to empty your Wastebasket, and any useless files you have hanging around in your /home directory or on the Desktop.
+
* Delete all your emails
Depending on why you're backing up you may want to delete all your emails, clear your browser search history and wipe your saved browser personal details. I do back all of those up, but if you're particularly security conscious you may not want to.
+
* Clear your browser search history
== Backing Up ==
+
* Wipe your saved browser personal details
"What should I use to backup my system then?" you might ask. Easy; the same thing you use to backup/compress everything else; TAR. Unlike Windows, Linux doesn't restrict root access to anything, so you can just throw every single file on a partition in a TAR file!
+
{|border="1" cellspacing="0"
To do this, become root with
+
|{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png%7D%7D  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.
<pre><nowiki>
+
|}
 +
* 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 ===
 +
{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=terminal.png%7D%7D
 +
* Some directories require root or superuser permissions to successfully backup. Gain superuser access by opening a [[UsingTheTerminal|terminal]] and entering:<pre><nowiki>
 
sudo -s -H
 
sudo -s -H
 
</nowiki></pre>
 
</nowiki></pre>
and go to the root of your file system (we use this in our example, but you can go anywhere you want your backup to end up, including remote or removable drives.)
+
* Go to the root of your file system: <pre><nowiki>
<pre><nowiki>
+
 
cd /
 
cd /
 
</nowiki></pre>
 
</nowiki></pre>
Now, below is the full command I would use to make a backup of my system:
+
{|border="1" cellspacing="0"
<pre><nowiki>
+
|{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconHint2.png%7D%7D 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:<pre><nowiki>
 
tar -cvpzf /backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys /
 
tar -cvpzf /backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys /
 
</nowiki></pre>
 
</nowiki></pre>
Now, lets explain this a little bit:
+
* {{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png%7D%7D Now, lets explain this a little bit:
 
* 'tar' is the program used to do a backup
 
* 'tar' is the program used to do a backup
 
* c - create a new backup archive
 
* c - create a new backup archive
 
* v - verbose mode, tar will print what it's doing to the screen
 
* v - verbose mode, tar will print what it's doing to the screen
 
* z - compress the backup file with 'gzip' to make it smaller
 
* 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
+
* 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 dirs aren't very useful to include. Also make sure you don't include the file itself, or else you'll get weird results. You might also not want to include the /mnt folder if you have other partitions mounted there or you'll end up backing those up too. Also make sure you don't have anything mounted in /media (i.e. don't have any cd's or removable media mounted). Either that or exclude /media.
+
* Now come the directories we want to exclude. We don't want to backup everything since some directories aren't very useful to include.
* After all of the options is the directory we want to backup. Since we want to backup everything we use / for the root directory
+
* Make sure you don't include the file itself, or else you'll get weird results.
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, that is, only the file system which you specify and not the file systems mounted under it in the file hierarchy. Use "df" to see which file systems you have mounted. The above command would thus be:
+
* 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.<<BR>>
 +
{|border="1" cellspacing="0"
 +
| {{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=example.png%7D%7D <code><nowiki>--exclude=/mnt/<unwanted_partition></nowiki></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><nowiki>--one-file-system</nowiki></code> option in addition to or instead of <code><nowiki>--exclude</nowiki></code>.
 +
* With the <code><nowiki>--one-file-system</nowiki></code> option, only the "local" file system is backed up. <<BR>>
 +
{|border="1" cellspacing="0"
 +
| {{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png%7D%7D The "local" file system is the file system you have specified, not the file systems mounted under it in the file hierarchy. Use <code><nowiki>df</nowiki></code> to see which file systems you have mounted.  
 +
|}
 +
{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=example.png%7D%7D
 
<pre><nowiki>
 
<pre><nowiki>
 
tar -cvzf /backup.tgz --one-file-system --exclude=/lost+found --exclude=/backup.tgz /
 
tar -cvzf /backup.tgz --one-file-system --exclude=/lost+found --exclude=/backup.tgz /
 
</nowiki></pre>
 
</nowiki></pre>
EDIT: kvidell suggests on the forum thread that we also exclude the /dev directory. I have other evidence that says it is very unwise to do so though.
+
* 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.
Well, if the command agrees with you, hit enter (or return, whatever) and sit back and relax. This might take a while.
+
* {{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=stop.png%7D%7D 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><nowiki>split</nowiki></code> or use a different way to get it onto the DVD. See <code><nowiki>man split</nowiki></code> for further information on <code><nowiki>split</nowiki></code>.
Afterwards you'll have a file called backup.tgz, which is probably pretty large, in the root of your filesystem. Now you can burn it to DVD or move it to another machine; whatever you like!
+
* A possible workaround is the following:<pre><nowiki>
https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconWarning3.png WARNING: Files that are bigger than 2GB (a little less actually) are not supported by ISO9660 and may or may not be restoreable.  So don't simply burn a DVD with a huge .iso file on it.  Split it up using the command split (see man page) or use a different way to get it onto the DVD. One possibility (untested) is the following:
+
<pre><nowiki>
+
 
sudo tar --create --bzip2 --exclude /tmp --one-file-system --sparse / | growisofs -use-the-force-luke -Z /dev/hda=/proc/self/fd/0
 
sudo tar --create --bzip2 --exclude /tmp --one-file-system --sparse / | growisofs -use-the-force-luke -Z /dev/hda=/proc/self/fd/0
 
</nowiki></pre>
 
</nowiki></pre>
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:
+
* Note that this only backs up one file system.  You might want to use <code><nowiki>--exclude</nowiki></code> instead of <code><nowiki>--one-file-system</nowiki></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: <pre><nowiki>
<pre><nowiki>
+
 
sudo tar --extract --bzip2 --file /dev/hda
 
sudo tar --extract --bzip2 --file /dev/hda
 
</nowiki></pre>
 
</nowiki></pre>
EDIT2:
+
{|border="1" cellspacing="0"
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.
+
| {{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconHint2.png%7D%7D 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.
Alternatively, you can use Bzip2 to compress your backup. This means higher compression but lower speed. If compression is important to you, just substitute
+
|}
the 'z' in the command with 'j', and give the backup the right extension.
+
* 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><nowiki>z</nowiki></code> in the command with <code><nowiki>j</nowiki></code>, and change the file name to ''backup.tar.bz2''.
That would make the command look like this:
+
That would make the command look like this: <pre><nowiki>
<pre><nowiki>
+
 
tar -cvpjf /backup.tar.bz2 --exclude=/proc --exclude=/lost+found --exclude=/backup.tar.bz2 --exclude=/mnt --exclude=/sys /
 
tar -cvpjf /backup.tar.bz2 --exclude=/proc --exclude=/lost+found --exclude=/backup.tar.bz2 --exclude=/mnt --exclude=/sys /
 
</nowiki></pre>
 
</nowiki></pre>
== Backup over network ==
+
==== Backup over a network ====
If the file system is low on space and you can't mount another file system to store the backup file on it is
+
* It is possible to use netcat to transfer the backup between computers.
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:<pre><nowiki>
On the receiving end you'll setup netcat to write the backup file like this:
+
<pre><nowiki>
+
 
nc -l -p 1024 > backup.tar.bz2
 
nc -l -p 1024 > backup.tar.bz2
 
</nowiki></pre>
 
</nowiki></pre>
Then you pipe the tar command without the 'f' flag through netcat on the sending end like this:
+
* Then you pipe the tar command without the <code><nowiki>f</nowiki></code> flag through netcat on the sending end like this:<pre><nowiki>
<pre><nowiki>
+
 
tar -cvj <all those other options> / | nc -q 0 <receiving host> 1024
 
tar -cvj <all those other options> / | nc -q 0 <receiving host> 1024
 
</nowiki></pre>
 
</nowiki></pre>
In the above commands 1024 is just a random port number, anything from 1024 and up should work.
+
* 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.
+
* If all goes well the backup will be piped through the network without touching the file system being read.
With a really fast network this could actually be faster then writing the backup file back to disk.
+
* You can also use SSH: <pre><nowiki>
A variation (which I just dreamed up, so I can't testify on its reliability) on the above is this command:
+
<pre><nowiki>
+
tar -cvj <all those other options> / | ssh <remote host> "cat > backup.tar.bz2"
+
</nowiki></pre>
+
edit: GlennBooth
+
I have successfully used the command line below to backup files between two Ubuntu 6.06
+
machines using ssh (a slight variation on the command above):
+
<pre><nowiki>
+
 
tar zcvf - /home | ssh <backuphost> "( cat > home_bkp.tar.gz )"
 
tar zcvf - /home | ssh <backuphost> "( cat > home_bkp.tar.gz )"
 
</nowiki></pre>
 
</nowiki></pre>
Here the directory to backup is /home, and the backup file will end up as home_bkp.tar.gz
+
* {{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png%7D%7D In this example:
on the machine called <backuphost>. Obviously you can adjust the directories to be backed
+
* The directory to backup is ''/home''
up to suit. The hyphen before /home tells tar to send output to stdout rather than to a
+
* The backup destination is ''home_bkp.tar.gz'' on the machine called <backuphost>.
file. Adding the 'p' option to tar is probably a good idea to preserve permissions.
+
* The hyphen before ''/home'' tells tar to send output to stdout rather than to a file.
== Restoring ==
+
* Adding the 'p' option to tar would preserve file permissions.
https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconWarning3.png Warning: 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!
+
=== Restoring ===
Well continue with our example from the previous chapter which created the file backup.tgz in the root directory.
+
{|border="1" cellspacing="0"
Once again, make sure you are root and that you and the backup file are in the root of the file system.
+
| {{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=warning.png%7D%7D 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!  
One of the beautiful things of Linux is that this will even work on a running system; no need to screw around with boot-cd's or anything. Of course, if you've rendered your system unbootable you might have no choice but to use a live cd but the results are the same. You can even remove every single file of a Linux system while it is running with one command. I'm not giving you that command though!
+
|}
Well, back on-topic.
+
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><nowiki>sudo</nowiki></code>.
This is the command that I would use:
+
* Restore your backup:<pre><nowiki>
<pre><nowiki>
+
 
tar -xvpzf /backup.tgz -C /
 
tar -xvpzf /backup.tgz -C /
 
</nowiki></pre>
 
</nowiki></pre>
Or if you used bz2;
+
* If you used bz2: <pre><nowiki>
<pre><nowiki>
+
 
tar -xvpjf backup.tar.bz2 -C /
 
tar -xvpjf backup.tar.bz2 -C /
 
</nowiki></pre>
 
</nowiki></pre>
The x option tells tar to extract the file. The -C <directory> option tells tar to change to a specific directory ( / in this example ) before extracting.
+
* {{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png%7D%7D A brief explanation:
The p option (preserve permissions) restores all file permissions as they originally were (default for superuser).
+
* The x option tells tar to extract the file.
https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconWarning3.png WARNING: this will overwrite every single file on your partition with the one in the archive! So any files that exist on your computer have a chance of being overwritten! Back them up for safe keeping!
+
* The -C <directory> option tells tar to change to a specific directory before extracting. " / " in this example.
Just hit enter/return/your brother/whatever and watch the fireworks. Again, this might take a while. When it is done, you have a fully restored Ubuntu system! Just make sure, before you do anything else, that you re-create the directories which were excluded ( /proc, /lost+found, /mnt, /sys, etc.).
+
* The p option preserves all file permissions. This is default action for tar when used by the superuser.
<pre><nowiki>
+
{|border="1" cellspacing="0"
 +
|{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=warning.png%7D%7D 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. <pre><nowiki>
 
mkdir /proc /lost+found /mnt /sys
 
mkdir /proc /lost+found /mnt /sys
 
</nowiki></pre>
 
</nowiki></pre>
And when you reboot, everything should be the way it was when you made the backup!
+
* Reboot and everything should be restored to the state of your system when you made the backup.
== Comments ==
+
{|border="1" cellspacing="0"
(It may not be *exactly* the way it was when you made the backup,
+
|{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png%7D%7D 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.  
because files created after the backing up won't be deleted).
+
|}
Other people using the "tar" method:
+
==== Restoring over a network ====
* [http://hddsaver.com/content/26/ "Backing Up Ubuntu"]
+
* If you used nc to backup to another computer the commands to restore are:
* [http://weichen.wordpress.com/2007/01/01/howto-backup-my-ubuntu/ "Wei’s world: HOWTO: backup my Ubuntu"]
+
* On the sender side, the side that has the backup file: <pre><nowiki>
== To restore from network ==
+
(this section needs to be improved and integrated on the page)
+
If you used nc to backup to another computer the commands to restore are, on the sender side, the side that has the backup file, type;
+
<pre><nowiki>
+
 
cat backup.tar.bz2 | nc -q 0 <receiving host> 1024
 
cat backup.tar.bz2 | nc -q 0 <receiving host> 1024
 
</nowiki></pre>
 
</nowiki></pre>
In the receiving side mount the disk (if you are running from a [[UbuntuHelp:LiveCD|LiveCD]]) and type;
+
* On the receiving side:
<pre><nowiki>
+
* Mount the disk (if you are running from a [[UbuntuHelp:LiveCD|LiveCD]])and type:<pre><nowiki>
 
nc -l -p 1024 | tar -xvpjf - -C /mnt/disk
 
nc -l -p 1024 | tar -xvpjf - -C /mnt/disk
 
</nowiki></pre>
 
</nowiki></pre>
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. Change "-xvpjf" is for the .bz2 file, change to the appropriate options you used when you created the backup.
+
* The - character will tell tar to accept the input from stdin, the pipe.
== Reformated Partitions ==
+
* The backup file will be expanded without being saved on the disk on the receiver, like when the backup was made.
(this section needs to be improved and integrated on the page)
+
* <code><nowiki>"-xvpjf</nowiki></code> is for a ''.bz2 file'', change <code><nowiki>j</nowiki></code> to <code><nowiki>z</nowiki></code> if you used a ''tar.gz'' backup.
If you had to format partitions, update the file /etc/fstab after restoring the backup. Mount the reformatted partitions on a [[UbuntuHelp:LiveCD|LiveCD]] and type "blkid" on a terminal. Then edit the /etc/fstab file in the restored root partition and change the UUID of the partitions you formatted.
+
=== Reformatted Partitions ===
<pre><nowiki>
+
* If you had to format partitions, update the ''/etc/fstab'' file after restoring the backup.
sudo nano /mnt/disk/etc/fstab
+
<ol><li>Mount the reformatted partitions on a [[UbuntuHelp:LiveCD|LiveCD]].
</nowiki></pre>
+
</li><li>Open a terminal and type <pre><nowiki>
== Restoring GRUB ==
+
blkid</nowiki></pre>
(this section needs to be improved and integrated on the page)
+
</li><li>Making note of the UUIDs, edit the ''/etc/fstab'' in the restored root partition.<pre><nowiki>
If you need to re-install GRUB, take a look at https://help.ubuntu.com/community/RecoveringUbuntuAfterInstallingWindows and check the troubleshooting section - Recovering GRUB Manually. You might have to update the actual partition UUID in the /boot/grub/menu.list that is on the restored partition using the "blkid" command.
+
sudo nano /mnt/disk/etc/fstab</nowiki></pre> Change the UUIDs to match the results of your <code><nowiki>blkid</nowiki></code> command.</li></ol>
[[category:CategoryDocumentation]]  
+
 
 +
=== Restoring GRUB ===
 +
* In most cases restoring GRUB should not be necessary.
 +
* If you need to reinstall grub GRUB see this [[RecoveringUbuntuAfterInstallingWindows|page]].
 +
=== Additional resources ===
 +
* [[http://hddsaver.com/content/26/|"Backing Up Ubuntu"]]
 +
* [[http://weichen.wordpress.com/2007/01/01/howto-backup-my-ubuntu/|"Wei’s world: HOWTO: backup my Ubuntu"]]
 +
----
 +
[[category:CategoryBackupRecovery]] [[category:CategoryCommandLine]]  
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2008年10月19日 (日) 04:19的版本


  1. title

Backup your system using TAR

{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=note.png%7D%7D This wiki article has been edited to meet the needs of wiki users. To view the complete content see [original forum post].
{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=stop.png%7D%7D 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.

{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png%7D%7D 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

{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconHint2.png%7D%7D 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
{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png%7D%7D 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

{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=terminal.png%7D%7D

  • 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 /

{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconHint2.png%7D%7D 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 -cvpzf /backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys /

  • {{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png%7D%7D 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.<
    >
{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=example.png%7D%7D --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. <
    >
{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png%7D%7D 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.

{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=example.png%7D%7D

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.
  • {{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=stop.png%7D%7D 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. See man split for further information on split.
  • 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

{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconHint2.png%7D%7D 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 with j, 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 )"

Restoring

{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=warning.png%7D%7D 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 /

{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=warning.png%7D%7D 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.
{{https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png%7D%7D 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 sender side, the side that has the backup file:
cat backup.tar.bz2 | nc -q 0 <receiving host> 1024
  • 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
  • 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, change j to z if you used a tar.gz backup.

Reformatted Partitions

  • If you had to format partitions, update the /etc/fstab file after restoring the backup.
  1. Mount the reformatted partitions on a LiveCD.
  2. Open a terminal and type
    blkid
  3. Making note of the UUIDs, edit the /etc/fstab in the restored root partition.
    sudo nano /mnt/disk/etc/fstab
    Change the UUIDs to match the results of your blkid command.

Restoring GRUB

  • In most cases restoring GRUB should not be necessary.
  • If you need to reinstall grub GRUB see this page.

Additional resources