backup script improved with multi-repo-support
This commit is contained in:
parent
7ea007fb49
commit
e1a8358c14
2 changed files with 15 additions and 6 deletions
19
backup.nix
19
backup.nix
|
@ -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.
|
# 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
|
let
|
||||||
forgejo-borgbackup = pkgs.writeShellScriptBin "forgejo-borgbackup" ''
|
makeBackupForRepo = repo: lib.getExe (pkgs.writeShellScriptBin "forgejo-borgbackup" ''
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -e
|
set -e
|
||||||
#stop forgejo
|
#stop forgejo
|
||||||
|
@ -12,7 +12,7 @@ let
|
||||||
${pkgs.mariadb}/bin/mysqldump -u root ''${MYSQL_DATABASE} > /borgbackupcache/forgejobackup.sql
|
${pkgs.mariadb}/bin/mysqldump -u root ''${MYSQL_DATABASE} > /borgbackupcache/forgejobackup.sql
|
||||||
# BorgBackup
|
# BorgBackup
|
||||||
export BORG_PASSCOMMAND="cat /etc/nixos/borgpassword"
|
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)"
|
export BACKUP_NAME="forgejo-$(date +%Y-%m-%d-%H-%M)"
|
||||||
|
|
||||||
# Add everything to be backed up
|
# Add everything to be backed up
|
||||||
|
@ -20,14 +20,21 @@ let
|
||||||
$BORG_REPO::$BACKUP_NAME \
|
$BORG_REPO::$BACKUP_NAME \
|
||||||
/var/lib/forgejo/repositories/ \
|
/var/lib/forgejo/repositories/ \
|
||||||
/var/lib/forgejo/data/ \
|
/var/lib/forgejo/data/ \
|
||||||
/borgbackupcache/forgejobackup.sql
|
/borgbackupcache/forgejobackup.sql \
|
||||||
|
/etc/nixos/
|
||||||
# Delete DB dump
|
# Delete DB dump
|
||||||
rm /borgbackupcache/forgejobackup.sql
|
rm /borgbackupcache/forgejobackup.sql
|
||||||
# Start Forgejo again
|
# Start Forgejo again
|
||||||
systemctl start forgejo.service
|
systemctl start forgejo.service
|
||||||
# Prune old backups
|
# Prune old backups
|
||||||
${pkgs.borgbackup}/bin/borg prune --list $BORG_REPO --prefix 'forgejo-' --show-rc --keep-daily=7 --keep-weekly=4 --keep-monthly=6
|
${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
|
in
|
||||||
{
|
{
|
||||||
environment.systemPackages = [ pkgs.borgbackup ];
|
environment.systemPackages = [ pkgs.borgbackup ];
|
||||||
|
@ -39,7 +46,7 @@ let
|
||||||
systemd.services.borg-backup = {
|
systemd.services.borg-backup = {
|
||||||
description = "Borg Backup for Forgejo and the Forgejo MySQL Database";
|
description = "Borg Backup for Forgejo and the Forgejo MySQL Database";
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
ExecStart = "${forgejo-borgbackup}/bin/forgejo-borgbackup";
|
ExecStart = lib.getExe (pkgs.writeShellScriptBin "doBackups" (lib.concatStringsSep ";" (backups config._module.args.borgrepolistfile)));
|
||||||
User = "root";
|
User = "root";
|
||||||
};
|
};
|
||||||
requires= ["mysql.service"];
|
requires= ["mysql.service"];
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
# comment in backup.nix for borgbackuping forgejo
|
# comment in backup.nix for borgbackuping forgejo
|
||||||
./backup.nix
|
./backup.nix
|
||||||
];
|
];
|
||||||
|
# Write path for borgbackup repos for backup.nix
|
||||||
|
_module.args.borgrepolistfile = ./borgrepos;
|
||||||
|
|
||||||
# Enable Flakes and the new command-line tool
|
# Enable Flakes and the new command-line tool
|
||||||
nix.settings.experimental-features = [ "nix-command" "flakes" ];
|
nix.settings.experimental-features = [ "nix-command" "flakes" ];
|
||||||
|
|
Loading…
Reference in a new issue