Linux Server backup

Giteqa

In order to keep your data safe in case of a failure or hacker attacks, they must be archived from time to time. The data that you have archived must be stored on a separate hard drive so that in case of a threat they are safe and inaccessible to viruses or hackers.
How to set up a backup in linux? How do I schedule a backup? How to restore the Linux system from the created image? The answers to all these questions can be found in this article.

The examples shown in this article are implemented on Ubuntu 20.04

Backup using console commands

The first way

It is not necessary to use third-party utilities to create a backup, just a few console commands.
An example of such commands is the following command:

tar czf /backup.tar.gz --exclude=/backup.tar.gz --exclude=/home --exclude=/media --exclude=/dev --exclude=/mnt --exclude=/proc --exclude=/sys --exclude=/tmp /

This command is executed on behalf of the Root user or a user with Sudo privileges. It takes a lot of time to create an archive because it copies absolutely the entire system. I will not paint each line, I will indicate only a few nuances

  • C - Creates an archive
  • Z - Enables compression
  • F - Indicates where to save the result
  • –exclude - Excludes certain folders and the archive itself

After the archive has been created, it is best to place it on a separate hard drive or on a USB flash drive in order to connect it in the future, if necessary, and restore the system and everything that was on it.

You can find the archive in the graphical shell and transfer it from there.

RDEBACKUP

RDEBACKUP2

To restore the system, then it will be enough to connect a USB flash drive with the archive and enter the command:

tar xf /run/media/usb-name/backup.tar.gz -C/mnt

After that, your system will be restored along with all the programs that were installed on it.

The second way

The second way is to create a partition image using this command, you can copy partitions/files or even an entire disk.

dd if=/folder/file of=~/backup.img


This command is very useful and easy to use. It can take a lot of time to create an archive, it all depends on the speed of your disk, as well as on the amount of memory that is being archived.

To restore a file/archive and other things, just enter the same command, but swap the options.

dd if=~/backup.img of=/folder/file

With these two commands, you can create a copy or restore certain files/folders, or even an entire system.