Linux Mint – Creating and restoring snapshots

Docker Compose Tutorial for Beginners

Although Mint comes with a standard handy backup tool, there are no built-in snapshot tools that use a graphical interface. This is actually fine, as there are many solutions available for snapshotting a system; one of which is discussed here.

So what are snapshots and backups? A backup is a collection of your important files, while a snapshot is a backup of your entire machine, from the top to bottom. Snapshots are often referred to as images. The idea is that you can save a backup of your entire machine, right down to your configuration files. When restoring a snapshot, your machine will look and act exactly as it did at the point the snapshot was taken, because it literally is a clone of the machine at that point in time.

There are various ways of performing snapshots in Linux, and the method detailed in this section isn’t even exclusive to Mint. One of the many benefits of Linux is that very little is hidden from you, to the point where you could essentially just copy your entire filesystem into a compressed archive and have that serve as your snapshot. In fact, this is exactly what we’re going to do with our first approach.

The command that we’re going to use is tar. The tar command is a very old command, which is still used by many today. It stands for tape archive and was once the primary method of saving files onto backup tapes. However, it is still used today to create archives of files as well as complete filesystems.

To get started, it’s best to place the commands that we’re going to use into a script, so we can easily reuse it later. To create the script, open any text editor. It really doesn’t matter which one you use. You can use gedit that comes with Mint, nano that you can use within the Linux shell, or even others such as geany or scite that you can install. Once you have a text editor open, let’s type in the first part of our script, which is shown as follows:



So, what did we do so far? If you’ve already read Chapter 4, An Introduction to the Terminal, then the first line should be immediately apparent to you. This is our hash bang, which is what we start all bash scripts with. We added a blank line simply for readability (the blank line in between the hashbang and the date line is not required), and then we have a variable.

Variables in Bash scripting are not something we’ve gone over yet, so what follows is a brief explanation. A variable is simply a name given to something you want to store and reference later. In the DATE=$(date +%Y%m%d) line, we’re creating a variable named DATE, and we’re setting it equal to the current date stored in our system clock. In the line, Y references the year, m references the month, and d references the day. So, if today was August 4, 2014, the variable would then store 20140804.

Now, let’s add some more text to our script, as shown in the following command:

sudo tar -cpzf /tmp/`hostname`_$DATE.tar.gz \

--exclude=/dev \
--exclude=/lost+found \
--exclude=/media \
--exclude=/mnt \
--exclude=/proc \
--exclude=/run \
--exclude=/sys \
--exclude=/tmp \
--exclude=*.gvfs \

Although the preceding section may look long, it’s really only one command. On each line, we use \ so that we can continue typing on the next line. This is only done for readability and is not required. On the first line of the new batch of text, we have the following statement:


We start the command off with sudo. This is because we’re making a full system backup. We need to make sure that we have access to everything. If we get a “permission-denied” response from something we’re trying to back up, then that item would be missing from the backup.

Next, we have the tar command. You can look up the tar man page ( man tar) for more information on how it works, but in this case, we’re using the -cpzf flags with the command. The -c flag clarifies that we would like to create a new archive. The -p flag references that we would like to preserve the permissions of each file included in the archive. This is essential when taking a snapshot. Next, we have the -z flag, which means we want to compress the archive in order to save space. Finally, we have the -f flag, which clarifies that we want to use an archive file. When we type all the flags together, we get -cpzf.

In the next part of the command, we see /tmp/`hostname`_$DATE.tar.gz. The short explanation is that this is the path and filename where we want to store the archive, but several parts of this portion of the command deserve more explanation. The `hostname` portion refers to the name of your machine. When you run the script, `hostname` will be replaced by whatever the name of your computer is. If your computer is named MyBox then `hostname` is automatically changed to MyBox when the file is written. It’s also important to note that the hostname is not enclosed in single quotes, but backticks—this is an important distinction. The backtick is located on the same key as the tilde symbol (~), which is typically the key directly underneath Escape.

Next, you see $DATE as part of the command. In Linux, when you create a variable, you don’t include the $ symbol, but you do so when you recall it. Earlier, we saved a variable named DATE, and now that we have this variable, we’re calling it by referencing it as $DATE. Thus, $DATE will be replaced by today’s date in the format we mentioned earlier. If you put the command together with this understanding, and we assume that your computer’s name is MyBox and the date is May 23, 2014, the command will be as follows:

tar -cpzf /tmp/MyBox_20140523.tar.gz

Thus, we’re saving an archive file in the /tmp folder with the name MyBox_20140523.tar.gz. As mentioned earlier, the forward slash at the end allows us to keep typing, so the rest of the lines are technically all in the same command. With each additional line, we have an exclude cause, typed as --exclude=, with a path typed directly after it. Excluding is important when creating a snapshot, because there are some folders, and it is generally not a good idea to back them up. You may even have other folders that you don’t want as part of your snapshot. For example, if you already have a backup of your music collection, there’s probably no point including it here. It would just make the archive file extremely large, so feel free to add additional excludes as you see fit.

Take a quick look at the files we’re excluding, to get a sense of the logic that this script uses. For example, take into consideration the fact that the /media folder is excluded. The reason for this is because if you have a flash drive inserted into your computer and it’s mounted under /media, then this flash drive would be copied into your archive. This is probably not what you want, especially if you’re planning on storing your snapshot on a flash drive. The /dev folder is omitted, because it only contains virtual device files; something that is not relevant to your backup as these would be regenerated anyway.

Next, we’ll want a more permanent place for our snapshot. The script is saving the snapshot in /tmp. This is good because /tmp is excluded (so essentially, the script won’t back up its own archive), but this is not a good place to leave it. In the final line of your script, move your resulting archive file somewhere else. For example, if you wanted the archive moved to /home/jdoe/Backups (assuming, of course, that the directory exists), then enter the mv command with sudo at the end of the script, shown as follows:

sudo mv /tmp/`hostname`_$DATE.tar.gz /home/jdoe/Backups

Finally, we need to make the script executable; otherwise, we won’t be able to run it. To do so, run the following statement:

chmod +x nameofscript

So, now that we have a snapshot, what do we do with it? First, it’s recommended that you copy it somewhere outside of your computer (such as a flash drive or external hard drive). However, the reason we created this script in the first place was to have something we could restore from in case of a disaster. So, how do we restore this snapshot?

There are several ways, but perhaps, the easiest way is to first reinstall Linux Mint on your computer’s hard drive. After you’ve installed the distribution, and with your archive stored on a flash drive that is inserted into your computer now, unzip the archive over the top of your new Mint installation with the following command:

tar xvpfz backup.tgz -C /

As you can see, we’re restoring the saved archive over the top of our entire filesystem, as we referenced / as the target. The options we used this time are xvpfz; x means that we’re extracting the archive instead of creating one. The v flag stands for verbose, and this means that we want to see what it’s doing. The p flag clarifies that we want to work with absolute names. The f flag, as mentioned earlier, suggests that we’re working with an archive. Finally, the z flag, in this case, means we want to uncompress the file.


While you can theoretically use this method to deploy a single image to many machines, this script would only work if you restored it on the same machine you took it from, because the universally unique identifier of your partition(s) are not the same from machine to machine. There is a way to work around this, but it is outside the scope of this tutorial, and you would be better off using a solution such as Clonezilla for mass deployments.

Comments are closed.