Backing up your data by turning on your computer

December 20, 2021

Lang: cs en de es

Need to back up efficiently? Do you use Linux? Then you're halfway there, because in this article I'll describe how to easily set up a very efficient backup method.

Every expert will agree that backup is necessary. However, practice often does not honor this and backup is one of the last things to be addressed. If you are going to make backups work, the only sensible use is to have automatic backups. Because if you have automatic backup, the user is not burdened with having to think about running the backup process.

Equally important is recovery. Just because you're backing up doesn't mean you're actually backing up. Nor does it mean that you're able to back up from the backup. It also depends on how easy it is to restore the data and if there is a requirement that the data owner can restore the data themselves.

Effective Simple Backup in Linux

If you use a professional operating system like Linux for your work, then you're halfway there, because in Linux has a lot of backup tools. I've already described the general principles of how to back up data. In this article, I will describe how to use the modern BTRFS file system for backup.

Requirements

Linux is an essential requirement. You need a kernel that is not old to include support for the BTRFS filesystem. Z technically, nothing else. You need to install your Linux distribution on a PC so that the filesystem is used for what you want to back up, ideally both the data partition, the user partition and the whole system. It is even ideal to have only one partition, because the file system BTRFS, can subvolume, which gives you the equivalent of partitions.

If you have Linux already installed, you will need to migrate to the BTRF file system, but you need to be knowledgeable to do this especially if it will also involve the partition you want to boot from and the operating system is on it.

Backups=snapshots

The BTRFS filesystem has a snapshot feature that we use for backups. It's ideal to make changes from a booted liveCD because then you won't be making them on the live system, but it's not necessary.

Create a new subvolume, which will then be your new file root. Any further changes will need to be made in files of the newly created snapshot, as it will be the one you will be using. So the "/" root will now remain as your backup.
> Use the following command to create a snapshot from the root of the filesystem:

btrfs subvolume snapshot / /ROOT

Edit etc/fstab. You need to add the subvolume parameter. For example, the resulting entry might look like this:

/dev/mapper/crypt_nvme0n1p5 / btrfs noatime,subvol=ROOT 0 0
With this modification, the system knows that the root partition will now be subvolume /ROOT

It's a good idea to include the location where the root partition will be mounted, because that's where you'll later need to work with snapshots: create them, browse them, and delete them.

/dev/mapper/crypt_nvme0n1p5 /mnt/systembtrfs btrfs noatime 0 0

You need to modify the bootloader configuration. If you have installed on the BTRFS filesystem, you already know that you able to boot from BTRFS.
I'm going to assume you are using the Grub boot loader in version 2. (More about the Grub boot loader and booting your computer.) You need to add a parameter to the /etc/grub2.cfg configuration file:

rootflags=subvol=ROOT
The result might look like this:
...
linux /ROOT/boot/vmlinuz-5.15.6-desktop-2.mga8 root=UUID=d7680e66-1a8a-47c9-ba53-147b0fcf9186 ro rootflags=subvol=ROOT "rd.luks.name=8b3a72a1-3926-4982-bc8f-30ca315c9dd1=crypt_nvme0n1p5 " " splash quiet noiswmd resume=/dev/nvme0n1p6 audit=0 vga=791"
initrd /ROOT/boot/initrd-5.15.6-desktop-2.mga8.img

It is also possible to skip the Grub configuration editing and specify the parameter at boot time when Grub is used to choose what to boot and how to boot. And then let the new configuration be generated automatically from the booted system. Generating the grub configuration is done with the command:

grub2-mkconfig > /boot/grub2/grub.cfg

Backup

Backups are easy to set up. For example, to /etc/rc.d/rc.local You add a script call with the contents:

#!/bin/bash
DATE=`date +%Y-%m-%d_%H:%M`
cd /mnt/systembtrfs
btrfs subvolume snapshot ROOT ROOT-$DATE
This modification ensures that every time the computer boots, a ROOT snapsahot is performed on the subvolume with the current date. These snapshots will then serve as backups that you can browse in the /mnt/systembtrfs directory.

If you modify it appropriately, you can use a similar procedure for backing up a Linux server.

Delete Backups

So, strictly speaking, you delete unnecessary backups with the snapshot command:

btrfs subvolume delete /mnt/systembtrfs/ROOT-date

Renewal

Renewal is absolutely trivial. You need to replace the /ROOT snapshot with another snapshot. So you can rename the original snapshot to /ROOT.old and rename the backup to /ROOT or make a snapshot from the backup called /ROOT and the next time you boot, the system will boot from the selected backup, which is newly available in the predefined path /ROOT.

You can browse backups as a normal directory structure.
If you only need to recover one file that you have deleted. Just in the /mnt/systembtrfs/ directory by date to find the version of the backup you want to copy from.

What to look out for when backing up

General rules for backups.

Check your backups from time to time and try to restore your backups as well. It may happen that somehow your intervention may cause the backups to stop working or or the backup data is not in order.

Again, I have to remind you that RAID is not a backup, but a way to make the service you are running more available on server.

Remember that it is also a good idea to have the process set up so that backups are on a different disk, a different machine, and ideally in different geolocation. And in this day and age of ransomware, it's a good idea to have your backup process and infrastructure set up so that on the performed backups will not be affected if ransomware attacks your computers - backed-up data needs to be safe from ransomware.

Články na podobné téma

Running Microsoft SQL Server on Linux
Backup: the Proxmox Backup Server
Linux as a router and firewall
How to upload a docker image to the Docker Registry
Linux: logical volume management
Linux Software RAID
Running a web application behind a proxy
Mailbox migration
Docker multistage build
Podman
Importing Windows into Proxmox virtualization
Docker and PHP mail
Proxmox virtualization
Docker and Cron
Lenovo ThinkPad X1 Carbon: LTE modem EM7544 commissioning
Yocto Project: Build custom operating system for embedded devices
Preparing a Linux server to run a web application in Python
How to address poor file share performance in Docker
How to get started using Docker correctly
Installing Linux on a dedicated HPE ProLiant DL320e server
How to stress test a web application
Why use the JFS filesystem
How to boot from a 4TB drive with GTP using UEFI
Btrfs file system
Raspberry PI
WINE - running Windous programs under Linux
GNU/Linux operating system

Newsletter

If you are interested in receiving occasional news by email.
You can register by filling in your email news subscription.


+