update borgmatic config to backup btrfs subvolume
This commit is contained in:
parent
5884585b3a
commit
fc2da07613
1 changed files with 32 additions and 35 deletions
|
@ -1,39 +1,28 @@
|
||||||
{ pkgs, lib, config, ... }:
|
{ pkgs, lib, config, ... }:
|
||||||
let
|
let
|
||||||
cfg = config.lab.backups;
|
cfg = config.lab.backups;
|
||||||
snapshotFile = "/tmp/snapshot.qcow2";
|
|
||||||
snapshotMount = "/tmp/snapshot";
|
|
||||||
beforeEverything = pkgs.writeShellScriptBin "beforeEverything" ''
|
beforeEverything = pkgs.writeShellScriptBin "beforeEverything" ''
|
||||||
${pkgs.libvirt}/bin/virsh snapshot-create-as --domain ${cfg.domainName} --name backup-${cfg.domainName} --disk-only --quiesce --no-metadata --diskspec vda,snapshot=no --diskspec vdb,file=${snapshotFile} && ${pkgs.coreutils}/bin/sleep 1
|
if [ -d "${cfg.snapshotLocation}" ]; then
|
||||||
${pkgs.coreutils}/bin/mkdir -p ${snapshotMount}
|
${pkgs.btrfs-progs}/bin/btrfs subvolume delete ${cfg.snapshotLocation}
|
||||||
${pkgs.libguestfs-with-appliance}/bin/guestmount -a ${snapshotFile} -m /dev/sda1 --ro ${snapshotMount}
|
fi
|
||||||
'';
|
|
||||||
|
|
||||||
afterEverything = pkgs.writeShellScriptBin "afterEverything" ''
|
${pkgs.btrfs-progs}/bin/btrfs subvolume snapshot -r ${cfg.subvolumeLocation} ${cfg.snapshotLocation}
|
||||||
set +e
|
|
||||||
${pkgs.coreutils}/bin/sleep 10
|
|
||||||
${pkgs.libguestfs-with-appliance}/bin/guestunmount ${snapshotMount} && ${pkgs.coreutils}/bin/sleep 1
|
|
||||||
${pkgs.coreutils}/bin/rm -rf ${snapshotMount}
|
|
||||||
${pkgs.libvirt}/bin/virsh blockcommit ${cfg.domainName} vdb --active --verbose --pivot
|
|
||||||
${pkgs.coreutils}/bin/rm -f ${snapshotFile}
|
|
||||||
'';
|
'';
|
||||||
|
|
||||||
borgmaticConfig = pkgs.writeTextFile {
|
borgmaticConfig = pkgs.writeTextFile {
|
||||||
name = "borgmatic-config";
|
name = "borgmatic-config";
|
||||||
text = ''
|
text = ''
|
||||||
source_directories:
|
source_directories:
|
||||||
- ${snapshotMount}
|
- ${cfg.snapshotLocation}
|
||||||
repositories:
|
repositories:
|
||||||
- path: ${cfg.repoLocation}
|
- path: ${cfg.repoLocation}
|
||||||
label: ${cfg.domainName}
|
label: nfs
|
||||||
keep_daily: 7
|
keep_daily: 7
|
||||||
keep_weekly: 4
|
keep_weekly: 4
|
||||||
keep_monthly: 6
|
keep_monthly: 6
|
||||||
unknown_unencrypted_repo_access_is_ok: true
|
unknown_unencrypted_repo_access_is_ok: true
|
||||||
before_everything:
|
before_everything:
|
||||||
- ${beforeEverything}/bin/beforeEverything
|
- ${beforeEverything}/bin/beforeEverything
|
||||||
after_everything:
|
|
||||||
- ${afterEverything}/bin/afterEverything
|
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
in
|
in
|
||||||
|
@ -48,33 +37,44 @@ in
|
||||||
};
|
};
|
||||||
|
|
||||||
repoLocation = lib.mkOption {
|
repoLocation = lib.mkOption {
|
||||||
default = "${config.lab.dataDisk.mountPoint}/backups/thecloud-data.borg";
|
# TODO: maybe make sure data disk is enabled? is there an "ensure" method in nix?
|
||||||
|
default = "${config.lab.dataDisk.mountPoint}/backups/nfs.borg";
|
||||||
type = lib.types.str;
|
type = lib.types.str;
|
||||||
description = ''
|
description = ''
|
||||||
Location of the Borg repository to back up to.
|
Location of the Borg repository to back up to.
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
domainName = lib.mkOption {
|
subvolumeLocation = lib.mkOption {
|
||||||
default = "thecloud";
|
default = "${config.lab.dataDisk.mountPoint}/nfs";
|
||||||
type = lib.types.str;
|
type = lib.types.str;
|
||||||
description = ''
|
description = ''
|
||||||
The name of the Libvirt domain with the data disk attached.
|
Location of the btrfs subvolume holding the data.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
snapshotLocation = lib.mkOption {
|
||||||
|
default = "${config.lab.dataDisk.mountPoint}/nfs-backup";
|
||||||
|
type = lib.types.str;
|
||||||
|
description = ''
|
||||||
|
Location to (temporary) create a snapshot of the subvolume.
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
config = lib.mkIf cfg.enable {
|
config = lib.mkIf cfg.enable {
|
||||||
environment.systemPackages = with pkgs; [ libguestfs-with-appliance borgbackup ];
|
environment.systemPackages = with pkgs; [ borgbackup ];
|
||||||
# Converted from:
|
# Converted from:
|
||||||
# https://github.com/borgmatic-collective/borgmatic/tree/84823dfb912db650936e3492f6ead7e0e0d32a0f/sample/systemd
|
# https://github.com/borgmatic-collective/borgmatic/tree/84823dfb912db650936e3492f6ead7e0e0d32a0f/sample/systemd
|
||||||
systemd.services.borgmatic = {
|
systemd.services.borgmatic = {
|
||||||
description = "borgmatic backup";
|
description = "borgmatic backup";
|
||||||
wants = [ "network-online.target" ];
|
wants = [ "network-online.target" ];
|
||||||
after = [ "network-online.target" ];
|
after = [ "network-online.target" ];
|
||||||
|
|
||||||
unitConfig = {
|
unitConfig = {
|
||||||
ConditionACPower = true;
|
ConditionACPower = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "oneshot";
|
Type = "oneshot";
|
||||||
Nice = 19;
|
Nice = 19;
|
||||||
|
@ -85,22 +85,19 @@ in
|
||||||
Restart = "no";
|
Restart = "no";
|
||||||
LogRateLimitIntervalSec = 0;
|
LogRateLimitIntervalSec = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
preStart = "${pkgs.coreutils}/bin/sleep 1m";
|
preStart = "${pkgs.coreutils}/bin/sleep 1m";
|
||||||
script = "${pkgs.systemd}/bin/systemd-inhibit --who=\"borgmatic\" --what=\"sleep:shutdown\" --why=\"Prevent interrupting scheduled backup\" ${pkgs.borgmatic}/bin/borgmatic --verbosity -2 --syslog-verbosity 1";
|
script = "${pkgs.systemd}/bin/systemd-inhibit --who=\"borgmatic\" --what=\"sleep:shutdown\" --why=\"Prevent interrupting scheduled backup\" ${pkgs.borgmatic}/bin/borgmatic --verbosity -2 --syslog-verbosity 1 -c ${borgmaticConfig}";
|
||||||
};
|
};
|
||||||
|
|
||||||
environment.etc."borgmatic/config.yaml" = {
|
systemd.timers.borgmatic = {
|
||||||
source = borgmaticConfig;
|
description = "Run borgmatic backup";
|
||||||
|
wantedBy = [ "timers.target" ];
|
||||||
|
timerConfig = {
|
||||||
|
OnCalendar = "*-*-* 3:00:00";
|
||||||
|
Persistent = true;
|
||||||
|
RandomizedDelaySec = "3h";
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
# systemd.timers.borgmatic = {
|
|
||||||
# description = "Run borgmatic backup";
|
|
||||||
# wantedBy = [ "timers.target" ];
|
|
||||||
# timerConfig = {
|
|
||||||
# OnCalendar = "*-*-* 3:00:00";
|
|
||||||
# Persistent = true;
|
|
||||||
# RandomizedDelaySec = "3h";
|
|
||||||
# };
|
|
||||||
# };
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue