backup script improved with multi-repo-support

This commit is contained in:
Lyn 2024-03-02 02:26:38 +01:00
parent 7ea007fb49
commit e1a8358c14
2 changed files with 15 additions and 6 deletions

View file

@ -1,8 +1,8 @@
{config, pkgs, ... }:
{config, pkgs, lib, ... }:
# NOTE: For this to work you should use MariaDB as your Forgejo-Database running on the same host. If this is not the case, update this script accordingly.
let
forgejo-borgbackup = pkgs.writeShellScriptBin "forgejo-borgbackup" ''
makeBackupForRepo = repo: lib.getExe (pkgs.writeShellScriptBin "forgejo-borgbackup" ''
#!/bin/sh
set -e
#stop forgejo
@ -12,7 +12,7 @@ let
${pkgs.mariadb}/bin/mysqldump -u root ''${MYSQL_DATABASE} > /borgbackupcache/forgejobackup.sql
# BorgBackup
export BORG_PASSCOMMAND="cat /etc/nixos/borgpassword"
export BORG_REPO=$(cat /etc/nixos/borgrepo)
export BORG_REPO=${repo}
export BACKUP_NAME="forgejo-$(date +%Y-%m-%d-%H-%M)"
# Add everything to be backed up
@ -20,14 +20,21 @@ let
$BORG_REPO::$BACKUP_NAME \
/var/lib/forgejo/repositories/ \
/var/lib/forgejo/data/ \
/borgbackupcache/forgejobackup.sql
/borgbackupcache/forgejobackup.sql \
/etc/nixos/
# Delete DB dump
rm /borgbackupcache/forgejobackup.sql
# Start Forgejo again
systemctl start forgejo.service
# Prune old backups
${pkgs.borgbackup}/bin/borg prune --list $BORG_REPO --prefix 'forgejo-' --show-rc --keep-daily=7 --keep-weekly=4 --keep-monthly=6
'';
'');
repos = repolistfile: lib.pipe repolistfile [
builtins.readFile
(lib.splitString "\n")
(lib.filter (s: s != ""))
];
backups = repolistfile: builtins.map makeBackupForRepo (repos repolistfile);
in
{
environment.systemPackages = [ pkgs.borgbackup ];
@ -39,7 +46,7 @@ let
systemd.services.borg-backup = {
description = "Borg Backup for Forgejo and the Forgejo MySQL Database";
serviceConfig = {
ExecStart = "${forgejo-borgbackup}/bin/forgejo-borgbackup";
ExecStart = lib.getExe (pkgs.writeShellScriptBin "doBackups" (lib.concatStringsSep ";" (backups config._module.args.borgrepolistfile)));
User = "root";
};
requires= ["mysql.service"];

View file

@ -11,6 +11,8 @@
# comment in backup.nix for borgbackuping forgejo
./backup.nix
];
# Write path for borgbackup repos for backup.nix
_module.args.borgrepolistfile = ./borgrepos;
# Enable Flakes and the new command-line tool
nix.settings.experimental-features = [ "nix-command" "flakes" ];