Objective
Learning how to use the partclone utility to backup a partition
Requirements
- Root permissions
- Partition to backup must be unmounted
Difficulty
EASY
Conventions
- # – requires given linux commands to be executed with root privileges either
directly as a root user or by use ofsudo
command - $ – requires given linux commands to be executed as a regular non-privileged user
Introduction
If you ever used Clonezilla
to create a backup of your operating system, you have already experienced the power of the partclone
. Unlike other tools like dd
, partclone knows how to interact with specific filesystems, therefore it can create much smaller backups, cloning only the used space in the partition. In this tutorial we will learn how to use partclone
from command line.
Block level backup
A backup can happen at file level, or at a deeper block level
. The former is the level at which partclone operates. We all know and love dd
, but one of the characteristics that make this programs great in some situations, represents also a weakness in other ones: dd knows nothing about filesystems. The program just replicates each block of a disk or partition, creating a 1:1 perfect clone (a 160GiB disk, will produce a 160Gib backup). Partclone behavior is different, since it can smartly interact with the most common filesystem types, backing up only the used blocks of a partition, obtaining much smaller backups.
The fact that partclone operates at block level
gives us the advantage of not having to worry about preserving specific file permissions like acls
or selinux labels
.
Supported filesystems
Partclone supports all the most used filesystems like: ext2, ext3, ext4, hfs+, reiserfs, xfs, jfs, ntfs, fat(12/16/32), exfat, etc. The filesystem to backup will determine the suffix to use with the program, following the syntax partclone.<fileystem_type>
utility. For example, to backup an ext2 filesytem, we will use the parclone.ext2
command. Furthermore, if some specific filesystem is not supported by partclone, partclone.dd
can be used.
Installation
Partclone should be availble in the most common distributions repositories. To install it in Ubuntu or Debian, we can use apt:
# apt-get update && apt-get install partclone
To install it on Fedora:
# dnf install partclone
On CentOS and Rhel we should first enable the EPEL
software source (Extra Package for Enterprise Linux). CentOS has the epel-release package available in the official repositories, therefore to enable this software source, we just have to run:
# yum install epel-release
Enabling the EPEL repository on Rhel requires an extra step. First we have to download the epel-release package from the project page, then we should install the retrieved package:
$ wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm # yum install epel-release-latest-7.noarch.rpm
After the repository is enabled, we just need to use yum
to install the partclone package:
# yum install partclone
On Archlinux, we can install partclone via pacman:
# pacman -S partclone
Cloning a partition
Enough words, let’s see partclone in action. First and foremost, we must remember that to clone a partition, it must not be mounted. Therefore if you want to backup a system partition, you must access the drive from a live cd (this is what clonezilla does). If you use an lvm
partitioned system, we are not subject to this restriction, since we can create a live snapshot of a logical volume and run partclone on it, preserving uptime.
I’m going to backup a snapshot made from the logical volume I use as /home
partition. It is formatted with an ext4
filesystem and it’s called home_snap_lv
. All we have to run is:
# partclone.ext4 -c -s /dev/fingolfin_vg/home_snap_lv -o /mnt/data/backup.pcl
Let’s analize the command. First we run partclone with the extension related to the filesystem we are interested in, ext4. Then we provided the -c
option. This let us specify that the action we want to do is to clone (partclone can also be used to restore a backup). The -s
option let us specify the source we want to clone (in this case the logical volume snapshot home_snap_lv). Finally the -o
option, short for --output
, tells partclone where we want to store the backup image. Notice that the .pcl
extension here is completely arbitrary: we just use it for convenience.
Once launched, partclone will display the progress of the operation:
Partclone v0.3.11 http://partclone.org Starting to clone device (/dev/fingolfin_vg/home_snap_lv) to image (/mnt/data/backup.pcl) Reading Super Block Calculating bitmap... Please wait... Elapsed: 00:00:01, Remaining: 00:00:00, Completed: 100.00% Total Time: 00:00:01, 100.00% completed! done! File system: EXTFS Device size: 16.1 GB = 3932160 Blocks Space in use: 2.2 GB = 547088 Blocks Free Space: 13.9 GB = 3385072 Blocks Block size: 4096 Byte Elapsed: 00:00:02, Remaining: 00:00:44, Completed: 4.31%, 2.90GB/min, current block: 52992, total block: 3932160, Complete: 1.35%
However, it’s also possible to represent those information “graphically”, using ncurses (this is what clonezilla does). All we need to do, is to add the -N
option. Running:
# partclone.ext4 -N -c -s /dev/fingolfin_vg/home_snap_lv -o /mnt/data/backup.pcl
Will produce the following output:
Compressing the backup
As said before, partclone only backups the used block on a filesystem, optimizing the space needed to store the backup. However, thanks to the power of shell redirections, we can combine partclone with gzip
to produce a smaller backup image on the fly:
# partclone.ext4 -c -s /dev/fingolfin_vg/home_snap_lv | gzip -c -9 > /mnt/data/backup.pcl
This time we didn’t use the -o
option to specify the output file. Instead, we piped the standard output of the program to gzip
standard input. We also specified the -c
option, short for --stdout
, which let us modify gzip behavior in order to compress or decompress to stdout
. The compression level to be used is set with the -9
option, the maximum available. Default compression rate is -6
. Alternatively --fast
can be used to use the fastest compression, favoring speed against efficiency, or, vice versa, --best
for the opposite behavior, obtaining the smallest file.
Finally we used the >
operator, to redirect the output to our desired destination file. You can notice that I have added a .gz
prefix to it: this is also completely optional, and done just for convention.
Restoring a backup
Partclone can also be used to restore a previously made backup on an existing partition. The only limitation is that the partition must be of the same size or bigger than the original one.
Restoring a backup is very easy. Say for example we want to restore the previously made backup (again the partition must be unmounted):
# partclone.ext4 -r -s /mnt/data/backup.pcl -o /dev/fingolfin_vg/home_snap_lv
This time we provided the -r
option, short for --restore
, and inverted the arguments we used before. The argument earlier used for the -o
option, this time represent our source, therefore we used it as the argument for the -s
option. What before was the source of the backup, our partition, this time represents our destination, since we want to restore the backup.
In case we used gzip to compress partlcone output, the command to restore the backup changes a little. We have to decompress the package and restore it on the fly, again using the power the shell:
# gzip -c -d /mnt/data/backup.pcl.gz | partclone.ext4 -r -o /dev/fingolfin_vg/home_snap_lv
The -c
switch, is again used to decompress to stdout, decompression specified as the action by using the -d
option, passing the name of the compressed file as the argument. The output is then piped to partclone stdin and restored to the original partition. The same result of the above command can be obtained by using the zcat
program:
zcat /mnt/data/backup.pcl.gz | partclone.ext4 -r -o /dev/fingolfin_vg/home_snap_lv
Closing thoughts
Partclone is the core program used by clonezilla and a very nice tool to obtain a “smart” backup of a filesystem, considering only the used blocks. Combining it with lvm partitioning, one can obtain a backup of a partition without having to reboot the system. Please refer to its manpage for further information about this really useful program.