Logo

dev-resources.site

for different kinds of informations.

Using Timeshift for System's Snapshots and Recovery on Debian 12 via Command Line

Published at
11/13/2024
Categories
debian
ubuntu
timeshift
backup
Author
dev-charodeyka
Categories
4 categories in total
debian
open
ubuntu
open
timeshift
open
backup
open
Author
14 person written this
dev-charodeyka
open
Using Timeshift for System's Snapshots and Recovery on Debian 12 via Command Line

Timeshift for Linux is an application that provides functionality similar to the System Restore feature in Windows and the Time Machine tool in Mac OS. Timeshift protects your system by taking incremental snapshots of the file system at regular intervals. These snapshots can be restored at a later date to undo all changes to the system.(Timeshift GitHub)

When exploring a new distro, itā€™s always helpful to have the option to undo changes, especially after installing or configuring something substantial that significantly alters your system. For example, installing NVIDIA drivers modifies multiple components of your system, and simply uninstalling the drivers doesnā€™t always revert everything to the exact state it was in before driver's installation.

Snapshots ā‰  Backups

Itā€™s important to note that, although snapshots are sometimes referred to as backupsā€”even Debian documentation lists Timeshift as a backup tool - BackupAndRecovery-Debian Wikiā€”I somewhat disagree with this classification.

The maintainers of Timeshift also make this distinction clear:

Timeshift is similar to applications like rsnapshot, BackInTime and TimeVault but with different goals. It is designed to protect only system files and settings. User files such as documents, pictures and music are excluded. This ensures that your files remain unchanged when you restore your system to an earlier date. (Source)

If you need a true backup tool, you should look elsewhere or manually back up periodically just some important data to cloud storage or physical media/storage device.

While many people use snapshots as backup toolsā€”and they work 90% of the timeā€”in the other 10%, they can fail. Holding onto snapshots for too long, especially as the system changes significantly, can lead to problems. This isn't an issue specific to Timeshift, but it can happen if, for example, you take a snapshot, then make major modifications to your storage setup (such as reconfiguring LVM, partitions, changing filesystems). In such cases, itā€™s best to delete old snapshots and create a fresh one after successful modifications.

I think that probability to encounter such issues with Timeshift is quite low, but in my experience with VMware snapshots, I ran into trouble for precisely this reason.

After taking a snapshot of a Virtual Machine (VM), a logical volume has been extended and additional RAID storage has been added to this VM.The snapshot afterward has not been deleted and then a write-intensive task has been launched, which eventually has triggered an I/O error on the added RAID storage. It was a bit of a "situationship" since the RAID was specifically set up to ensure data integrity. Fortunately, no data was lost, but the Virtual Machine got crazy, caught between the restrictive snapshot (designed to allow a full rollback in VMware) and the newly evolved storage configurationā€”especially with the repetitive write process on the RAID system.

For more details about Snaphshots vs Backups, you can read this thread on Reddit. And I proceed with timeshift installation, because I need exactly a snapshot tool, not a backup tool.

Timeshift GUI - a little drawback

Timeshift has a graphical user interface, which, for some, may be a strong advantage, while for others, it might be less appealing (I, for example, prefer terminal or command line interfaces).

The timeshift package depends on lib-gtk-3.0 (Debian -- details of package timeshift in Bookworm). lib-gtk is the GTK graphical user interface library, and this dependency makes timeshift a suboptimal solution for machines primarily used as servers. However, I use it on my personal PC, and although I canā€™t recall any other package in my setup that uses GTK, this toolā€™s reliability outweighs the drawback of having GTK installed. But if this is a concern for you, you may want to consider a different tool for snaphots. Hereā€™s the original response (a bit dated) from the creator of Timeshift:

Timeshift has dependencies on GTK3 libraries so you must have that installed even if you have not installed a desktop. During installation it will install all dependencies it requires.
Separating the codebase into 2 separate packages (with and without Gtk dependencies) involves a lot of work.

Iā€™m not sure if the current maintainers of Timeshift have plans to separate Timeshift into GUI-only and CLI/TUI-only versions, or if this might have already been done. I havenā€™t found any information on it.


Preparing "storage device" for Timeshift

My system is partitioned with LVM, and I have a volume group that holds all my systemā€™s logical volumes (called wonderland-vg). First, I want to check if thereā€™s any free space in this volume group. If not, Iā€™ll need to expand it.

Most tutorials and guides focus on how to configure Timeshift where to store snapshots via the GUI. However, my system currently doesnā€™t have a DE nor even a display server. Timeshiftā€™s approach to snapshot destinations is based on storage devices (timeshiftā€™s config file expecting UUID) rather than directories, which makes senseā€”storing snapshots on the external storage device is useful in case the system breaks. So first, Iā€™ll prepare a separate logical partitionā€”a logical volumeā€”for my future snapshots.

#these commands give me stats about my volume group
$ sudo vgdisplay
$ sudo vgs
Enter fullscreen mode Exit fullscreen mode

I have around 160 GB free in wonderland-vg volume group, so I do not need to expand it:

Image description

I proceed with creation of new logical volume and mounting it to /timeshift directory:

