The fastest and simpliest way to backup Linux server.

If you reading this, you probably already know, that backups can save your ass when things not going as planned.
Commands on this page has been tested on Debian 12 servers, however they will work on most Linux distributions, especially Debian-based ones.

Option 1: the Universal one

Backup command:

tar -cpzf /tmp/full-backup.tar.gz --exclude=/proc --exclude=/tmp --exclude=/mnt --exclude=/dev --exclude=/sys --exclude=/run /

Explaination: This command packs everything on server filesystem in one tar archive, using gzip compression. Some folders are excluded – they only contain OS-related things that shouldn’t be included in backup.

Restore command:

tar -xpzf /tmp/full-backup.tar.gz -C / --exclude=boot --exclude=etc/fstab --exclude=etc/default/grub --exclude=etc/grub.d --exclude=etc/hostname --exclude=etc/netplan --exclude=etc/network/interfaces --exclude=etc/machine-id --numeric-owner

Explaination: This command unpacks earlier created archive. Instance-specific configurations are excluded – if you restore on new server with same OS, then you usually shouldn’t restore grub, fstab, and network details or it will broke everything if not handled accordingly.

Option 2: the Efficient one

This method follows priciples mentioned earlier and adds some improvements – is uses all the available CPUs and more efficient compression. This way backup will be created faster and take less space.

zstd is not a pre-installed package and should be installed manually:

apt install zstd

Backup command:

tar -I 'zstd -T0' -cpf /tmp/full-backup.tar.zst --exclude=/proc --exclude=/tmp --exclude=/mnt --exclude=/dev --exclude=/sys --exclude=/run /

Explaination: As previous one, but better. -T 0 means use all CPU cores. Compression level can be adjusted if needed.

Restore command:

tar -I 'zstd -T0' -xpf /tmp/full-backup.tar.zst -C / --exclude=boot --exclude=etc/fstab --exclude=etc/default/grub --exclude=etc/grub.d --exclude=etc/hostname --exclude=etc/netplan --exclude=etc/network/interfaces --exclude=etc/machine-id --numeric-owner

Explaination: The process is same as with gzip. Also uses all the available CPU cores.

Final words

Described approach is not advised as reliable backup straategy. Some applications (like databases) are sensitive for crash-consistent backups, showed here.

For production usage think about application-aware methods – use native backup tools for each application or think about commercial solution like Commvault or Veeam.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *