Create system snapshots with CYA

System Saver

© Lead Image © ssilver, 123RF.com

© Lead Image © ssilver, 123RF.com

Author(s):

The CYA script helps you back up system files, which you can then restore quickly in case of an emergency.

It is always surprising how many users skip regular backups. This negligence can hit you hard when least expected. Backup software generally backs up directories, partitions, or an entire installation.

Some backup tools are used to back up and restore the operating system itself, not the user data. CYA, for instance, is an 80KB Bash script based on rsync that lets the user keep a copy of the system close at hand. CYA stands for "Cover Your Ass(ets)" – I think we all get the gist. CYA does not try to overload you with a graphical interface; instead, it lets you start the backup with a single command – or even automatically.

Versatile

According to the developer, the CYA script, which was developed by Cyberweb Solutions, can create snapshots of any Linux or BSD distribution – and theoretically, even of any system that offers Bash and rsync. CYA understands how to handle most Linux filesystems, including ZFS and Btrfs. The software is licensed under a BSD license, so anyone can view and modify the code for their own use. You can run CYA automatically with systemd or cron or integrate it into scripts.

The way CYA works is similar to restore points in Microsoft's System Restore, which also only backs up the operating system itself without user data. You can reset the system to an earlier status using a restore point. CYA offers the same features. The home directory remains untouched – at least so far. In the latest version, the software saves the user's data separately with the cya mydata function.

By default, the program automatically or manually creates three snapshots; then, it overwrites the oldest one. You can also tell CYA to create snapshots that it does not overwrite or that do not even appear in the list of backups.

The software backs up without compression or a proprietary format by default, which means that you can easily restore not only the entire installation, but also individual files or directories if required. You do not actually need the cya restore function; in principle, a file manager or a tool for the terminal is sufficient.

What Installation?

Because the software is a script, you don't need to install CYA in the classic way. Just download it as a ZIP archive [1], which you then unpack, then copy the cya file to the bin directory under your home directory or the usr/local/bin directory. If you want to put the cya script somewhere else, add the path to the $PATH variable. Be sure to set the file permissions to make the file executable.

The script is now ready for use. The only other requirement is that the system is configured to use sudo [2]. Once you have done this, make sure you are in the home directory and execute the cya command (Figure 1). If nothing happens, log out and back on again – but this should not be necessary for most distributions.

Figure 1: During the first run, CYA creates the new user named cya and its infrastructure.

As you can see, the program creates a new user named cya, as well as some directories and files. Snapshots will end up in points/ in the future. The cya.conf file is still empty – you can store your own configuration options here. The LAST_RUN file tells you when the software last ran.

The cya help call reveals all the options and parameters you can use to control CYA (see Table 1). The default settings are fine for many use cases; however, you might want to familiarize yourself with the available options to have the right commands at hand when in doubt.

Table 1

CYA Commands

Command

Results

Create Snapshots

save

Create snapshot (rotating)

keep

Create manual snapshot (no rotation)

mydata

Back up your home directory

archive

Manually archive snapshot

Recovery

restore

Restore data

Other

configfile

Help for manual configuration

directories

List directories to be backed up

list

List snapshots you have created

remove

Remove manual snapshots

script

Linux mount script

uses

Use cases for script integration

version

Update check

Configuration

Type cya configfile to configure. Examples of modifying the configuration are shown here. If you need a rotation setup with more than three slots, you can change MAX_SAVES="" and write this statement and all the others to the empty cya.conf file in /home/cya/. You can then add or exclude files and directories until the result is what you want. The sequence is irrelevant.

Once you have completed the configuration, start the first backup. Simply enter cya save in the terminal (Figure 2). The script then saves the system except for /home/. CYA stores the snapshot in the home directory of the cya user created by the system under the points/ directory.

Figure 2: During the first snapshot, the software implements the configuration you have created. The backed up data is stored in the points/ directory.

If the home directory is on a separate partition and you use the automatic method, you must make sure there is enough free space for at least four backups. This number results from the fact that the program keeps three snapshots each for the automatic method and then rotates.

Enumerated

If you call cya list, you will see the first snapshot with its date and time under point 1. Other snapshots that you created automatically or by running cya save are numbered by the tool and rotated as needed (Figure 3).

Figure 3: The list option displays two standard snapshots and two manually saved snapshots.

The cya keep command lets you create manual snapshots that are not subject to rotation, which is a good way to take a snapshot immediately after installing the operating system under a name such as basisinstallation. Even if you are importing a large update or an extensive package, creating a snapshot up front gives you additional assurance.

