Skip to content

Commit

Permalink
Add rbbackup service, configure LDAP backups
Browse files Browse the repository at this point in the history
  • Loading branch information
m1cr0man committed Mar 14, 2021
1 parent 1a2b5a7 commit c2ea84f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
29 changes: 29 additions & 0 deletions common/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,35 @@ with lib;
example = "zbackup/nfs";
type = types.str;
};

rbbackup = with types; {
destination = mkOption {
description = "Where to rsync the backup data to.";
default = "[email protected]:/zbackup/generic/${config.networking.hostName}/";
type = str;
};

sources = mkOption {
description = "Paths on the current system to be backed up.";
default = [];
type = listOf str;
};

commands = mkOption {
description = (
"Commands to run before commencing copy of backups. Files created"
+ " in the current working directory will be deleted after backups are completed."
);
default = "";
type = str;
};

extraPackages = mkOption {
description = "Packages to make available in the backup commands scripts.";
default = [];
type = listOf path;
};
};
};

config.assertions = [
Expand Down
1 change: 1 addition & 0 deletions common/sysconfig.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ in {
./options.nix
./ldap.nix
../packages/overlays
../services/rbbackup.nix
];

time.timeZone = "Europe/Dublin";
Expand Down
3 changes: 3 additions & 0 deletions hosts/albus/configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ in {
# Keep longer monthly snapshots
services.zfs.autoSnapshot.monthly = lib.mkForce 3;

# Albus _is_ the backup hosts - change rbbackup destination
redbrick.rbbackup.destination = "/zbackup/generic/albus/";

users.users.rbbackup = {
useDefaultShell = true;
openssh.authorizedKeys.keys = [
Expand Down
8 changes: 8 additions & 0 deletions services/ldap/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,13 @@ in {
};
};

# Configure backups of LDAP
redbrick.rbbackup.sources = [ "ldap.ldif.gz" ];
redbrick.rbbackup.extraPackages = with pkgs; [ openldap gzip ];
redbrick.rbbackup.commands = ''
ldapsearch -b o=redbrick -xLLL -D ${slurpdDN} -y ${slurpdpwFile} | gzip -8 > ldap.ldif.gz
chmod 400 ldap.ldif.gz
'';

networking.firewall.allowedTCPPorts = [ 389 ];
}
41 changes: 41 additions & 0 deletions services/rbbackup.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{ pkgs, config, lib, ... }:
with config.redbrick.rbbackup;
let
# Quote and join paths for arguments to rsync
sourcePaths = lib.concatStringsSep " " (builtins.map
(srcPath: "'${srcPath}'")
sources);

in lib.mkIf (sources != []) {
systemd.services.redbrick-backup = {
aliases = [ "rbbackup.service" "redbrick-backups.service" ];
description = "Redbrick backup script. Copies data to ${destination}";
wants = [ "multi-user.target" ];
requires = [ "local-fs.target" "network-online.target" ];
path = with pkgs; [ rsync openssh ] ++ extraPackages;
script = ''
set -euxo pipefail
echo "Backup starting"
rsync -a --progress --delete ${sourcePaths} ${destination}
echo "Backup successful"
'';
preStart = commands;
postStart = ''
rm -rf $CACHE_DIRECTORY
'';
serviceConfig = {
Type = "oneshot";
CacheDirectory = "redbrick-backups";
WorkingDirectory = "/var/cache/redbrick-backups";
};
};

systemd.timers.redbrick-backup = {
description = "Start Redbrick backup script";
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = "hourly";
RandomizedDelaySec = 60 * 30;
};
};
}

0 comments on commit c2ea84f

Please sign in to comment.