Backup a server on Debian or Ubuntu Linux

Backup a server can limit the risk of data loss and facilitate reinstall in case of failure. It is therefore imperative to regularly backup all critical data to a local disk or to a remote server.

A backup can be done manually by running a script or automatically by creating a scheduled taskKeep multiple backups and customize the backup frequency can adapt the amount of data to store and “go back in time”.

This post describes how to backup a server on Debian or Ubuntu Linux.

Prerequisites

Install rsync

rsync is a file synchronization tool.

apt-get install rsync

Create a local backup

The script below provides an example of backing up data to a local disk.

DB_USER:DB_PASSWORD is the administrator of the databases to export. Files are saved in the directory DIR.

nano /root/backup

# !/bin/sh

# Variables loading
DB_USER="root"
DB_PASSWORD="mypassword"
DIR="/var/backups/mybackup"

# Databases exporting
/usr/bin/mysqldump -u$DB_USER -p$DB_PASSWORD --all-databases > /root/databases.sql

# Directories creating
mkdir -p $DIR
mkdir -p $DIR/etc
mkdir -p $DIR/etc/apache2
mkdir -p $DIR/home
mkdir -p $DIR/root
touch $DIR

# Files backing up
rsync -a --delete /etc/apache2/sites-available $DIR/etc/apache2
rsync -a --delete /etc/hostname $DIR/etc
rsync -a --delete /home/myuser $DIR/home
rsync -a --delete /root/databases.sql $DIR/root

chmod 744 /root/backup

The command /root/backup runs the backup. See the following paragraphs to keep multiple backups and automate the backup.

Create a remote backup

Remote backup is done through the SSH protocol. The server to backup must therefore have an automatic SSH connection for the user root to the remote server (see Connect to SSH without entering a password on Linux).

The script below provides an example of backing up data to a remote server.

DB_USER:DB_PASSWORD is the administrator of the databases to export. Files are saved to the server HOST on the port PORT in the directory DIR.

nano /root/backup

# !/bin/sh

# Variables loading
DB_USER="root"
DB_PASSWORD="mypassword"
HOST="root@myip"
PORT=22
DIR="/var/backups/mybackup"

# Databases exporting
/usr/bin/mysqldump -u$DB_USER -p$DB_PASSWORD --all-databases > /root/databases.sql

# Directories creating
ssh -p $PORT $HOST <<EOF
    mkdir -p $DIR
    mkdir -p $DIR/etc
    mkdir -p $DIR/etc/apache2
    mkdir -p $DIR/home
    mkdir -p $DIR/root
    touch $DIR
    logout
EOF

# Files backing up
rsync -aze "ssh -p $PORT" --delete /etc/apache2/sites-available $HOST:$DIR/etc/apache2
rsync -aze "ssh -p $PORT" --delete /etc/hostname $HOST:$DIR/etc
rsync -aze "ssh -p $PORT" --delete /home/myuser $HOST:$DIR/home
rsync -aze "ssh -p $PORT" --delete /root/databases.sql $HOST:$DIR/root

chmod 744 /root/backup

The command /root/backup runs the backup. See the following paragraphs to keep multiple backups and automate the backup.

Keep multiple backups

To keep multiple backups and be able to “go back in time”, simply use a dynamic directory name.

For example:

  • To keep 7 days of daily backups the directory contains the day number of the week %w.
    DIR="/var/backups/mybackups/$(date +%w)"
  • To keep 1 month of daily backups the directory contains the day number of the month %d.
    DIR="/var/backups/mybackups/$(date +%d)"
  • To keep 1 year of daily backups the directory contains the day number of the year %j.
    DIR="/var/backups/mybackups/$(date +%j)"

Automate backup

To automate backup and customize its frequency, simply create a scheduled task by using crontab.

crontab -e

For example:

  • To run a backup every day at 4am.
    0 4 * * * /root/backup > /dev/null
  • To run a backup once a week at 4am.
    0 4 * * 1 /root/backup > /dev/null

» Create and install a firewall on Debian or Ubuntu Linux

Leave a Reply

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

*