Compare commits

..

3 commits

Author SHA1 Message Date
d2947e3803 working nextcloud backups 2023-10-28 19:43:56 -07:00
4f9f3dc676 imported backup script 2023-10-28 17:06:56 -07:00
5b5bca3145 added backupscript derive 2023-10-28 17:04:37 -07:00
2 changed files with 174 additions and 10 deletions

View file

@ -4,18 +4,35 @@
{ {
imports = [ imports = [
(import ./hardware-configuration.nix) ./modules/backup_script.nix
(import ./modules/dnsmasq.nix)
(import ./modules/forgejo.nix) ./hardware-configuration.nix
(import ./modules/nginx.nix) ./modules/dnsmasq.nix
(import ./modules/nextcloud.nix) ./modules/forgejo.nix
(import ./modules/nicks_nextcould_integrations.nix) ./modules/nginx.nix
(import ./modules/msmtp.nix) ./modules/nextcloud.nix
(import ./modules/headscale.nix) ./modules/nicks_nextcould_integrations.nix
(import ./modules/tailscale.nix) ./modules/msmtp.nix
(import ./modules/vaultwarden.nix) ./modules/headscale.nix
./modules/tailscale.nix
./modules/vaultwarden.nix
]; ];
programs.alaska_backup_script = {
enable = true;
run_nightly = false;
tmp_mount_point = /Aurora/backup_drive_mount_point;
backup1_drive_label = "AlaskaBackup";
nextcloud = {
root_dir = /Aurora/nextcloud;
db_server = "127.0.0.1";
db_name = "nextcloud";
db_user = "nextcloud";
db_passfile = /Aurora/nextcloud/nextcloud_db_password;
};
};
networking.hosts = { networking.hosts = {
"100.64.0.1" = ["files.nickiel.net" "git.nickiel.net" "nickiel.net" "jellyfin.nickiel.net" ]; "100.64.0.1" = ["files.nickiel.net" "git.nickiel.net" "nickiel.net" "jellyfin.nickiel.net" ];
}; };
@ -25,6 +42,7 @@
#pkgs.jellyfin-ffmpeg #pkgs.jellyfin-ffmpeg
pkgs.hddtemp pkgs.hddtemp
pkgs.smartmontools pkgs.smartmontools
pkgs.screen
]; ];
services.xserver.videoDrivers = [ "nvidia" ]; services.xserver.videoDrivers = [ "nvidia" ];

View file

@ -0,0 +1,146 @@
{ config, pkgs, system, lib, stdenv, ...}:
let
cfg = config.programs.alaska_backup_script;
in
{
options.programs.alaska_backup_script = {
enable = lib.mkEnableOption (lib.mdDoc "Alaska Backup Script service");
run_nightly = lib.mkOption {
type = lib.types.bool;
default = false;
description = lib.mdDoc ''
Whether to enable the service to auto-run the script
'';
};
tmp_mount_point = lib.mkOption {
type = lib.types.path;
default = /Aurora/backup_drive_mount_point;
description = lib.mdDoc ''
The point used as the mount point of the primary external backup
'';
};
backup1_drive_label = lib.mkOption {
type = lib.types.str;
default = "AlaskaBackup";
description = lib.mdDoc ''
The drive label used as the primary backup device
'';
};
nextcloud = {
root_dir = lib.mkOption {
type = lib.types.path;
default = /Aurora/nextcloud;
description = lib.mdDoc ''
The root of the nextcloud file directory
'';
};
db_server = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1";
description = lib.mdDoc ''
The server to connect to with the nextcloud db credentials and backup
'';
};
db_name = lib.mkOption {
type = lib.types.str;
default = "nextcloud";
description = lib.mdDoc ''
The nextcloud database to copy
'';
};
db_user = lib.mkOption {
type = lib.types.str;
default = "nextcloud";
description = lib.mdDoc ''
The nextcloud database user to use for backups.
'';
};
db_passfile = lib.mkOption {
type = lib.types.path;
description = lib.mdDoc ''
The filepath to the nextcloud database user password.
'';
};
};
};
config = lib.mkIf cfg.enable {
systemd.services.alaska_backup_script_onfail = {
description = "Clean up permissions on script fail";
path = with pkgs; [
config.services.nextcloud.occ
umount
];
script = ''
nextcloud-occ maintenance:mode --off
echo "Unmounting the external drive"
umount ${cfg.tmp_mount_point}
'';
};
systemd.services.alaska_backup_script = {
description = "Alaska Nightly Backup Service";
onFailure = [ "alaska_backup_script_onfail.service" ];
path = with pkgs; [
config.services.nextcloud.occ
config.services.postgresql.package
rsync
mount
umount
];
script = ''
# ${pkgs.lib.makeBinPath [ pkgs.screen ]}/screen -dmS AlaskaBackupJob \
# ${pkgs.lib.makeBinPath [ pkgs.bash ]}/bash -c "
echo "Created the temporary mount point if it does not exit"
mkdir -p ${builtins.toString cfg.tmp_mount_point}
echo "Mounting the external backup drive"
mount /dev/disk/by-label/${cfg.backup1_drive_label} ${builtins.toString cfg.tmp_mount_point}
echo "Puttin nextcloud into maintenance mode so that changes cannot happen during the backup"
nextcloud-occ maintenance:mode --on
echo "Backing up the nextcloud database"
password='cat ${builtins.toString cfg.nextcloud.db_passfile}'
PGPASSWORD="$password" pg_dump \
${builtins.toString cfg.nextcloud.db_name} -h ${builtins.toString cfg.nextcloud.db_server} \
-U ${builtins.toString cfg.nextcloud.db_user} \
-f ${builtins.toString cfg.tmp_mount_point}/nextcloud-sqlbkp_`date +"%Y%m%d"`.bak
echo "Backing up the nextcloud files"
# -a archive | -v verbose
rsync -av ${builtins.toString cfg.nextcloud.root_dir} \
${builtins.toString cfg.tmp_mount_point}/nextcloud
echo "Get nextcloud out of maintenance mode so that normal operations can resume"
nextcloud-occ maintenance:mode --off
echo "Unmounting the external drive"
umount ${cfg.tmp_mount_point}
echo "Job completed"
# "
'';
startAt = "Sun 14:00:00";
# serviceConfig = {
# Type = "oneshot";
# };
};
# config.systemd.timers.alaska_backup_script = lib.mkIf cfg.run_nightly {
# wantedBy = [ "timers.target" ];
# partOf = [ "alaska_backup_script.service" ];
# timerConfig.OnCalendar
# };
};
}