Computing: Computer Administration

Backing up a Linux (Ubuntu) VPS.

This tutorial describes how I backed up my VPS, actually running Ubuntu Server 20.04 with Hestia Control Panel. The procedure described should apply to other Linux systems, too. To note that Hestia CP includes a backup feature, but 1. my Hestia is actually down, and 2. the way to proceed here is a general and easy to realize approach, that everyone running an Internet server should know about.

There are essentially two things to be backed up:

  1. The /home directory, that contains all personal data, including most configuration files, and also the website files (Apache htdocs and cgi-bin). The home directory can easily be backed up into one or several files using the Linux tar utility.
  2. The MySQL/Mariadb (or other) databases. As is difficult to get a clean snapshot by just copying the files (creating an archive as with the home directory data) when the RDBMS is running, databases should be backed up by exporting them to SQL files (that not only contain the data, but also the SQL statements to create the database objects, and thus can be used to quickly recreate the database). This can easily be done using phpMyAdmin.
  3. It may also be useful to backup other directories, such as /etc and /var/lib (or the entire /var), not in order to restore as such, but for the case where you need some of the original files after a re-installation of the operating system (/etc, for example, contains the configuration files of lots of programs).

I connected to my VPS using PuTTY with my main Ubuntu user allu62 (the user who installed the system and is member of the sudoers). In order to be able to access all directories and to have all permissions on all files and folders, I did my backup as root. This can be done by starting a root terminal with the command
    sudo -i
Please, be aware that having the permissions to do what you want means that it's your and only your responsibility what you do!

Backing up the /home directory.

As you can see on the screenshot further down, there are three users on my Ubuntu VPS: allu62 (main Ubuntu user), admin (Hestia administrator), and allu (my "every day" user, under the account of whom my web domain and my databases have been created). It is, of course, possible to create one single backup archive for the entire /home directory. Instead, I decided to make four backups, creating archives for the home folders of allu62 and admin, for the web folder of allu, and for the rest of allu's home directory.

Also I decided to create the archives in a new subdirectory (that I called "backup") of allu's web folder. The evident advantage of this is that I will be able to easily access and download the backups using FTP. The screenshot shows the 3 user folders in /home and the subdirectories of /home/allu with web/streetinfo.lu that contain my website files. It also shows the creation of the new folder /home/allu/web/backup, where the backup archives will be stored.

Ubuntu VPS backup: Creating a directory to store the backup archives

tar is a very powerful Linux archiving utility. The general format for creating an archive is
    tar <command> --<options> <directory-to-backup>

Typical commands are:

Note, that these commands may be concatenated using one single hyphen: -cvpzf <archive-name>. There is no problem to place the options before the commands, but you cannot place the commands or the options after the positional parameters (directory to backup).

So, lets start to with backing up the home folder of allu62 (screenshot) and admin. It is supposed that the command is executed in the /home/allu/web/backup directory.
    tar -cvpzf allu62.tar.gz /home/allu62
    tar -cvpzf admin.tar.gz /home/admin
where "allu62.tar.gz" and admin.tar.gz are the names that I gave to the backup files.

Ubuntu VPS backup: Backing up a user's home directory using the 'tar' utility

Backing up allu's web directory is somewhat more complicated. In fact, the backup will be done to the "backup" subdirectory of /home/allu/web and thus our backup files would normally be included in the backup archive when backing up this directory. To avoid this, we'll tell tar to exclude the directory /home/allu/web/backup (with all its files and subdirectories if there were any) from the archive. Here the command that I used to create the backup of allu's web folder:
    tar --exclude=/home/allu/web/backup -cvpzf allu_web.tar.gz /home/allu/web

Similarly, if we want to backup the resting files and folders in allu's home folder, we'll have to exclude the web directory when creating the archive:
    tar --exclude=/home/allu/web -cvpzf allu_other.tar.gz /home/allu

The screenshot below shows the /home/allu/web/backup directory with the 4 backup archives that we have created with the tar utility. Note that the owner of these files is root (remember that we work in a root terminal), but that the files are readable by everyone. That is important in order to be able to download them onto the local machine.

Ubuntu VPS backup: .tar.gz backup archives created with the 'tar' utility

Downloading the backup archives.

Having saved the backups to allu's web directory, we can directly access them via FTP in order to download them onto the local machine. The screenshot shows FTP access to the /home/allu/web/backup folder using WinSCP. The archives are readable by everyone, so no problem to download them.

Ubuntu VPS backup: Accessing the backup archives using WinSCP in order to download them

Opening one of the downloaded files in 7-Zip, we can see that (thanks to the -p command of tar) group, user and file permissions have been preserved. That means that we'll have the possibility to restore the users' home directories exactly as they were!

Ubuntu VPS backup: Group, user and file permissions preserved in the backup archives

Backing up the databases.

MySQL/MariaDB databases can easily be backed up using phpMyAdmin. Select the database to be backed up in the left pane of the GUI and choose Export from the menu. Default options (export as SQL with all defaults) are fine. The result of such exports is an SQL file that, by default, is named <database-name>.sql.

Ubuntu VPS backup: Database backup using the 'export' feature of phpMyAdmin

If we open one of these files in Notepad++, we can see that not only the data records (database content), but also the table structure (as well as possible view and function definitions) has been exported. Thus, we have everything we need to recreate the database and fill in the data. Just importing the SQL files in phpMyAdmin...

Ubuntu VPS backup: Content (data + structure) of a database exported SQL file

If you find this text helpful, please, support me and this website by signing my guestbook.