If something goes wrong, you can roll back the system to a previous state. Although you will want to keep basisinstallation and even archive it with cya archive, you can dispose of snapshots created before updates with the cya remove command as soon as you no longer need them.

Manual and Scripted

After entering the cya keep command, the program prompts for a unique snapshot name, which must not contain spaces. Alternatively, enter a name with the command; if run in scripts, use:

cya keep name pre_upgrade

The rest of the process is the same as the automatic backups, except that the snapshot does not reside in a numbered directory. Instead, it ends up in a folder with the assigned name. So, if you stick to the above backup as an example right after installing the operating system, then the data could be found in /home/cya/points/basicinstallation.

If you want to use the software in a script that automatically creates a snapshot every month and overwrites the previous one, the command is:

cya keep name monthly overwrite

Here, the tool takes an incremental approach and only considers changes since the last snapshot.

If you do not want to overwrite the backups, use

cya keep name monthly-DATE

instead; this tells the script to append the respective date to the name.

Recovery

Any backup or snapshot system is only as good as its data recovery function. It is therefore worth testing the restore before an emergency occurs. First, use the cya script command to create a recovery script.

Specify which directories the system must mount when restoring. You can make your own entries here if you want to include additional directories (Figure 4). You will want to create the script immediately after configuration and save it separately.

Figure 4: The recovery script includes commands to mount the necessary partitions to ensure easy recovery. You can adapt it to your individual needs.

If data to be restored resides outside the default directories, you can manually enter the under /home/cya/. For example, if /var/ is on an external hard disk, you need to add the mountpoint here. Then, copy the finished recovery.sh script to a USB stick, which you later mount in a Live system and use to restore the backups.

As already mentioned, you should test the process before an emergency occurs so that you are familiar with the procedure. If the hardware, partition layout, or CYA version changes, you will want to run cya script again to customize the script.

The recovery process requires a Live system. You start this from a USB stick, an optical medium such as a CD, or a Netboot image. If possible, use a version of the operating system that matches the installation to be restored.

In a field test, I deleted the boot/ directory on a machine with:

sudo rm -rf /boot

This meant the computer was no longer bootable. CYA had to restore this directory to have a bootable system after the restore.

Recovery from a Live System

From a running Live session, first mount the USB stick with the recovery script, if this does not happen automatically, and switch to the console. Alternatively, you could mount the partition manually and specify the absolute path to the script in home/cya/. Then, start the script with the ./recovery.sh command and follow the instructions (Figure 5).

Figure 5: Starting the recovery script in the Live environment initiates the recovery. If you start it within the normal installation, it will refuse to work.

First, select which snapshot you want to restore; then, you can take a new snapshot of the installed system. In the test case here, this didn't make sense because the system was no longer bootable, but it can help in the later diagnosis of a broken system.

Next, decide whether the script removes files that are not in the backup from the installed system. The answer to the last question determines whether the recovery runs as a single pass or whether you want to confirm each directory of the root tree individually. In my example, the latter made sense because I knew that the system only lacked the boot directory. If you don't know what's broken, no would be the right answer.

Now, a final warning appears notifying you that the software will overwrite all data from this point onward. CYA shows the selected options and waits for you to enter the word restore to start the process (Figure 6). The script then processes the selected options (Figure 7).

Figure 6: CYA warns about the point from the process cannot be reversed. It then starts the recovery of the lost files.
Figure 7: First you determine which backup you want to import. Then specify whether you also want to create a current backup of the installed system.

Within minutes to half an hour, depending on the options selected and the hardware used, the process is complete and the software prompts you to restart the installed system (Figure 8).

Figure 8: The cya list command shows the new backup created during the restore after restarting the restored system.

Conclusions

There are many ways to create restore points for a system restore. CYA may seem complex at first glance, but that has a positive long-term effect. The script offers many possibilities for customization and is suitable for desktop use, as well as for use via SSH on a server (Figure 9).

Figure 9: CYA supports many scenarios, some of which are listed in the cya uses command.

As with any backup program, you should run through the process, including the restore, at least once. The project has documented the entire process in an exemplary manner on the website and in around 20 YouTube videos on the individual topics [3].

Once you have configured CYA and provided it with a cronjob or systemd timer, you do not need to do anything until an emergency occurs. If necessary, you can take manual snapshots in just a few seconds. CYA did not cause any problems in the test and will replace Clonezilla on my system, which I previously used for this purpose.