特殊:Badtitle/NS100:BackupYourSystem/TAR:修订间差异
小无编辑摘要 |
小无编辑摘要 |
||
(未显示2个用户的8个中间版本) | |||
第2行: | 第2行: | ||
{{Languages|UbuntuHelp:BackupYourSystem/TAR}} | {{Languages|UbuntuHelp:BackupYourSystem/TAR}} | ||
#title | #title | ||
== | == Introduction to tar == | ||
This page is part of the [[UbuntuHelp:BackupYourSystem|BackupYourSystem]] article, as such, ensure you've read that prior to continuing. This subpage will acquaint a user with the tar archival program, a CLI solution to the creation of compressed archival backups. It will detail the creation and restoration of archives, including operation over a network. | |||
Before continuing users are encouraged to read the TerminalHowto page which explains many basic concepts related to working with a terminal. | |||
https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=stop.png Improper usage of any archival program can cause unintended data loss. Read the entire tutorial before proceeding and understand what you are doing. | |||
== Preparing for Backup == | |||
In preparation for a complete backup of the system, it is a good idea to empty the trash and remove any unwanted files and programs from your current installation. This includes the home folder which can be filled with many files not needed. Doing so will reduce the size of the archive created in relation to how much space is liberated. | |||
A quick list of examples is below, decide for yourself what applies: | |||
== | * Delete all your emails. | ||
This | * Wipe your saved browser personal details and search history. | ||
* 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 drives and remove any optical media such as CDs or DVDs that you do not want to include in the backup. | |||
* This will reduce the amount of exclusions you need to type later in the process. | |||
=== | * Go through the contents of your user folder in <code><nowiki>/home</nowiki></code> and delete all unwanted files in the subdirectories, often people download files and forget about them for instance. | ||
== Backing Up == | |||
To get started, please open up a terminal, in Ubuntu this can be done by '''Applications Menu''' -> '''Accessories''' -> '''Terminal'''. Some directories require root or superuser permissions to read and write (needed for backup), for an explanation on why see [[UbuntuHelp:FilePermissions|FilePermissions]]. To gain temporary root permission, simply preface any command you want to issue with sudo, as explained in [[UbuntuHelp:RootSudo|RootSudo]]. | |||
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. | |||
<pre><nowiki> | |||
* | cd / </nowiki></pre> | ||
* | The following is an exemplary command of how to archive your system. | ||
* | <pre><nowiki> | ||
tar -cvpzf backup.tar.gz -–exclude=/backup.tar.gz --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev / </nowiki></pre> | |||
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 <filename>''' - 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 <code><nowiki>/proc</nowiki></code>, <code><nowiki>/lost+found</nowiki></code>, <code><nowiki>/sys</nowiki></code>, <code><nowiki>/mnt</nowiki></code>, <code><nowiki>/media</nowiki></code> and <code><nowiki>/dev</nowiki></code> directories in root. <code><nowiki>/proc</nowiki></code> and <code><nowiki>/sys</nowiki></code> are virtual filesystems that provide windows into variables of the running kernel, so you do not want to try and backup or restore them. <code><nowiki>/dev</nowiki></code> 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 <code><nowiki>/media</nowiki></code> folder excludes all mounted drives and media there. | |||
* If there are certain partitions you wish to backup located in <code><nowiki>/media</nowiki></code>, simply remove the exclusion and write a new one excluding the partitions you don't want backed up stored within it. For example: | |||
* <pre><nowiki> | |||
--exclude=/media/unwanted_partition </nowiki></pre> | |||
* '''/''' - After all options is the directory to backup. Since we want to backup everything on the system we use <code><nowiki>/</nowiki></code> 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 <code><nowiki>/</nowiki></code> 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. | |||
=== Additional Tips === | |||
* 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 <code><nowiki>z</nowiki></code> in the command with <code><nowiki>j</nowiki></code>, 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 <code><nowiki>--one-file-system</nowiki></code> 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 <code><nowiki>/home</nowiki></code> to keep user folders separate from root, adding this option to our original example would exclude home's contents entirely. | |||
=== Archive Splitting === | |||
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 <code><nowiki>split</nowiki></code> command. Below are examples of both scenarios. More information than conveyed here can be found in the man pages of split, use <code><nowiki>man split</nowiki></code> 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''' | |||
<pre><nowiki> | |||
tar -cvpz <put options here> / | split -d -b 3900m - /name/of/backup.tar.gz. </nowiki></pre> | |||
* 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''' | |||
<pre><nowiki> | |||
split -d -b 3900m /path/to/backup.tar.gz /name/of/backup.tar.gz. </nowiki></pre> | |||
* Here instead of using standard input, we are simply splitting an existing file designated by /path/to/backup.tar.gz . | |||
'''To Reconstitute the Archive'''<<BR>> | |||
Reconstructing the complete archive is easy, first <code><nowiki>cd</nowiki></code> into the directory holding the split archives. Then simply use <code><nowiki>cat</nowiki></code> to write all the archives into one and send over standard output to tar to extract to the specified directory. | |||
<pre><nowiki> | |||
cat *tar.gz* | tar -xvpzf - -C / </nowiki></pre> | |||
* 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. | |||
* For a more complete explanation of restoration, see [[UbuntuWiki:starcraft.man/Sandbox#Restoring|Restoring]]. | |||
=== Backup Over a Network === | |||
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. | |||
==== Netcat ==== | |||
The command <code><nowiki>nc</nowiki></code> 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''' <<BR>> | |||
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:<pre><nowiki> | |||
nc -l -p 1024 > backup.tar.gz </nowiki></pre> | |||
'''Sending Computer''' <<BR>> | |||
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 <receiving host> 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:<pre><nowiki> | |||
tar -cvpz <all those other options like above> / | nc -q 0 <receiving host> 1024 </nowiki></pre> | |||
If all goes well the backup will be piped through the network without touching the file system being read. | |||
==== SSH ==== | |||
You can also use SSH. For a complete explanation of its proper use see [[UbuntuHelp:SSH|SSH]]. The command below is an example of what is possible. <pre><nowiki> | |||
tar -cvpz <all those other options like above> / | ssh <backuphost> "( cat > ssh_backup.tar.gz )" | |||
</nowiki></pre> | </nowiki></pre> | ||
* | 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. | ||
* '''<backuphost>''' - Should be replaced with the name of the computer in question on the network. | |||
== Restoring == | |||
https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=warning.png '''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 <code><nowiki>/home/test</nowiki></code> 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. | |||
tar - | '''Restore Your Backup''' | ||
</nowiki></pre> | <pre><nowiki> | ||
tar -xvpzf /home/test/backup.tar.gz -C / </nowiki></pre> | |||
* ' | A brief explanation: | ||
* '''x''' - Tells tar to extract the file designated by the f option immediately after. In this case, the archive is <code><nowiki>/home/test/backup.tar.gz</nowiki></code> | |||
* '''-C <directory>''' - This option tells tar to change to a specific directory before extracting. In this example, we are restoring to the root (/) directory. | |||
https://help.ubuntu.com/community/IconsPage?action=AttachFile&do=get&target=warning.png 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 <code><nowiki>/proc</nowiki></code>, <code><nowiki>/lost+found</nowiki></code>, <code><nowiki>/sys</nowiki></code>, <code><nowiki>/mnt</nowiki></code> and <code><nowiki>/media</nowiki></code>. This can be done with the following command in our example: | |||
<pre><nowiki> | |||
* | mkdir /proc /lost+found /sys /mnt /media </nowiki></pre> | ||
Once finished, reboot and everything should be restored to the state of your system when you made the backup. | |||
=== Restoring Over a Network === | |||
This short guide, assumes you employed nc to make the original backup as described above. | |||
'''Receiving Computer''' <<BR>> | |||
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 <code><nowiki>/mnt/disk</nowiki></code> will be extracted to. | |||
<pre><nowiki> | |||
nc -l -p 1024 | tar -xvpjf - -C /mnt/disk </nowiki></pre> | |||
'''Sending Computer''' <<BR>> | |||
On the computer with the archive to send, use the following command: | |||
| | |||
<pre><nowiki> | <pre><nowiki> | ||
cat backup.tar.gz | nc -q 0 <receiving host> 1024 </nowiki></pre> | |||
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. | |||
== Reformatted Partitions == | |||
If you had to format partitions or are restoring to different discs, update the <code><nowiki>/etc/fstab</nowiki></code> and <code><nowiki>/boot/grub/menu.lst</nowiki></code> files after restoring the backup. | |||
</nowiki></pre> | |||
* The | |||
* | |||
== | |||
</nowiki></ | |||
<ol><li>Mount the reformatted partitions on a [[UbuntuHelp:LiveCD|LiveCD]]. | <ol><li>Mount the reformatted partitions on a [[UbuntuHelp:LiveCD|LiveCD]]. | ||
</li><li>Open a terminal and type <pre><nowiki> | </li><li>Open a terminal and type <pre><nowiki> | ||
blkid</nowiki></pre> | blkid</nowiki></pre> | ||
</li><li>Making note of the UUIDs, edit the | </li><li>Making note of the UUIDs listed, edit the <code><nowiki>/etc/fstab</nowiki></code> and <code><nowiki>/boot/grub/menu.lst</nowiki></code> files in the restored root partition to match. Use the following commands to edit the files respectively.</li></ol> | ||
<pre><nowiki> | |||
sudo nano /mnt/disk/etc/fstab </nowiki></pre> | |||
<pre><nowiki> | |||
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. | |||
=== Restoring RAID1 to EXT3 for import into VM === | |||
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 | |||
<pre><nowiki> | |||
cd /media/disk/etc/ | |||
mv mdadm.conf mdadm.conf.old | |||
mkinitrd /media/disk/boot/initrd-noraid-'uname -r'.img 'uname -r' | |||
cd /media/disk/grub | |||
</nowiki></pre> | |||
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. | |||
=== Restoring GRUB === | === Restoring GRUB === | ||
In most cases restoring GRUB should not be necessary since we are only restoring files. | |||
If you find yourself unable to boot GRUB properly, follow the instructions on [[UbuntuHelp:GrubHowto#Backup,_Repairing_and_Reinstalling_GRUB|GrubHowto]] | |||
== Additional Resources == | |||
* [http://hddsaver.com/content/26/ "Backing Up Ubuntu"] | * [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"] | * [http://weichen.wordpress.com/2007/01/01/howto-backup-my-ubuntu/ "Wei’s world: HOWTO: backup my Ubuntu"] |
2010年5月19日 (三) 21:40的最新版本
文章出处: |
{{#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
Introduction to tar
This page is part of the BackupYourSystem article, as such, ensure you've read that prior to continuing. This subpage will acquaint a user with the tar archival program, a CLI solution to the creation of compressed archival backups. It will detail the creation and restoration of archives, including operation over a network. Before continuing users are encouraged to read the TerminalHowto page which explains many basic concepts related to working with a terminal. Improper usage of any archival program can cause unintended data loss. Read the entire tutorial before proceeding and understand what you are doing.
Preparing for Backup
In preparation for a complete backup of the system, it is a good idea to empty the trash and remove any unwanted files and programs from your current installation. This includes the home folder which can be filled with many files not needed. Doing so will reduce the size of the archive created in relation to how much space is liberated. A quick list of examples is below, decide for yourself what applies:
- Delete all your emails.
- Wipe your saved browser personal details and search history.
- 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 drives and remove any optical media such as CDs or DVDs that you do not want to include in the backup.
- This will reduce the amount of exclusions you need to type later in the process.
- Go through the contents of your user folder in
/home
and delete all unwanted files in the subdirectories, often people download files and forget about them for instance.
Backing Up
To get started, please open up a terminal, in Ubuntu this can be done by Applications Menu -> Accessories -> Terminal. Some directories require root or superuser permissions to read and write (needed for backup), for an explanation on why see FilePermissions. To gain temporary root permission, simply preface any command you want to issue with sudo, as explained in RootSudo. 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.
cd /
The following is an exemplary command of how to archive your system.
tar -cvpzf backup.tar.gz -–exclude=/backup.tar.gz --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev /
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 <filename> - 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:
--exclude=/media/unwanted_partition
- / - 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.
Additional Tips
- 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 withj
, 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.
Archive Splitting
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
tar -cvpz <put options here> / | split -d -b 3900m - /name/of/backup.tar.gz.
- 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
split -d -b 3900m /path/to/backup.tar.gz /name/of/backup.tar.gz.
- 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.
cat *tar.gz* | tar -xvpzf - -C /
- 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.
- For a more complete explanation of restoration, see Restoring.
Backup Over a Network
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.
Netcat
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:
nc -l -p 1024 > backup.tar.gz
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 <receiving host> 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:
tar -cvpz <all those other options like above> / | 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.
SSH
You can also use SSH. For a complete explanation of its proper use see SSH. The command below is an example of what is possible.
tar -cvpz <all those other options like above> / | ssh <backuphost> "( cat > ssh_backup.tar.gz )"
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.
- <backuphost> - Should be replaced with the name of the computer in question on the network.
Restoring
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
tar -xvpzf /home/test/backup.tar.gz -C /
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 <directory> - This option tells tar to change to a specific directory before extracting. In this example, we are restoring to the root (/) directory.
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:
mkdir /proc /lost+found /sys /mnt /media
Once finished, reboot and everything should be restored to the state of your system when you made the backup.
Restoring Over a Network
This short guide, assumes you employed nc to make the original backup as described above.
Receiving Computer <
>
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.
nc -l -p 1024 | tar -xvpjf - -C /mnt/disk
Sending Computer <
>
On the computer with the archive to send, use the following command:
cat backup.tar.gz | nc -q 0 <receiving host> 1024
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.
Reformatted Partitions
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.
- Open a terminal and type
blkid
- 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.
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 RAID1 to EXT3 for import into VM
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
cd /media/disk/etc/ mv mdadm.conf mdadm.conf.old mkinitrd /media/disk/boot/initrd-noraid-'uname -r'.img 'uname -r' cd /media/disk/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.
Restoring GRUB
In most cases restoring GRUB should not be necessary since we are only restoring files. If you find yourself unable to boot GRUB properly, follow the instructions on GrubHowto