ObjectiveLearning how to use the partclone utility to backup a partition
- Root permissions
- Partition to backup must be unmounted
- # - requires given command to be executed with root privileges either directly as a root user or by use of
- $ - given command to be executed as a regular non-privileged user
IntroductionIf you ever used
Clonezillato 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
partclonefrom command line.
Block level backupA 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 levelgives us the advantage of not having to worry about preserving specific file permissions like
Supported filesystemsPartclone 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.ext2command. Furthermore, if some specific filesystem is not supported by partclone,
partclone.ddcan be used.
InstallationPartclone 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 partcloneTo install it on Fedora:
# dnf install partcloneOn CentOS and Rhel we should first enable the
EPELsoftware 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-releaseEnabling 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.rpmAfter the repository is enabled, we just need to use
yumto install the partclone package:
# yum install partcloneOn Archlinux, we can install partclone via pacman:
# pacman -S partclone
Cloning a partitionEnough 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
lvmpartitioned 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
/homepartition. It is formatted with an
ext4filesystem 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.pclLet's analize the command. First we run partclone with the extension related to the filesystem we are interested in, ext4. Then we provided the
-coption. This let us specify that the action we want to do is to clone (partclone can also be used to restore a backup). The
-soption let us specify the source we want to clone (in this case the logical volume snapshot home_snap_lv). Finally the
-ooption, short for
--output, tells partclone where we want to store the backup image. Notice that the
.pclextension 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
# partclone.ext4 -N -c -s /dev/fingolfin_vg/home_snap_lv -o /mnt/data/backup.pclWill produce the following output:
Compressing the backupAs 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
gzipto produce a smaller backup image on the fly:
# partclone.ext4 -c -s /dev/fingolfin_vg/home_snap_lv | gzip -c -9 > /mnt/data/backup.pclThis time we didn't use the
-ooption to specify the output file. Instead, we piped the standard output of the program to
gzipstandard input. We also specified the
-coption, 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
-9option, the maximum available. Default compression rate is
--fastcan be used to use the fastest compression, favoring speed against efficiency, or, vice versa,
--bestfor 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
.gzprefix to it: this is also completely optional, and done just for convention.
Restoring a backupPartclone 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_lvThis time we provided the
-roption, short for
--restore, and inverted the arguments we used before. The argument earlier used for the
-ooption, this time represent our source, therefore we used it as the argument for the
-soption. 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_lvThe
-cswitch, is again used to decompress to stdout, decompression specified as the action by using the
-doption, 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 /mnt/data/backup.pcl.gz | partclone.ext4 -r -o /dev/fingolfin_vg/home_snap_lv