# create logical volume with name timeshift in volume group wonderland-vg
$ sudo lvcreate -L 20G -n timeshift wonderland-vg
#verify creation
$ sudo lvdisplay
# create a filesystem (all my system is ext4, choose any you use
$ sudo mkfs.ext4 /dev/wonderland-vg/timeshift
Enter fullscreen mode Exit fullscreen mode

Installing Timeshift

Installation of timeshift in Debian is quite straightforward: just run sudo apt install timeshift.

Image description

Oh my, thatā€™s quite a lot of dependencies!
My current Debian setup is very close to a server setup, so I have very few things installed. Surprisingly, it doesnā€™t seem to pull in the display server Xorg, which is actually pretty good, especially for people using Wayland. However, there are still quite of some X11 packages installed.

Configuring Timeshift

The configuration of timeshift with command line is done mostly via configuration file. In /etc/timeshift/timeshift.config, you can find the default config. If you run blkid, youā€™ll see the UUIDs of all your storage devices and can pick one where you are willing to place your snapshots.

When you first launch timeshift --create, it will prompt you to select a device from a list of identified devices. The device identifiers arenā€™t very clear; I identified my logical volume mostly by its sizeā€”it was named /dev/sda1/dm-4.

Image description

Once selected, your device UUID will be automatically written to the /etc/timeshift/timeshift.json config file. The next time you run sudo timeshift --create, you will not be asked to select a device. If you want to change it, youā€™ll have to edit the config file and replace the UUID of the storage.

NB! IF you run sudo timeshift --create for the first time after installation and it doesnā€™t prompt you to select the storage (but instead picks one on its own), interrupt the execution (Ctrl +C). Then go to modify/etc/timeshift/timeshift.json (it should be created after the first launch) - either manually provide the correct UUID of the desirable storage or remove automatically set UUIDs (backup_device_uuid and parent_device_uuid fields). Once youā€™ve done that, restart the command, and it should definitely prompt you to select the storage.

I think the GUI would give more comprehensive control over device selection.

If using Timeshift's GUI is not an option, you can schedule backups using cron jobs or changing false to true in configuration file for schedule_monthly, schedule_weekly, schedule_daily, schedule_hourly (NB! I did not test it!). I don't enable automatic snapshots because, as I mentioned, theyā€™re not true backups, and I donā€™t need constant snapshotting. Iā€™ll take snapshots manually when necessary (e.g., before major system updates or modifications).

If you're planning to set up automatic snapshots, you should also check the configuration file for the snapshot retention policy.

Image description

