特殊:Badtitle/NS100:BackupYourSystem/TAR/zh:修订间差异

来自Ubuntu中文
跳到导航跳到搜索
Chenfengyuan留言 | 贡献
新页面: Linux ChatIRC Blog Paste Wiki Forum Ubuntu 搜索 UbuntuHelp 讨论 编辑 历史 移动 监视 简体 繁體 导航 首页 社区 当前事件 最近更改 随机页面 帮助 ...
 
Chenfengyuan留言 | 贡献
 
(未显示同一用户的6个中间版本)
第1行: 第1行:
Linux
{{From|https://help.ubuntu.com/community/BackupYourSystem/TAR}}
ChatIRC
{{Languages|UbuntuHelp:BackupYourSystem/TAR}}
Blog
#title  
Paste
译注:文章已经经过改动。
Wiki
== 用TAR来备份系统 ==
Forum
{|border="1" cellspacing="0"
Ubuntu
|https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=note.png 这篇文章已经被改动过了,以便更好地刊登到wiki上。完整的版本可以看这里[http://ubuntuforums.org/showpost.php?p=175981&postcount=1 the original forum post].
搜索   
|}
UbuntuHelp
{|border="1" cellspacing="0"
讨论
|https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=stop.png  如果你没有正确使用程序来备份,那你的资料可能就会被你抹去了。所以请在读完后再动手
编辑
|}
历史
=== 介绍 ===
移动
这篇文章是HOWTO系列中 [[UbuntuHelp:BackupYourSystem|备份系统]] 的一部分.
监视
{|border="1" cellspacing="0"
简体
|https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png 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 .  
繁體
|}
 
导航
首页
社区
当前事件
最近更改
随机页面
帮助
工具箱
链入页面
链出更改
上传文件
特殊页面
个人工具
Chenfengyuan
我的对话页
我的参数设置
我的监视列表
我的贡献
退出
UbuntuHelp:BackupYourSystem/TAR
出自Ubuntu中文
文章出处:
 
https://help.ubuntu.com/community/BackupYourSystem/TAR  
点击翻译:
 
English
 
请不要直接编辑翻译本页,本页将定期与来源同步。
title  
目录
[隐藏]
1 Backup your system using TAR
1.1 Introduction
1.2 Preparing for backup
1.3 Backing up
1.3.1 Backup over a network
1.4 Restoring
1.4.1 Restoring over a network
1.5 Reformatted Partitions
1.6 Restoring GRUB
1.7 Additional resources
[编辑] 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 -cvpzf /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.
 


=== 为备份系统做准备 ===
{|border="1" cellspacing="0"
|https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconHint2.png 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
{|border="1" cellspacing="0"
|https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png  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
* Some directories require root or superuser permissions to successfully backup. Gain superuser access by opening a [[UbuntuHelp:UsingTheTerminal|terminal]] and entering:<pre><nowiki>
sudo -s -H
</nowiki></pre>
* Go to the root of your file system: <pre><nowiki>
cd /
</nowiki></pre>
{|border="1" cellspacing="0"
|https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconHint2.png 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 -cvzf /backup.tgz --exclude=/proc --exclude=/lost+found --exclude=/backup.tgz --exclude=/mnt --exclude=/sys /
</nowiki></pre>
* https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png 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.<<BR>>
{|border="1" cellspacing="0"
| https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=example.png <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 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
<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 /
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.  
</nowiki></pre>
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.  
* 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.
A possible workaround is the following:
* https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=stop.png 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>.
sudo tar --create --bzip2 --exclude /tmp --one-file-system --sparse / | growisofs -use-the-force-luke -Z /dev/hda=/proc/self/fd/0  
* A possible workaround is the following:<pre><nowiki>
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 --create --bzip2 --exclude /tmp --one-file-system --sparse / | growisofs -use-the-force-luke -Z /dev/hda=/proc/self/fd/0
 
</nowiki></pre>
sudo tar --extract --bzip2 --file /dev/hda  
* 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>
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.
sudo tar --extract --bzip2 --file /dev/hda
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.  
</nowiki></pre>
That would make the command look like this:  
{|border="1" cellspacing="0"
 
| https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconHint2.png 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><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: <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 /
[编辑] Backup over a network  
</nowiki></pre>
It is possible to use netcat to transfer the backup between computers.  
==== Backup over a network ====
On the receiving end you'll setup netcat to write the backup file like this:
* It is possible to use netcat to transfer the backup between computers.
nc -l -p 1024 > backup.tar.bz2  
*On the receiving end you'll setup netcat to write the backup file like this:<pre><nowiki>
Then you pipe the tar command without the f flag through netcat on the sending end like this:
nc -l -p 1024 > backup.tar.bz2
 
</nowiki></pre>
tar -cvj <all those other options> / | nc -q 0 <receiving host> 1024  
* Then you pipe the tar command without the <code><nowiki>f</nowiki></code> flag through netcat on the sending end like this:<pre><nowiki>
In the above commands 1024 is just a random port number, anything from 1024 and up should work.  
tar -cvj <all those other options> / | nc -q 0 <receiving host> 1024
If all goes well the backup will be piped through the network without touching the file system being read.  
</nowiki></pre>
You can also use SSH:  
* In the above commands 1024 is just a random port number, anything from 1024 and up should work.
tar zcvf - /home | ssh <backuphost> "( cat > home_bkp.tar.gz )"  
* If all goes well the backup will be piped through the network without touching the file system being read.
In this example:  
* You can also use SSH: <pre><nowiki>
The directory to backup is /home  
tar zcvf - /home | ssh <backuphost> "( cat > home_bkp.tar.gz )"
The backup destination is home_bkp.tar.gz on the machine called <backuphost>.  
</nowiki></pre>
The hyphen before /home tells tar to send output to stdout rather than to a file.  
* https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png In this example:
Adding the 'p' option to tar would preserve file permissions.  
* The directory to backup is ''/home''
[编辑] Restoring  
* The backup destination is ''home_bkp.tar.gz'' on the machine called <backuphost>.
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!
* 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.
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.  
=== Restoring ===
Restore your backup:
{|border="1" cellspacing="0"
 
| https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=warning.png 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!  
tar -xvpzf /backup.tgz -C /  
|}
If you used bz2:  
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>.
* Restore your backup:<pre><nowiki>
tar -xvpzf /backup.tgz -C /
</nowiki></pre>
* If you used bz2: <pre><nowiki>
tar -xvpjf backup.tar.bz2 -C /
</nowiki></pre>
* https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=info.png 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.
{|border="1" cellspacing="0"
|https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=warning.png 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
</nowiki></pre>
* Reboot and everything should be restored to the state of your system when you made the backup.
{|border="1" cellspacing="0"
|https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=IconNote.png 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 [[UbuntuHelp:LiveCD|LiveCD]])and type:<pre><nowiki>
nc -l -p 1024 | tar -xvpjf - -C /mnt/disk
</nowiki></pre>
* On the sender side, the side that has the backup file: <pre><nowiki>
cat backup.tar.bz2 | nc -q 0 <receiving host> 1024
</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.
* <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.
=== Reformatted Partitions ===
* If you had to format partitions, update the ''/etc/fstab'' and ''/boot/grub/menu.lst'' files after restoring the backup.
<ol><li>Mount the reformatted partitions on a [[UbuntuHelp:LiveCD|LiveCD]].
</li><li>Open a terminal and type <pre><nowiki>
blkid</nowiki></pre>
</li><li>Making note of the UUIDs, edit the ''/etc/fstab'' and ''/boot/grub/menu.lst'' files in the restored root partition.<pre><nowiki>
sudo nano /mnt/disk/etc/fstab</nowiki></pre><pre><nowiki>


tar -xvpjf backup.tar.bz2 -C /  
sudo nano /mnt/disk/boot/grub/menu.lst</nowiki></pre> Change the UUIDs to match the results of your <code><nowiki>blkid</nowiki></code> command.</li></ol>
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 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.
Mount the reformatted partitions on a LiveCD.
Open a terminal and type


blkid
=== Restoring GRUB ===
Making note of the UUIDs, edit the /etc/fstab in the restored root partition.
* 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<pre><nowiki>
find /boot/grub/stage1</nowiki></pre><pre><nowiki>
root (hd?,?)</nowiki></pre><pre><nowiki>
setup (hd?)</nowiki></pre> substitute the <code><nowiki>?</nowiki></code> with the results of your <code><nowiki>find</nowiki></code> command.
press esc to restart the system.
=== 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]]


sudo nano /mnt/disk/etc/fstab
[[category:UbuntuHelp]]
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
"Backing Up Ubuntu"
"Wei’s world: HOWTO: backup my Ubuntu"
取自"http://wiki.ubuntu.org.cn/UbuntuHelp:BackupYourSystem/TAR"
4个分类: 翻译请求 | CategoryBackupRecovery | CategoryCommandLine | UbuntuHelp
本页面已经被浏览1,664次。
此页由Ubuntu中文用户Wikibot于2008年10月19日 (星期日) 22:03的最后更改。 在Oneleaf的工作基础上。
关于Ubuntu中文
免责声明

2009年6月25日 (四) 09:31的最新版本

{{#ifexist: :BackupYourSystem/TAR/zh/zh | | {{#ifexist: BackupYourSystem/TAR/zh/zh | | {{#ifeq: {{#titleparts:BackupYourSystem/TAR/zh|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:BackupYourSystem/TAR/zh|1|-1|}} | zh | | }}

  1. title

译注:文章已经经过改动。

用TAR来备份系统

IconsPage?action=AttachFile&do=get&target=note.png 这篇文章已经被改动过了,以便更好地刊登到wiki上。完整的版本可以看这里the original forum post.
IconsPage?action=AttachFile&do=get&target=stop.png 如果你没有正确使用程序来备份,那你的资料可能就会被你抹去了。所以请在读完后再动手

介绍

这篇文章是HOWTO系列中 备份系统 的一部分.

IconsPage?action=AttachFile&do=get&target=info.png 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 .

为备份系统做准备

IconsPage?action=AttachFile&do=get&target=IconHint2.png 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
IconsPage?action=AttachFile&do=get&target=IconNote.png 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

IconsPage?action=AttachFile&do=get&target=terminal.png

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

IconsPage?action=AttachFile&do=get&target=IconHint2.png 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 /

  • IconsPage?action=AttachFile&do=get&target=info.png 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.<
    >
IconsPage?action=AttachFile&do=get&target=example.png --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. <
    >
IconsPage?action=AttachFile&do=get&target=info.png 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.

IconsPage?action=AttachFile&do=get&target=example.png

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.
  • IconsPage?action=AttachFile&do=get&target=stop.png 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

IconsPage?action=AttachFile&do=get&target=IconHint2.png 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 )"

  • IconsPage?action=AttachFile&do=get&target=info.png 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

IconsPage?action=AttachFile&do=get&target=warning.png 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 /

  • IconsPage?action=AttachFile&do=get&target=info.png 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.
IconsPage?action=AttachFile&do=get&target=warning.png 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.
IconsPage?action=AttachFile&do=get&target=IconNote.png 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, change j to z 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.
  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 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 your blkid 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.

Additional resources