Whether you have a OEM edition of Windows installed on your computer and you don’t have an install disk, or you have finally gotten your Gentoo system up and running, it’s a good idea to make a complete backup of your operating system. Although you can generally simply copy the files that make up Linux over onto a different hard drive or partition, with Windows you need to make an image. Making an image also has benefits on Linux, not the least of which is it contains the entire operating system backup in a single, compressed file.

The most powerful, free, and open source tool I’ve been able to find to do this is fsarchiver. This tool makes an image of the selected partition, and is capable not only of restoring a backup but also migrating an operating system to a different partition or hard drive (although this requires tweaking certain configuration files such as grub and fstab, which is beyond the scope of this guide). The fsarchiver site lists the features of fsarchiver and contrasts them to partimage but the relevant features are its ability to save and restore Linux and Windows file-systems, its ability to restore the file-system to a differently sized partition (useful if your drive has failed) and its ability to compress the image.

Fsarchiver runs in Linux, so in order to back up a partition you will need to use an existing Linux installation, or use a Live-CD/USB. There are methods for live backups in Linux, but these are outside of the scope of this article. If you wish to backup your Linux operating system, use a Live-CD/USB or a separate Linux installation.

Preparations

In order to successfully complete the imaging process, you will need a few things before you begin. The first is a backup location. Any number of options are available for this, you could use a USB storage device, space on a file server, a secondary hard drive, or even a different partition on the same hard drive (although this leaves you vulnerable to hard drive failure). You will also need a few pieces of information. The first is the name of the partition you are imaging. This should start with /dev and end with either sda# or hda# where # is a number. The second is the mount point for the location you are saving the image to. If you are saving to USB storage, this is probably in /media, as well as if you are saving to an internal hard drive. If you are saving over the network, you will need to manually mount the share to a location of your choice. Finally, you need root privileges. All the following commands are to be run either when logged in as root or as sudo.

The first step to backing up with fsarchiver is installing the program. Open Synaptic Package Manager, search for fsarchiver and install it.

Examining the File-systems

If you don’t already know what the names for your partitions are, but have them labeled or know their size or file-system types, fsarchiver has a built in tool that displays the drives’s labels, device names, size and file-system types.

To get this information, run:

fsarchiver probe

The result is a table with all the relevant information, broken down by hard drive. Once you figure out what partition you are imaging based on its size or file-system, look in the left-most row. This row should be label “device” and should have things like sda1 and sda2. The device in the row that you have selected is the partition you are going to image. From now on, it will have /dev/ prepended to it. This means sda1 will look like /dev/sda1. It is this last version of the device name that you will use in all the following steps.

Creating the Image

With the program now installed, open a terminal. To create the simplest possible image, with no compression, enter the below command:

fsarchiver savefs /media/savelocation/image.fsa /dev/sdb3

where /media/savelocation is the mount point of the location you are saving to and /dev/sdb3 is the partition you are imaging.

Multiple Partitions

Multiple partitions can be saved in the same image, simply add the partition names to the end of the command.

Compression

Of course, if you have a large installation, making a copy with the default options isn’t very useful because the image will be massive. Fortunately, fsarchiver has built in four different compression algorithms, with a total of nine different levels of compression to suit your needs.

To compress the image, the -z flag is used, followed by a number between 1 and 9. The level of compression is lowest (resulting in a large file) at 1, and the highest level (resulting in a much smaller file) is 9. The memory and time consumed goes up progressively at each level. A discussion of the different compressions and their merits is available at fsarchiver.org. Rewriting the previous command so that it uses the highest level of compression would result in the following:

fsarchiver -z 9 savefs /media/savelocation/image.fsa /dev/sdb3

Multiple Cores

Optionally, you can tell fsarchiver to use more than one core of the CPU for compression with the -j flag, followed by the number of cores you wish to dedicate to the process. Note however, that the number of cores used is limited to the number present in the CPU. Additionally, by using more cores in the compression process you may find the system’s performance reduced substantially. Rewriting the previous command to use both of my CPU’s cores gives us the following command:

fsarchiver -z 9 -j 2 savefs /media/savelocation/image.fsa /dev/sdb3

Labeling the Image

By using the -L flag, followed by a comment in quotation marks, you can add a comment to the image. For example, issuing the command

fsarchiver savefs -L "First archive" /media/savelocation/image.fsa /dev/sdb3

creates an image with the label “First archive”. Now when you run fsarchiver archinfo /media/savelocation.fsa you will see a segment called “Label” which had the information “First archive”.

Using label allows you to keep track of what is contained in each backup.

Encrypting the Image

Fsarchiver provides encryption, which implements the blowfish algorithm (if you are truly concerned about the security of your image, then consider encrypting it with gpg to encrypt it instead, making sure to use the stronger AES). To activate encryption, add the flag `-c to the command, followed by your desired password. This password must be between 6 and 64 characters.

To use encryption, the rewritten command is:

fsarchiver savefs -c password /media/savelocation/image.fsa /dev/sdb3

Why Use Encryption

Although encrypting your backup may seem pointless, especially if you don’t have confidential data in the backup, it protects you against someone modifying or replacing your backup with a malicious version. Encryption is especially important when you backup to a network location; however I suggest that you employ encryption in all cases.

A Note on Flags

The flags mentioned above can all be combined in a single command, you are not limited to using just encryption, or just compression.

Extracting the Image

When you need to restore your operating system from you image, the process is simple. For the image written in the original command, or the images resulting from the compressions, use the following command:

fsarchiver restfs /media/savelocation/fsa id=0,dest=/dev/sdb3

This command will restore the partition originally imaged from /dev/sdb3 to its original partition.

Multiple Partitions in the Image

If you saved multiple partitions when you imaged, you can extract each partition by identifying it with id=# where # is a number that starts at 0 and indicates a partition saved into the image. For the first partition (which is the first partition saved to the image), the number is 0, the second is 1, the third is 2, and so on.

Thus, if you ran the ran the command, fsarchiver savefs /media/savelocation/fsa /dev/sdb3 /dev/sda1 /dev/sdf5, /dev/sdb3 would be identified with 0, /dev/sda1 with 1, and /dev/sdf5 with 2. In order to restore these partitions from the image, you would use following command:

fsarchiver restfs /media/savelocation/fsa id=0,dest=/dev/sdb3 id=1,dest=/dev/sda1 id=2,dest=/dev/sdf5

Restoring an Encrypted Image

Use the argument -c -, as with encryption. The command would be

fsarchiver restfs -c password /media/savelocation/image.fsa /dev/sdb3

where password is your password.

Getting Information From and Existing Archive

Assuming you ran your backup some time ago, you may not remember the information needed to extract everything from the archive. Fortunately, fsarchiver has the command archinfo to give you information from the archive. To use it, enter the following where existingarchive.fsa is the archive you need information on:

fsarchiver archinfo existingarchive.fsa

On my systems, I have used fsarchiver to backup Windows 7, although I’ve never restored Windows from an image, and I’ve also used it to backup my old copy of Fedora, which I successfully moved into a Virtual Machine.

All in all, I found that fsarchiver was more effective than using partimage (which was essentially the predecessor of fsarchiver). Fsarchiver conveniently compresses files to a smaller size that partimage, and its ability to run live-backups make it more useful for general backup purposes than partimage. Additionally, fsarchiver’s ability to resize partitions allows it to push out cloned copies of an operating system onto hard drives of varying size, making it a more general purpose tool.`