For instance, if you schedule snapshots to run daily, even if theyā€™re incremental, thereā€™s no point in keeping all of them indefinitely (because they occupy storage's space). You need to pay particular attention to the values of these fields in the configuration file: count_monthly, count_weekly, count_daily, count_hourly.

Keep in mind that manually deleting snapshots via command line is a bad idea. Snapshots are incremental: only the first snapshot contains a "full" snapshot, while subsequent snapshots are just deltas (differences) from that first snapshot. If you manually delete the first (or "parent") snapshot, the others become unusable. However, if you use the retention policies and Timeshift commands, like sudo timeshift --delete --snapshot '<name/timestamp from sudo timeshift --list>', your snapshots will be managed correctly.

Another configuration field to check is exclude. By default all user's home directory is excluded from snapshotting.

NB! If youā€™re experimenting with different Desktop Environments, browsers, terminal emulators, or shells, remember that anything related to the customization, display stuff typically has configuration files in your $HOME (/home/your_username) directoryā€”often in $HOME/.config. If your /home directory is excluded from snapshotting (field exclude in /etc/timeshift/timeshift.json), when you install for instance KDE -> sudo timeshift --restore -> install GNOME, all of KDEā€™s config files will remain in $HOME and $HOME/.config. If you do many experiments withs such things, it will inevitably lead to clutter of $HOME directory over time (remember, that ls is not sufficient to see all content, try at least ls -a).

Testing Timeshift

Iā€™ve already created a couple of backups, and since theyā€™re incremental, only the first one was large and took more time to create. Letā€™s see if this tool can handle reverting a kernel version update and restore the system from "incremental" snapshot (not the first, complete one).

To see how timeshift organizes its stuff/files/snapshots, you can mount the storage device which is used by timeshift with:

# create a directory - mounting point
$ sudo mkdir /timeshift
# mount there newly created logical partition
$ sudo mount /dev/wonderland-vg/timeshift /timeshift
# to see what is inside
$ ls -a /timeshift
Enter fullscreen mode Exit fullscreen mode

This is directory structure that was created automatically by timeshift for itself:

Image description

My system's kernel version before kernel upgrade from bookworm-backports repo:

Image description

My system's kernel version after kernel upgrade from bookworm-backports repo:

Image description

I have 2 snapshots and I will be using the latest one.

Image description

$ sudo timeshift --list
$ sudo timeshift --restore --snapshot <timestamp/name form the list>
Enter fullscreen mode Exit fullscreen mode

Timeshift will ask me whether I want to reinstall GRUB. Since the change to my system was a kernel upgrade, I confirm. Then, Iā€™m asked where GRUB should be installed.

After the system state and settings are retrieved from the snapshot and ready for rollback, the system reboots.

NB: If you use BTRFS, don't miss the opportunity to take advantage of its excellent restore capabilitiesā€”they are fully supported in Timeshift:

It is strongly recommended to use BTRFS snapshots on systems that are installed on BTRFS partition. BTRFS snapshots are perfect byte-for-byte copies of the system. Nothing is excluded. BTRFS snapshots can be created and restored in seconds, and have very low overhead in terms of disk space.(Timeshift GitHub)

My system's kernel version after restore:

Image description

UPD on testing:

Iā€™ve noticed that sometimes when I do a rollback using timeshift restore and then reboot, the restored systemā€™s wireless network interface (Wi-Fi) ends up down. NetworkManager canā€™t resolve itā€”itā€™s not just the Wi-Fi gets disconnected, but the entire wireless interface disappearing (ip a does not list it at all). However, doing a clean reboot after the restore fixes this issue without any problem.

Summarizing:

Timeshift's Pros:
šŸ¦„ Continuously maintained and widely-used snapshotting tool
šŸ¦„ Easy management via command line
šŸ¦„ Incremental snapshots
šŸ¦„ Clearly separates snapshots from backups
šŸ¦„ Has an active community
Timeshift's Cons:
šŸ« No-GUI (CLI-only) installation is not possible
šŸ« Brings many dependencies related to the Xorg display server, which may not be the best for server setups or systems with Wayland
šŸ« Limited guides on CLI-only usage, even though it's possible
šŸ« Storage destination is bound by UUID, which may be confusing
šŸ« Limited identifiers in storage selection when creating the first snapshot, making it harder to choose the correct storage device

debian Article's
30 articles in total
Favicon
Fixes for a critical rsync vulnerability (CVE-2024-12084) have been released for Stable/Bookworm, Testing and Unstable....
Favicon
Debian and KDE 6 - WSL - How to install KDE 6 via Debian - Windows 11 - X410 - Linux - 2024 https://www.youtube.com/watch?v=yrtgmwsptVc
Favicon
Comprehensive Guide: Setting Up Gestures on Linux (Debian-Based Distributions)
Favicon
č®©å®‰å“ę‰‹ęœŗäøå†åƒē°ļ¼šåœØå®‰å“ę‰‹ęœŗäøŠę­å»ŗ Rust 开发ēŽÆ境
Favicon
The Importance of Reading Documentation: A Lesson from Nvidia Drivers
Favicon
I changed the arm on my android with Debian no root Linux to the Debian arm my android does wonders now.
Favicon
"Why is it, when something happens, it is always you TWO?"- troubleshooting Bluetooth and Wi-Fi devices on Debian 12
Favicon
Virtualization on Debian with virsh&QEMU&KVM ā€” Installation of virtualization tools and first VM creation
Favicon
The Debian LTS Team is actively working towards ensuring that security fixes made in LTS are also propogated to more recent...
Favicon
OKMX6UL Development Board Debian Filesystem Creation Process (Including Tool Installation, Configuration, and Burning)
Favicon
From Debian to Devuan
Favicon
Debian Outreachy interns selected for the December 2024 round
Favicon
Abilitare SSH root login su Debian Linux Server
Favicon
Calls for bids for DebConf26 have started, please see: https://lists.debian.org/debconf-announce/2024/11/msg00001.html
Favicon
LINUX ƜZERINDE OPERA BROWSER VIDEO OYNATMAMA SORUNU
Favicon
Debian 12 ā€¦ is amazing! How to: Create your custom codehouse #6 [Giving Voice to Debian: Wireless Audio Devices configuration]
Favicon
Kicksecure: Hardening Your Linux Systemā€™s Security (Debian Morph)
Favicon
Managing Large Debian Repositories with Pulp
Favicon
Debian 12 ā€¦ is amazing! How to: Create your custom codehouse #5 [From Console only to Custom Graphical User Interface]
Favicon
Reviving the Remix Mini PC: A Guide to Running ARM-based OS Images
Favicon
Automating Debian Package Update Summaries with Python and Gemini (gemini-1.5-flash)
Favicon
DebConf26 bids: Please get your information in shape soon!
Favicon
Debian 12: NVIDIA Drivers Installation
Favicon
Debian Secure Boot: To be, or not to be, that is the question!
Favicon
Bulk Linux Users Creation
Favicon
Using Timeshift for System's Snapshots and Recovery on Debian 12 via Command Line
Favicon
Streamlining .deb Package Installation on Ubuntu: A Better Way to Manage Downloaded Packages
Favicon
Debian 12 ā€¦ is amazing! How to: Create your custom codehouse #4 [Security mechanisms against Network-Based attacks]
Favicon
Debian 12 ā€¦ is amazing! How to: Create your custom codehouse #3 [Security mechanisms against malware]
Favicon
Debian 12 ā€¦ is amazing! How to: Create your custom codehouse #2 [Installation & Manual Disk Partitioning with LVM]

Featured ones: