2020-06-20 17:54:21 +08:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
makeBackup = pkgs.writeScript "make-backup" ''
|
|
|
|
#!${pkgs.bash}/bin/bash
|
|
|
|
|
|
|
|
set -e
|
|
|
|
umask 0077
|
|
|
|
|
2020-07-27 20:28:30 +08:00
|
|
|
FILENAME=backup-`date +%F`.tar.bz2.gpg
|
|
|
|
|
2020-06-20 17:54:21 +08:00
|
|
|
DBDUMPDIR=`mktemp -d`
|
|
|
|
pushd $DBDUMPDIR
|
|
|
|
|
|
|
|
${config.services.mysql.package}/bin/mysqldump --single-transaction flarum > flarum.sql
|
|
|
|
${pkgs.sudo}/bin/sudo -u mattermost ${config.services.postgresql.package}/bin/pg_dump mattermost > mattermost.sql
|
2021-11-25 00:15:09 +08:00
|
|
|
${pkgs.sudo}/bin/sudo -u rt ${config.services.postgresql.package}/bin/pg_dump rt5 > rt.sql
|
2020-06-20 17:54:21 +08:00
|
|
|
|
2022-06-27 18:15:47 +08:00
|
|
|
${pkgs.gnutar}/bin/tar cf - --exclude "/var/lib/gitea/repositories/*/*.git/archives" --exclude "/var/lib/gitea/data/repo-archive" --exclude "/var/lib/afws/.cache" /etc/nixos /var/vmail /var/lib/gitea /var/lib/afws /var/lib/mattermost/data flarum.sql mattermost.sql rt.sql | \
|
2020-06-20 17:54:21 +08:00
|
|
|
${pkgs.bzip2}/bin/bzip2 | \
|
2021-11-16 14:21:56 +08:00
|
|
|
${pkgs.gnupg}/bin/gpg --symmetric --batch --passphrase-file /etc/nixos/secret/backup-passphrase > /home/backupdl/$FILENAME
|
2020-06-20 17:54:21 +08:00
|
|
|
|
|
|
|
popd
|
|
|
|
rm -rf $DBDUMPDIR
|
|
|
|
|
2021-11-16 14:21:56 +08:00
|
|
|
chown backupdl.users /home/backupdl/$FILENAME
|
|
|
|
|
2020-06-20 17:54:21 +08:00
|
|
|
echo Backup done
|
|
|
|
'';
|
|
|
|
cfg = config.services.mlabs-backup;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.mlabs-backup = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Enable backups";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.mlabs-backup = {
|
|
|
|
description = "M-Labs backup";
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
User = "root";
|
|
|
|
Group = "root";
|
|
|
|
ExecStart = "${makeBackup}";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.timers.mlabs-backup = {
|
|
|
|
description = "M-Labs backup";
|
|
|
|
wantedBy = [ "timers.target" ];
|
2022-06-27 18:15:57 +08:00
|
|
|
timerConfig.OnCalendar = "tuesday,friday *-*-* 08:00:00";
|
2020-06-20 17:54:21 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|