Skip to main content

Setting Up an Incremental Backup System with Borg

··3 mins·
Backup Linux Borg
Makoto Morinaga
Author
Makoto Morinaga
A personal notebook for tech notes, coding, and system experiments.
Table of Contents

Incremental backups are essential for preserving data in case of hardware failure or accidental deletion. Borg provides an efficient way to manage backups with minimal storage usage by utilizing deduplication.

This post describes the setup process for an incremental backup system using Borg.

Installing Borg
#

Borg can be installed on the server using:

Terminal
# Arch Linux
sudo pacman -S borg

Preparing the Backup Script
#

Creating the Backup Script
#

Create backup.sh under /root/scripts/ with the following content:

/root/scripts/backup.sh
#!/bin/bash


######################################
# Configuration Parameters
######################################
# host name
hostname=`/bin/hostname`

# backup date
date=`/bin/date +"%Y%m%d_%H%M"`

# backup directory
backup_dir="/backup/${hostname}"

file_keep_days=7
file_keep_weeks=4

backup_list_file=/root/scripts/backup_list
if [ ! -s ${backup_list_file} ]; then
    echo "${backup_list_file} is not found."
    logger -t `basename ${0}` "backup aborted."
    cleanup
    exit 1
fi

# backup list
backup_list="$(cat "${backup_list_file}" | tr "\n" " ")"

# ignored backup list
excluded_backup_list_file=/root/scripts/excluded_backup_list

# post-processing function
cleanup () {
    rm -rf ${temp_excluded_backup_list_file}
    rm -rf ${backup_log_file}
}

######################################
# processing
######################################

# Initialize backup directroy
if [ ! -d ${backup_dir} ]; then
    mkdir -p ${backup_dir}
    borg init --encryption=none ${backup_dir}
fi

# Add backup directory to the list of backup exclusions
temp_excluded_backup_list_file=`mktemp`
if [ -s ${excluded_backup_list_file} ]; then
    cat ${excluded_backup_list_file} > ${temp_excluded_backup_list_file}
fi
echo "${backup_dir}" >> ${temp_excluded_backup_list_file}

# execute backup
backup_log_file=`mktemp`
logger -t `basename ${0}` "backup started."
borg create --exclude-from ${temp_excluded_backup_list_file} ${backup_dir}::${hostname}-${date} ${backup_list} > ${backup_log_file} 2>&1

code=$?
if [ ${code} -ne 0 ]; then
    logger -t `basename ${0}` "backup aborted."
    logger -t `basename ${0}` "$(cat "${backup_log_file}")"
    cleanup
    exit 1
fi

logger -t `basename ${0}` "backup finished."

# delte old backup
borg prune -v --list --keep-daily=${file_keep_days} --keep-weekly=${file_keep_weeks} ${backup_dir} >> ${backup_log_file} 2>&1

# post-processing
cleanup

exit 0

Grant execution permissions:

Terminal
sudo chmod 700 /root/scripts/backup.sh

Creating Backup Directory
#

Create a dedicated backup directory:

Terminal
sudo mkdir /backup

Defining Backup Targets
#

Specify directories to be backed up in backup_list:

/root/scripts/backup_list
/root
/home

Excluding Files from Backup
#

Specify directories and files to be excluded in excluded_backup_list:

/root/scripts/excluded_backup_list
/root/tmp
*.DS_Store
*.thumbnails

Running the Backup Script
#

Execute the backup script:

Terminal
sudo /root/scripts/backup.sh

If hostname is not found, install it using:

Terminal
# arch linux
sudo pacman -S inetutils

After execution, verify that a directory with the hostname exists under backup/.

Listing and Restoring Backups
#

To list available backups:

Terminal
sudo borg list /backup/"$(hostname)"

To list contents of a specific backup:

Terminal
sudo borg list /backup/"$(hostname)"::"$(hostname)-20180722_0939"

To restore an entire backup:

Terminal
sudo borg extract /backup/"$(hostname)"::"$(hostname)-20180722_0939"

To restore a specific file:

Terminal
sudo borg extract /backup/"$(hostname)"::"$(hostname)-20180722_0939" home/mkt3/text.txt

Automating the Backup Process
#

Systemd timers can automate the execution of the backup script daily at 1 AM.

Creating a Systemd Service
#

Create backup.service:

/etc/systemd/system/backup.service
[Unit]a
Description=Daily Backup

[Service]
Type=simple
ExecStart=/root/scripts/backup.sh

Creating a Systemd Timer
#

Create backup.timer:

/etc/systemd/system/backup.timer
[Unit]
Description=Daily Backup Timer

[Timer]
OnCalendar=*-*-* 01:00:00
Unit=backup.service

[Install]
WantedBy=timers.target

Enabling the Timer
#

Activate the timer with:

Terminal
sudo systemctl enable backup.timer

Conclusion
#

With this setup, incremental backups are maintained efficiently. The system ensures up to one month of recoverable data, mitigating risks associated with hardware failure or accidental data loss.

Related

iptables Configuration
··4 mins
Linux Firewall Iptables
Setting Up a File Sharing Server Using Samba
··3 mins
Linux Samba
Setting Up an Internal DNS Server Using NSD and Unbound
··2 mins
Unbound Nsd Linux