Skip to content

Instantly share code, notes, and snippets.

@phdoerfler
Created January 12, 2018 19:38
Show Gist options
  • Save phdoerfler/7db4c8598a87949a0f4522c7adcadcc7 to your computer and use it in GitHub Desktop.
Save phdoerfler/7db4c8598a87949a0f4522c7adcadcc7 to your computer and use it in GitHub Desktop.
A work in progress backup of dovecot maildirs using rsnapshot and dsync.
{ config, pkgs, lib, ... }:
let
cfg = config.mailserver;
backupUser = "tardis";
dsyncBackupScript = "dsyncbackup.sh";
set = cfg.loginAccounts;
mailboxes = builtins.attrNames set;
lines = lib.lists.imap0 (i: v: "dsync -f -o plugin/acl= -o plugin/quota= -o plugin/zlib_save= -u ${toString v} backup maildir:$WORK_DIR/${toString v}") mailboxes;
scriptLines = builtins.concatStringsSep "\n" lines;
script = pkgs.writeScript (toString dsyncBackupScript) ''
#!${pkgs.stdenv.shell}
set -e
# create temp directory that dsync can write to
WORK_DIR=$(mktemp -d)
chmod o+rwx $WORK_DIR
function cleanup {
rm -rf "$WORK_DIR"
echo "Deleted temp working directory $WORK_DIR"
}
# register cleanup
trap cleanup EXIT
# backs up all mailboxes to subdirectories of the current one to be picked up by rsnapshot
${scriptLines}
# move content of tmp directory into current directory
mv $WORK_DIR/* .
'';
in {
# dsync backup of a single mailbox
services.rsnapshot = {
enable = true;
cronIntervals = {
# minute, hour, day-in-month, month, weekday (0 = sunday)
"hourly" = " 0 * * * *"; # Every full hour
"daily" = "50 21 * * *"; # Every day at 21:50
"weekly" = " 0 5 * * 0"; # Every sunday at 5:00 AM
};
# rsnapshot expects intervals shortest first, e.g. hourly first, then daily.
# tabs must separate all elements
extraConfig = ''
#snapshot_root /home/${backupUser}/rsnapshot
snapshot_root /var/rsnapshot
retain hourly 24
retain daily 365
retain weekly 54
backup_exec chmod o+rx g+rx /var/rsnapshot
backup_script ${script} localhost/dsync/
backup /var/vmail/ localhost/
'';
};
users.extraUsers."${backupUser}" = {
isSystemUser = true;
home = "/home/${backupUser}";
createHome = true;
extraGroups = [ ];
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment