-
Notifications
You must be signed in to change notification settings - Fork 1
Realization of Backup Automation
This document describes how to automate the process of backing up (user) files using Anacron.
This document is related to NB, as it describes how NB works behind the scenes. Hence it's not intended for users but developers who want to adopt concepts or contribute.
NB should support users in backing up their personal files in two ways:
- use rsync, hidden by a convenient wrapper
- allow to specify multiple source directories
- provide meaningful defaults (include/exclude, flags)
- automate this process to a maximum extent
- backup user files on a daily basis or, if missed, ASAP
- public-key SSH authentication
The first aim of NB is to provide a convenient rsync wrapper. It should allow to specify multiple backup source directories in a single call and provide meaningful defaults for rsync parameters.
The mapping files specifies pairs of backup source and backup target directories (mappings) that will be synchronized via rsync. For convenience, the target directory can be specified relative to a globally configured backup destination. Due to the design of rsync, each line in this file (i.e. mapping) results in a rsync call behind the scenes.
Meaningful defaults include an inclusion and exclusion of files that work for most users and rsync flags that fit the task at hand.
Nearly all UNIX-based systems ship with a folder structure that stores all user data in dedicated folders inside the home directory. This directory will serve well as a starting point.
Some directories, however, probably should not be covered by a user's own backup:
- Dropbox, Google Drive or Owncloud/Nextcloud folders are usually synchronized to the respective service automatically.
- Locally checked out Git repositories are largely synchronized with their remote(s). Especially the Git index with its numerous (tiny) files causes a performance penalty.
The following table lists all the rsync flags that are used by the NB backup script.
Flag | Description / Reasoning |
---|---|
Output | |
-h | use human readable numbers |
-i | show change summary |
--progress | show progress during transfer |
Transfer Options | |
-r | recursive |
-l | copy soft symlinks as-is |
-p | preserve permissions |
-t | preserve modification times |
-g | preserve group |
-o | preserve owner |
-D | preserve devices and special files |
-s | no space-splitting |
-u | update files (skip if target existing and newer) |
-z | file compression during transfer |
The following section describes why and how Anacron has been choosen as a scheduler for the NB backup script.
Consider a backup script that is able to backup all relevant user files. In order to let backups run automatically, e.g. on a daily basis, we need a scheduler that runs this script. Due to file permissions and security reasons, we must run the backup script as the user that's owning the files.
Cron is executed in user mode but only runs at a specified time / reboot. Missed jobs are not executed at all. Backups may skip multiple days even if the user is actually using the machine every day but at the 'wrong' time.
Anacron executes missed jobs but is executed as root. This leads to undesired effects in the SSH authentication process. A solution is to use anacron in the user mode.
-
To set up a user anacrontab create the anacron files and directories in the user's home:
mkdir ~/etc && touch ~/etc/anacrontab mkdir -p ~/var/spool/anacron
(The first directory holds user-specific configuration files, such as the anacrontab file. The second directory is used by anacron to store job timestamps to keep track of which ones are due and which are not.)
-
Create an anacrontab for the user that starts the script in the period of your choice (here: daily):
# /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # period delay job-identifier command 1 2 user.backup /usr/local/bin/backupnas.sh
-
Call anacron from
~/.profile
to execute the job on each login, if it's due:# run anacron in user mode /usr/sbin/anacron -s -t ${HOME}/etc/anacrontab -S ${HOME}/var/spool/anacron
If your computer runs for a long period and doesn't login daily, this approach would be insufficient. In this case it would be better to create a daily user cronjob that calls anacron.
This way the SSH key doesn't have to be unlocked on each SSH command.
In order to test if the everything is working as expected, run anacron in foreground mode, forcing the immediate execution of configured jobs:
/usr/sbin/anacron -s -t ${HOME}/etc/anacrontab -S ${HOME}/var/spool/anacron -dfn