How to perform system backup with backup-manager on Linux

Last updated on August 21, 2020 by Gabriel Cánepa

One thing that we all may have had the chance to learn in either easy or hard ways is that the importance of backups can never be underestimated. Considering that there are as many backup methods as the number of fish in the sea, you may wonder how you can effectively choose the right tool and strategy for your system.

In this article I will introduce you to backup-manager, a simple yet handy command-line backup tool which is available in the standard repositories of most Linux distributions.

What makes backup-manager stand out among other backup tools or strategies? Let me mention just a few of its distinguishing features:

Installing backup-manager on Linux

Installation of backup-manager is quick and effortless since it is included in the base repositories of most Linux distributions.

For Debian, Ubuntu and Their Derivatives:

# aptitude install backup-manager

During the installation process on Debian-based systems, you will be prompted to enter the directory where you want to store generated backup archives. If the chosen directory does not exist, it will be created automatically when you run backup-manager for the first time.

Select OK and press ENTER.

In the next step, you will be asked to enter all the directories (separated by space) that you want to be backed up. It is advised, though not strictly required, to list several sub-directories of the same parent directory instead of entering only the parent directory.

You can skip this step and configure the list of directories later using the variable BM_TARBALL_DIRECTORIES in the configuration file. Otherwise, feel free to add as many directories as you wish, and then choose OK:

For Fedora, CentOS or RHEL:

# yum install backup-manager

On CentOS/RHEL, you will need to enable EPEL repository first before running the yum command.

On Red Hat based systems, by default, backup-manager does not back up any directories. If you want to enable backup, open /etc/backup-manager.conf with a text editor, and define the following variables.

# where to store generated backup archives
export BM_REPOSITORY_ROOT="/var/backup-manager"

# a comma-separated list of directories to back up
export BM_TARBALL_DIRECTORIES="/etc /home /var/log"

For more details on other backup related settings, refer to the next section.

Configuring backup-manager

The main configuration file for backup-manager is /etc/backup-manager.conf. This file is divided into sections where the backup methods and their associated variables (or keys) are defined, making backup-manager a versatile tool that can be used in a wide variety of cases.

For demonstration purposes, we will consider the following scenario:

Open /etc/backup-manager.conf with your favorite text editor and edit the following variables. Feel free to disregard the lines beginning with # if you want. They are provided only as explanatory comments in this article:

# Specify the backup method(s) that will be used.
# tarball: takes a list of directories and builds the corresponding tarballs.
# mysql: archives MySQL databases using mysqldump. To restore the database, you # need to use the same tool manually.
export BM_ARCHIVE_METHOD="tarball mysql"

# Where to store the backups.
export BM_REPOSITORY_ROOT="/var/archives"

# The following directive indicates backup-manager to name
# the generated files after the directory that was backed up.
export BM_TARBALL_NAMEFORMAT="long"

# Define the compression type for the generated files.
export BM_TARBALL_FILETYPE="tar.gz"

# List the directories that you want to backup.
export BM_TARBALL_DIRECTORIES="/etc /home /var/log"

# Exclude some subdirectories or file extensions.
export BM_TARBALL_BLACKLIST="/var/log/myotherapp.log *.mp3 *.mp4"

# List the database(s) that you want to backup, separated by spaces.
export BM_MYSQL_DATABASES="mysql mybase wordpress dotclear phpbb2"

# MySQL username.
export BM_MYSQL_ADMINLOGIN="root"

# MySQL password for username.
export BM_MYSQL_ADMINPASS="mypassword"

# Add support for DROP statements (optional).
export BM_MYSQL_SAFEDUMPS="true"

# The hostname or IP address where the database(s) reside.
export BM_MYSQL_HOST="localhost"

# Port where MySQL server is listening.
export BM_MYSQL_PORT="3306"

# Compression type (optional).
export BM_MYSQL_FILETYPE="gzip"

# Do not archive remote hosts, but only localhost.
BM_TARBALL_OVER_SSH="false"

# User account for SSH upload.
export BM_UPLOAD_SSH_USER="root"

# Absolute path of the user's private key for passwordless SSH login.
export BM_UPLOAD_SSH_KEY="/root/.ssh/id_rsa"

# Remote hosts (make sure you have exported your public key to them):
export BM_UPLOAD_SSH_HOSTS="dev1 dev3"

# Remote destination for uploading backups. If it doesn't exist,
# this directory will be created automatically the first time
# backup-manager runs.
export BM_UPLOAD_SSH_DESTINATION="/var/archives/backups/$HOSTNAME"

If you want to back up directories whose name contain a space, comment out BM_TARBALL_DIRECTORIES variable definition, and use the following format instead.

declare -a BM_TARBALL_TARGETS
BM_TARBALL_TARGETS[0]="/etc/Backup Files"
BM_TARBALL_TARGETS[1]="/home/xmodulo/My Documents"
BM_TARBALL_TARGETS[2]="/var/log"
export BM_TARBALL_TARGETS

Running backup-manager

To run backup-manager manually, type the following command. Optionally, you can add the -v flag in order to examine the process step by step, in a verbose way.

# backup-manager

The directories listed in BM_TARBALL_DIRECTORIES will be backed up in BM_REPOSITORY_ROOT as tarballs, and then transferred over SSH to hosts dev1 and dev3 specified in BM_UPLOAD_SSH_DESTINATION.

As can be seen in the above image, backup-manager during runtime creates a file named /root/.backup-manager_my.cnf with the MySQL password provided in BM_MYSQL_ ADMINPASS. That way, mysqldump can authenticate to a MySQL server without having to accept login password through the command line in plain-text format, which poses a security risk.

Running backup-manager through cron

Once you have decided what is the best day of the week (and the best time) to perform your weekly backup, you can have cron run backup-manager for you.

Open root's crontab file (note that you must be logged on as root):

# crontab -e

Assuming that you want to run backup-manager on Sunday at 5:15 am, add the following line.

15 05 * * 0 /usr/sbin/backup-manager > /dev/null 2>&1

Summary

In this article we have shown how backup-manager is a simple, yet powerful and easy-to-use backup tool. There are several other options that you may want to consider in your backup strategy, so feel free to refer to the man page or to the user guide, which also contains several implementation examples and advice.

Hope it helps. Feel free to leave your questions and comments below.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean