Skip to content

Commit

Permalink
feat(backups): add local harddrive backup service and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenhoenle committed Nov 10, 2024
1 parent 4d7afa3 commit 8245d91
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ _Don't forget to specify root as username when connecting to the initrd ssh sess

## Backups

### Offsite backup

```bash
# start
systemctl start restic-backups-fullbackup
Expand All @@ -34,6 +36,30 @@ restic-fullbackup ls latest /var/lib/
/run/current-system/sw/bin/restic-fullbackup restore --target / latest
```

### Local harddrive backup

```bash
# IMPORTANT: the mount/unmount pkgs are only available for the root user

# mount the HDD backup drive
hdd-mount

# starting the HDD backup
systemctl start restic-backups-hdd

# showing the status of the HDD backup
systemctl status restic-backups-hdd

# unmount the HDD backup drive
hdd-unmount

# showing the snapshots of the HDD backup
restic-hdd snapshots

# restoring the backup from the HDD
restic-hdd restore latest --target /
```

## Podman containers

To view the logs of the podman containers specified in the nix config, use the following command:
Expand Down
6 changes: 6 additions & 0 deletions modules/users.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{ pkgs, ... }:
let
hddMountScript = import ../pkgs/hdd-mount.nix { inherit pkgs; };
hddUnmountScript = import ../pkgs/hdd-unmount.nix { inherit pkgs; };
in
{
users.users.ruben = {
isNormalUser = true;
Expand All @@ -16,4 +20,6 @@

/* group which provides access to restic agenix secrets */
users.groups.backup = { };

users.users.root.packages = [ hddMountScript hddUnmountScript ];
}
7 changes: 7 additions & 0 deletions pkgs/hdd-mount.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ pkgs, ... }: pkgs.writeShellApplication {
name = "hdd-mount";
text = ''
mkdir -p /mnt/SAMSUNG
mount LABEL=SAMSUNG /mnt/SAMSUNG
'';
}
6 changes: 6 additions & 0 deletions pkgs/hdd-unmount.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{ pkgs, ... }: pkgs.writeShellApplication {
name = "hdd-unmount";
text = ''
umount LABEL=SAMSUNG
'';
}
35 changes: 28 additions & 7 deletions services/backup.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ let
cfg.paperless.backupPrepareCommandExport
]
));
backupPrepareScriptHdd = pkgs.writeText "backup-hdd-prepare-script.sh" (pkgs.lib.strings.concatLines (
[ ] ++ pkgs.lib.ifEnable cfg.paperless.enable [
cfg.paperless.backupPrepareCommandDatabase
cfg.paperless.backupPrepareCommandExport
]
));

excludeFile = pkgs.writeText "restic-excludes.txt"
''
Expand All @@ -32,24 +38,27 @@ let
/home/ruben/.zsh_history
/home/ruben/.zshrc
'';
restic-common = {
paths = [ "/home/ruben" ]
++ pkgs.lib.ifEnable cfg.gitserver.enable [ cfg.gitserver.path ]
++ pkgs.lib.ifEnable cfg.fileserver.enable [ cfg.fileserver.path ]
++ pkgs.lib.ifEnable cfg.phone-backup.enable [ cfg.phone-backup.path ]
++ pkgs.lib.ifEnable cfg.paperless.enable [ cfg.paperless.path cfg.paperless.backup-path ];
};
in
{
options.ruben.fullbackup.enable = lib.mkEnableOption "full backup";

config = lib.mkIf (cfg.fullbackup.enable)
{
/* backup service */
/* automated backup service */
services.restic.backups.fullbackup = {
user = "root";
initialize = true;
passwordFile = config.age.secrets.resticPassword.path;
repository = "s3:https://s3.eu-central-003.backblazeb2.com/nixos-server-restic-backup/system-backup/${hostname}";
environmentFile = config.age.secrets.backblazeB2ResticS3EnvironmentSecrets.path;
paths = [ "/home/ruben" ]
++ pkgs.lib.ifEnable cfg.gitserver.enable [ cfg.gitserver.path ]
++ pkgs.lib.ifEnable cfg.fileserver.enable [ cfg.fileserver.path ]
++ pkgs.lib.ifEnable cfg.phone-backup.enable [ cfg.phone-backup.path ]
++ pkgs.lib.ifEnable cfg.paperless.enable [ cfg.paperless.path cfg.paperless.backup-path ];
paths = restic-common.paths;
backupPrepareCommand = "${pkgs.bash}/bin/bash ${backupPrepareScript}";
pruneOpts = [
"--keep-hourly 48"
Expand All @@ -66,11 +75,11 @@ in
};
};

/* monitoring for automated backup */
systemd.services."restic-backups-fullbackup" = {
onSuccess = [ "[email protected]" ];
onFailure = [ "[email protected]" ];
};

systemd.services."restic-notify-fullbackup@" =
let
script = pkgs.writeText "restic-notify-fullbackup.sh"
Expand All @@ -85,5 +94,17 @@ in
ExecStart = "${pkgs.bash}/bin/bash ${script}";
};
};

/* restic backup service to a local drive */
services.restic.backups.hdd = {
user = "root";
initialize = true;
passwordFile = config.age.secrets.resticPassword.path;
repository = "/mnt/SAMSUNG/restic-nixos-server";
paths = restic-common.paths;
backupPrepareCommand = "${pkgs.bash}/bin/bash ${backupPrepareScriptHdd}";
extraBackupArgs = [ "--exclude-caches" "--exclude-file=${excludeFile}" ];
timerConfig = null;
};
};
}

0 comments on commit 8245d91

Please sign in to comment.