Compare commits
No commits in common. "8722afe2c8ff91c6ecf6e3c249c4dc667a260574" and "162c35769cc06b117b6753eb93460af650b64921" have entirely different histories.
8722afe2c8
...
162c35769c
33
backup.nix
33
backup.nix
@ -11,7 +11,30 @@ let
|
|||||||
runtimeInputs = [
|
runtimeInputs = [
|
||||||
pkgs.restic
|
pkgs.restic
|
||||||
];
|
];
|
||||||
text = (builtins.readFile ./backup.sh);
|
text = ''
|
||||||
|
if [ -z "$BACKUP_HELPER_DIRS" ]; then
|
||||||
|
echo "BACKUP_HELPER_DIRS is not set"
|
||||||
|
exit 1;
|
||||||
|
fi
|
||||||
|
exit_code=0;
|
||||||
|
for i in ''${BACKUP_HELPER_DIRS//,/ }; do
|
||||||
|
echo "Starting backup of $i";
|
||||||
|
if ! output=$(restic backup "$i"); then
|
||||||
|
exit_code=1;
|
||||||
|
echo "Backup of $i failed with exit code $?:"
|
||||||
|
echo "$output"
|
||||||
|
else
|
||||||
|
echo "Backup of $i successful:"
|
||||||
|
echo "$output"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [ "$BACKUP_FORGET" -eq 1 ]; then
|
||||||
|
echo "Removing old backups"
|
||||||
|
output=$(restic forget -d 7 -w 4 -m 6 --keep-within 1d --prune)
|
||||||
|
echo "$output"
|
||||||
|
fi
|
||||||
|
exit "$exit_code";
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
@ -27,11 +50,6 @@ in
|
|||||||
default = [ ];
|
default = [ ];
|
||||||
description = "Directories to be backed up.";
|
description = "Directories to be backed up.";
|
||||||
};
|
};
|
||||||
backup-commands = lib.mkOption {
|
|
||||||
type = lib.types.listOf lib.types.str;
|
|
||||||
default = [ ];
|
|
||||||
description = "Backup the stdout of commands. Format strings like key:command";
|
|
||||||
};
|
|
||||||
schedule = lib.mkOption {
|
schedule = lib.mkOption {
|
||||||
type = lib.types.str;
|
type = lib.types.str;
|
||||||
default = "*-*-* 00:00:00";
|
default = "*-*-* 00:00:00";
|
||||||
@ -65,9 +83,6 @@ in
|
|||||||
}
|
}
|
||||||
// lib.attrsets.optionalAttrs (builtins.hasAttr "password-file" cfg) {
|
// lib.attrsets.optionalAttrs (builtins.hasAttr "password-file" cfg) {
|
||||||
RESTIC_PASSWORD_FILE = cfg.password-file;
|
RESTIC_PASSWORD_FILE = cfg.password-file;
|
||||||
}
|
|
||||||
// lib.attrsets.optionalAttrs (cfg.backup-commands != [ ]) {
|
|
||||||
BACKUP_HELPER_COMMANDS = lib.strings.concatStringsSep ";" cfg.backup-commands;
|
|
||||||
};
|
};
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "oneshot";
|
Type = "oneshot";
|
||||||
|
49
backup.sh
49
backup.sh
@ -1,49 +0,0 @@
|
|||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
# If neither of BACKUP_HELPER_DIRS and BACKUP_HELPER_COMMANDS not set. Exit
|
|
||||||
if [ -z "$BACKUP_HELPER_DIRS" ] && [ -z "$BACKUP_HELPER_COMMANDS" ]; then
|
|
||||||
echo "BACKUP_HELPER_DIRS is not set"
|
|
||||||
exit 1;
|
|
||||||
fi
|
|
||||||
exit_code=0;
|
|
||||||
|
|
||||||
# Backup directories
|
|
||||||
if [ -n "$BACKUP_HELPER_DIRS" ]; then
|
|
||||||
for i in ${BACKUP_HELPER_DIRS//,/ }; do
|
|
||||||
echo "Starting backup of $i";
|
|
||||||
if ! output=$(restic backup "$i"); then
|
|
||||||
exit_code=1;
|
|
||||||
echo "Backup of $i failed with exit code $?:"
|
|
||||||
echo "$output"
|
|
||||||
else
|
|
||||||
echo "Backup of $i successful:"
|
|
||||||
echo "$output"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Backup command outputs
|
|
||||||
if [ -n "$BACKUP_HELPER_COMMANDS" ]; then
|
|
||||||
IFS=";" read -r -a pairs <<< "$BACKUP_HELPER_COMMANDS"
|
|
||||||
|
|
||||||
for pair in "${pairs[@]}"; do
|
|
||||||
IFS=":" read -r key value <<< "$pair"
|
|
||||||
|
|
||||||
if ! output=$(restic backup --stdin-filename "$key" --stdin-from-command -- "$value"); then
|
|
||||||
exit_code=1;
|
|
||||||
echo "Backup of $i failed with exit code $?:"
|
|
||||||
echo "$output"
|
|
||||||
else
|
|
||||||
echo "Backup of $i successful:"
|
|
||||||
echo "$output"
|
|
||||||
fi
|
|
||||||
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$BACKUP_FORGET" -eq 1 ]; then
|
|
||||||
echo "Removing old backups"
|
|
||||||
output=$(restic forget -d 7 -w 4 -m 6 --keep-within 1d --prune)
|
|
||||||
echo "$output"
|
|
||||||
fi
|
|
||||||
exit "$exit_code"
|
|
22
flake.nix
22
flake.nix
@ -3,8 +3,7 @@
|
|||||||
|
|
||||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||||
|
|
||||||
outputs =
|
outputs = { self, nixpkgs }:
|
||||||
{ self, nixpkgs }:
|
|
||||||
let
|
let
|
||||||
allSystems = [
|
allSystems = [
|
||||||
"x86_64-linux"
|
"x86_64-linux"
|
||||||
@ -12,31 +11,24 @@
|
|||||||
"x86_64-darwin"
|
"x86_64-darwin"
|
||||||
"aarch64-darwin"
|
"aarch64-darwin"
|
||||||
];
|
];
|
||||||
forAllSystems =
|
forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f {
|
||||||
f:
|
|
||||||
nixpkgs.lib.genAttrs allSystems (
|
|
||||||
system:
|
|
||||||
f {
|
|
||||||
pkgs = import nixpkgs { inherit system; };
|
pkgs = import nixpkgs { inherit system; };
|
||||||
}
|
});
|
||||||
);
|
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
nixosModules.backup-helper = import ./backup.nix;
|
nixosModules.backup-helper = import ./backup.nix;
|
||||||
nixosModules.default = self.nixosModules.backup-helper;
|
nixosModules.default = self.nixosModules.backup-helper;
|
||||||
|
|
||||||
devShells = forAllSystems (
|
devShells = forAllSystems ({ pkgs }: {
|
||||||
{ pkgs }:
|
|
||||||
{
|
|
||||||
default = pkgs.mkShell {
|
default = pkgs.mkShell {
|
||||||
packages = with pkgs; [
|
packages = with pkgs;
|
||||||
|
[
|
||||||
restic
|
restic
|
||||||
bash
|
bash
|
||||||
jq
|
jq
|
||||||
curl
|
curl
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
}
|
});
|
||||||
);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user