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:
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.
The following is an exemplary command of how to archive your system.
tar -cvpzf backup.tar.gz --exclude=/backup.tar.gz --one-file-system /
To understand what is going on, we will dissect each part of the command.
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.
The "problem" with the --one-file-system option would be that you would then have to include /boot /home or other partitions.
Below is a tar example with additional suggestions for excludes to make the resulting archive smaller. Please review and understand the excludes before you use this example and modify as needed.
cd / # THIS CD IS IMPORTANT THE FOLLOWING LONG COMMAND IS RUN FROM /
tar -cvpzf backup.tar.gz \
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.
To Split After Creation
split -d -b 3900m /path/to/backup.tar.gz /name/of/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 /ย
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.
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.
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 1024 > backup.tar.gz
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.
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:
You will want to restore from a Live CD. If needed, first partition and format the drive. You can do this with gparted. Then simply mount the partition you are going to restore somewhere. If you open the drive in nautilus, it will be auto mounted somewhere under /media. Take a look to find out where with:
Restore Your Backup
sudo tar -xvpzf /path/to/backup.tar.gz -C /media/whatever --numeric-owner
A brief explanation:
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, you may need to recreate directories that were not included in the original archive because you excluded them with --exclude. This does not apply to filesystems excluded with --one-file-system. This can be done with the following command:
mkdir /proc /sys /mnt /media
Once finished, reboot and everything should be restored to the state of your system when you made the backup.
For the system to boot, you will need to restore grub. To do this, you will need to reconfigure it in a chroot:
for f in dev dev/pts proc ; do mount --bind /$f /media/whatever/$f ; done
You will get a menu asking you what drive(s) grub should be installed on. Choose whatever drive(s) the computer will be booting from.
For more information on repairing grub, see GrubHowto
Restoring Over a Network
This short guide, assumes you employed nc to make the original backup as described above.
Ensure the disk has been mounted and use the following command to accept input over the network that will then be extracted to the path indicated. In this example, the directory /mnt/disk will be extracted to.
nc -l 1024 | sudo tar -xvpzf - -C /media/whatever
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: