Intro to Snapshot / Cloning


Jarret W. Buse

Intro to Snapshot / Cloning

Some file systems allow for native snapshots and cloning. The two have one difference, but otherwise are the same.

The only difference between a snapshot and clone is that the snapshot is read-only, while the clone allows for reading and writing.

Snapshots or clones can be made of whole file systems to duplicate files on to different storage units. It should be noted that in some cases, the “data copy” is kept on the same partition as the original files. In this case, when a file is changed on a clone, the snapshot is technically now a separate file. At first, the two are a hard link to the same inode. When the writeable copy changes, a new inode is made for the updated file. For more information on inodes, see “Intro to Inodes”.

NOTE: Some file systems actually must place the clone or snapshot on a new partition, so extra drive space is required.

You may ask, “What the point if it takes up space?”.

If a backup of specific data is needed, a snapshot can be made. For example, if files on a hard disk are constantly being used and changed (usually more on a server), the files need to be backed up. A snapshot can be created which is a copy of files at a specific instant. These files then can be backed up to off-line storage without interrupting the file use by users.

Snapshots and clones can be used to test data. They can used to verify the data values in a test environment before the files are placed into a production environment.

Files can be updated in a clone to test the files. If the updates are found to be preferable to the original, the new files can overwrite the older ones. Replacing the file system with a snapshot or clone is sometimes called a clone promotion.

Another benefit is that if files are inadvertently updated or deleted, they can be retrieved from a snapshot or clone.

Once a snapshot or clone is no longer needed, it can be removed to free up any used disk space. Multiple snapshots and clones can be made, but they should eventually be removed.

NOTE: A snapshot can usually be changed to a clone to allow for file updating. Some file systems allow for a clone to be made of a snapshot.

Be aware that when a clone is made of a snapshot, you then have three entities:
  • Original data
  • Snapshot
  • Clone
NOTE: The clone must be destroyed first before the snapshot can be removed.

In the cases when a snapshot or clone is made to another disk or partition, the file system must be the same type.

The process of making a snapshot is as follows:

1. Make the snapshot
2. Mount the snapshot
3. Perform tasks on the snapshot
4. Unmount snapshot
5. Remove/promote snapshot

NOTE: Clones are done in the same manner.

On BTRFS, for example, a snapshot can be created by using the following command:
btrfs subvolume snapshot <volume-source> [<dest>/]<name>

NOTE: A ‘-r’ parameter can sometimes be used to make a read-only snapshot and not a clone.

To create a snapshot of a file, do the following:

cp --reflink <source>/<file> <dest>/<file>

NOTE: Not all file systems which support snapshots allow for files to be “snapshotted”.

Once a snapshot needs to be removed, perform the following command:

btrfs subvolume delete <name>

NOTE: Of course with BTRFS, a snapshot is the same as a clone since the snapshots are writeable.

To get a list of all snapshots, use the following command:

btrfs subvolume list /

On a BTRFS s drive, let’s assume we want to create a snapshot of a volume named ‘files’ called ‘snapshot1’:

btrfs subvolume snapshot -r files snapshot1

Once the snapshot is created, it can be restored as a whole over ‘files’ in the following manner:

btrfs subvolume del files

This line deletes the subvolume called ‘files’ so it is now ready to copy the snapshot, ‘snapshot1’, into the empty subvolume, ‘files’. The command to copy the files over is:

btrfs subvolume snapshot snapshot1 files

The final task is to delete the unneeded snapshot, ‘snapshot1’:

btrfs sub del snapshot1
$100 Digital Ocean Credit
Get a free VM to test out Linux! Hosting Donations
Consider making a donation

Staff online

Members online