add borg
This commit is contained in:
61
modules/homelab/services/arr/default.nix.template
Normal file
61
modules/homelab/services/arr/default.nix.template
Normal file
@@ -0,0 +1,61 @@
|
||||
{ pkgs, config, lib, ... }:
|
||||
|
||||
let
|
||||
cfg = config.modules.services.<service_name>;
|
||||
ids = <gid_and_uid_number>;
|
||||
in
|
||||
{
|
||||
options.modules.services.<service_name> = {
|
||||
enable = lib.mkEnableOption "enables <service_name>";
|
||||
# extra options
|
||||
# mode = lib.mkOption {
|
||||
# type = lib.types.enum [ "server" "client" ];
|
||||
# default = "client";
|
||||
# description = "whether syncthing should run as a client (user) or server (system-wide).";
|
||||
# };
|
||||
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
|
||||
# declare <service_name> group
|
||||
users.groups.<service_name> = { gid = ids; };
|
||||
|
||||
# declare <service_name> user
|
||||
users.users.<service_name> = {
|
||||
description = "<service_name> media server user";
|
||||
uid = ids;
|
||||
isSystemUser = true;
|
||||
home = "/var/lib/<service_name>";
|
||||
createHome = true;
|
||||
group = "<service_name>";
|
||||
extraGroups = [ "media" "video" "render" ];
|
||||
};
|
||||
|
||||
# enable the <service_name> service
|
||||
services.<service_name> = {
|
||||
enable = true;
|
||||
openFirewall = true; # Opens 8096/8920 automatically
|
||||
user = "<service_name>"; # Default: <service_name>
|
||||
group = "<service_name>"; # Default: <service_name>
|
||||
dataDir = "/var/lib/<service_name>"; # Config + metadata storage
|
||||
};
|
||||
|
||||
# override umask to make permissions work out
|
||||
systemd.services.<service_name>.serviceConfig = { UMask = lib.mkForce "0007"; };
|
||||
|
||||
# open firewall
|
||||
#networking.firewall.allowedTCPPorts = [ 8096 ];
|
||||
|
||||
# reverse proxy entryo
|
||||
services.nginx.virtualHosts."media.blakedheld.xyz" = {
|
||||
enableACME = false;
|
||||
forceSSL = true;
|
||||
sslCertificate = config.sops.secrets."ssl_blakedheld_crt".path;
|
||||
sslCertificateKey = config.sops.secrets."ssl_blakedheld_key".path;
|
||||
locations."/" = {
|
||||
proxyPass = "http://127.0.0.1:8096";
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
BIN
modules/homelab/services/arr/sonarr_backup_v4.0.15.2941_2025.10.01_20.42.01.zip
Executable file
BIN
modules/homelab/services/arr/sonarr_backup_v4.0.15.2941_2025.10.01_20.42.01.zip
Executable file
Binary file not shown.
@@ -4,17 +4,18 @@ let
|
||||
cfg = config.modules.services.<service_name>;
|
||||
ids = <gid_and_uid_number>;
|
||||
default_port = <port_number>;
|
||||
data_dir = "/var/lib/<service_name>";
|
||||
in
|
||||
{
|
||||
options.modules.services.<service_name> = {
|
||||
enable = lib.mkEnableOption "enables <service_name>";
|
||||
|
||||
# set port options
|
||||
# port = lib.mkOption {
|
||||
# type = lib.types.int;
|
||||
# default = cfg.default_port;
|
||||
# description = "set port for <service_name> (default: ${toString default_port}";
|
||||
# };
|
||||
# set port options
|
||||
port = lib.mkOption {
|
||||
type = lib.types.int;
|
||||
default = cfg.default_port;
|
||||
description = "set port for <service_name> (default: ${toString default_port}";
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
@@ -25,7 +26,7 @@ in
|
||||
|
||||
# declare <service_name> user
|
||||
users.users.<service_name> = {
|
||||
description = "<service_name> media server user";
|
||||
description = "<service_name> server user";
|
||||
uid = ids;
|
||||
isSystemUser = true;
|
||||
home = "/var/lib/<service_name>";
|
||||
@@ -37,21 +38,20 @@ in
|
||||
# enable the <service_name> service
|
||||
services.<service_name> = {
|
||||
enable = true;
|
||||
openFirewall = true; # Opens 8096/8920 automatically
|
||||
user = "<service_name>"; # Default: <service_name>
|
||||
group = "<service_name>"; # Default: <service_name>
|
||||
dataDir = "/var/lib/<service_name>"; # Config + metadata storage
|
||||
dataDir = "/var/lib/<service_name>"; # Config + metadata storage
|
||||
# settings = {
|
||||
# server.port = cfg.port;
|
||||
# };
|
||||
openFirewall = true;
|
||||
user = "<service_name>";
|
||||
group = "<service_name>";
|
||||
dataDir = data_dir;
|
||||
settings = {
|
||||
server.port = cfg.port;
|
||||
};
|
||||
};
|
||||
|
||||
# override umask to make permissions work out
|
||||
systemd.services.<service_name>.serviceConfig = { UMask = lib.mkForce "0007"; };
|
||||
|
||||
# open firewall
|
||||
#networking.firewall.allowedTCPPorts = [ port ];
|
||||
# # open firewall
|
||||
# networking.firewall.allowedTCPPorts = [ port ];
|
||||
|
||||
# reverse proxy entryo
|
||||
services.nginx.virtualHosts."<service_name>.snowbelle.lan" = {
|
||||
@@ -63,5 +63,8 @@ in
|
||||
proxyPass = "http://127.0.0.1:${toString port}";
|
||||
};
|
||||
};
|
||||
|
||||
# add to backups
|
||||
modules.system.backups.paths = lib.mkIf cfg.backups [ data_dir ];
|
||||
};
|
||||
}
|
||||
|
||||
75
modules/system/backups.nix
Normal file
75
modules/system/backups.nix
Normal file
@@ -0,0 +1,75 @@
|
||||
{ config, lib, pkgs, ... }:
|
||||
|
||||
let
|
||||
borg = "${pkgs.borgbackup}/bin/borg";
|
||||
backup_paths = lib.unique config.modules.system.backups.paths;
|
||||
passwd_file = config.sops.secrets."borg_passwd".path;
|
||||
in
|
||||
{
|
||||
options.modules.system.backups = {
|
||||
paths = lib.mkOption {
|
||||
type = lib.types.listOf lib.types.path;
|
||||
default = [];
|
||||
description = "list of directories to back up";
|
||||
};
|
||||
repo = lib.mkOption {
|
||||
type = lib.types.path;
|
||||
default = "/backups";
|
||||
description = "borg repository path";
|
||||
};
|
||||
passphraseFile = lib.mkOption {
|
||||
type = lib.types.path;
|
||||
default = passwd_file;
|
||||
description = "borg repository passphrase file";
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf (backup_paths != []) {
|
||||
systemd.services.borg-backup = {
|
||||
description = "BorgBackup service for system data";
|
||||
serviceConfig = {
|
||||
Type = "oneshot";
|
||||
EnvironmentFile = config.modules.system.backups.passphraseFile;
|
||||
};
|
||||
script = ''
|
||||
set -eux
|
||||
export BORG_REPO=${config.modules.system.backups.repo}
|
||||
export BORG_PASSPHRASE="$(cat ${config.modules.system.backups.passphraseFile})"
|
||||
|
||||
# Initialize repo if it doesn’t exist
|
||||
if ! ${borg} info "${repo}" >/dev/null 2>&1; then
|
||||
echo "Initializing new Borg repository at ${repo}"
|
||||
${borg} init --encryption=repokey "${repo}"
|
||||
fi
|
||||
|
||||
timestamp=$(date +%Y-%m-%d_%H-%M-%S)
|
||||
|
||||
${borg} create \
|
||||
--stats \
|
||||
--compression zstd,3 \
|
||||
::${timestamp} \
|
||||
${lib.concatStringsSep " " backup_paths}
|
||||
|
||||
# Retention policy
|
||||
${borg} prune -v --list \
|
||||
--keep-daily=7 \
|
||||
--keep-weekly=52 \
|
||||
--keep-monthly=-1
|
||||
'';
|
||||
};
|
||||
|
||||
systemd.timers.borg-backup = {
|
||||
description = "daily borg backup timer";
|
||||
wantedBy = [ "timers.target" ];
|
||||
timerConfig = {
|
||||
OnCalendar = "daily";
|
||||
Persistent = true;
|
||||
};
|
||||
};
|
||||
|
||||
environment.systemPackages = [ pkgs.borgbackup ];
|
||||
};
|
||||
}
|
||||
|
||||
# add to module
|
||||
# modules.system.backups.paths = lib.mkIf cfg.backups [ <path> ];
|
||||
Reference in New Issue
Block a user