2024-03-02 02:26:38 +01:00
{ config , pkgs , lib , . . . }:
2024-02-17 02:42:35 +01:00
2024-02-20 04:41:48 +01:00
# 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.
2024-02-17 02:42:35 +01:00
let
2024-03-02 02:26:38 +01:00
makeBackupForRepo = repo : lib . getExe ( pkgs . writeShellScriptBin " f o r g e j o - b o r g b a c k u p " ''
2024-02-17 02:42:35 +01:00
#!/bin/sh
set - e
#stop forgejo
systemctl stop forgejo . service
2024-02-20 04:41:48 +01:00
# Dump Forgejo DB
2024-02-17 02:42:35 +01:00
MYSQL_DATABASE = " f o r g e j o d b "
2024-02-17 05:44:04 +01:00
$ { pkgs . mariadb } /bin/mysqldump - u root '' ${ MYSQL_DATABASE } > / b o r g b a c k u p c a c h e / f o r g e j o b a c k u p . s q l
2024-02-17 02:42:35 +01:00
# BorgBackup
2024-02-17 05:44:04 +01:00
export BORG_PASSCOMMAND = " c a t / e t c / n i x o s / b o r g p a s s w o r d "
2024-03-02 02:26:38 +01:00
export BORG_REPO = $ { repo }
2024-02-20 04:41:48 +01:00
export BACKUP_NAME = " f o r g e j o - $ ( d a t e + % Y - % m - % d - % H - % M ) "
2024-02-17 02:42:35 +01:00
# Add everything to be backed up
$ { pkgs . borgbackup } /bin/borg create - - verbose - - filter AME - - list - - stats - - show-rc - - compression lz4 - - exclude-caches \
$ BORG_REPO : : $ BACKUP_NAME \
/var/lib/forgejo/repositories / \
/var/lib/forgejo/data / \
2024-03-02 02:26:38 +01:00
/borgbackupcache/forgejobackup.sql \
/etc/nixos /
2024-02-20 04:41:48 +01:00
# Delete DB dump
2024-02-17 02:42:35 +01:00
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
2024-03-02 02:26:38 +01:00
'' ) ;
repos = repolistfile : lib . pipe repolistfile [
builtins . readFile
( lib . splitString " \n " )
( lib . filter ( s : s != " " ) )
] ;
backups = repolistfile : builtins . map makeBackupForRepo ( repos repolistfile ) ;
2024-02-17 03:11:14 +01:00
in
{
environment . systemPackages = [ pkgs . borgbackup ] ;
# Create folders
systemd . tmpfiles . rules = [
" d / b o r g b a c k u p c a c h e 7 0 0 r o o t r o o t "
] ;
# Backup timer
systemd . services . borg-backup = {
2024-02-20 04:41:48 +01:00
description = " B o r g B a c k u p f o r F o r g e j o a n d t h e F o r g e j o M y S Q L D a t a b a s e " ;
2024-02-17 03:11:14 +01:00
serviceConfig = {
2024-03-02 02:26:38 +01:00
ExecStart = lib . getExe ( pkgs . writeShellScriptBin " d o B a c k u p s " ( lib . concatStringsSep " ; " ( backups config . _module . args . borgrepolistfile ) ) ) ;
2024-02-17 03:11:14 +01:00
User = " r o o t " ;
} ;
2024-02-20 22:15:27 +01:00
requires = [ " m y s q l . s e r v i c e " ] ;
after = [ " f o r g e j o . s e r v i c e " " m y s q l . s e r v i c e " " n e t w o r k - o n l i n e . t a r g e t " ] ;
wants = [ " n e t w o r k - o n l i n e . t a r g e t " ] ;
2024-02-17 03:11:14 +01:00
wantedBy = [ " m u l t i - u s e r . t a r g e t " ] ;
} ;
systemd . timers . borg-backup = {
description = " D a i l y B o r g B a c k u p T i m e r " ;
wantedBy = [ " t i m e r s . t a r g e t " ] ;
timerConfig = {
2024-02-20 22:15:27 +01:00
OnActiveSec = " 3 0 s " ;
2024-02-17 03:11:14 +01:00
OnCalendar = " d a i l y " ;
Persistent = true ;
} ;
} ;
2024-02-17 05:44:04 +01:00
}