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:
# Arch Linux
sudo pacman -S borg
Preparing the Backup Script #
Creating the Backup Script #
Create backup.sh
under /root/scripts/
with the following content:
#!/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:
sudo chmod 700 /root/scripts/backup.sh
Creating Backup Directory #
Create a dedicated backup directory:
sudo mkdir /backup
Defining Backup Targets #
Specify directories to be backed up in backup_list
:
/root
/home
Excluding Files from Backup #
Specify directories and files to be excluded in excluded_backup_list
:
/root/tmp
*.DS_Store
*.thumbnails
Running the Backup Script #
Execute the backup script:
sudo /root/scripts/backup.sh
If hostname
is not found, install it using:
# 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:
sudo borg list /backup/"$(hostname)"
To list contents of a specific backup:
sudo borg list /backup/"$(hostname)"::"$(hostname)-20180722_0939"
To restore an entire backup:
sudo borg extract /backup/"$(hostname)"::"$(hostname)-20180722_0939"
To restore a specific file:
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
:
[Unit]a
Description=Daily Backup
[Service]
Type=simple
ExecStart=/root/scripts/backup.sh
Creating a Systemd Timer #
Create 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:
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.