Compare commits
2 Commits
c12a47cee0
...
087753eb1e
Author | SHA1 | Date |
---|---|---|
Oystein Kristoffer Tveit | 087753eb1e | |
Oystein Kristoffer Tveit | 6efebc5cb7 |
|
@ -1,4 +1,7 @@
|
||||||
{ pkgs, lib, config, values, ... }:
|
{ pkgs, lib, config, values, ... }:
|
||||||
|
let
|
||||||
|
backupDir = "/var/lib/mysql/backups";
|
||||||
|
in
|
||||||
{
|
{
|
||||||
sops.secrets."mysql/password" = {
|
sops.secrets."mysql/password" = {
|
||||||
owner = "mysql";
|
owner = "mysql";
|
||||||
|
@ -36,11 +39,6 @@
|
||||||
}];
|
}];
|
||||||
};
|
};
|
||||||
|
|
||||||
services.mysqlBackup = {
|
|
||||||
enable = true;
|
|
||||||
location = "/var/lib/mysql/backups";
|
|
||||||
};
|
|
||||||
|
|
||||||
networking.firewall.allowedTCPPorts = [ 3306 ];
|
networking.firewall.allowedTCPPorts = [ 3306 ];
|
||||||
|
|
||||||
systemd.services.mysql.serviceConfig = {
|
systemd.services.mysql.serviceConfig = {
|
||||||
|
@ -50,4 +48,51 @@
|
||||||
values.ipv6-space
|
values.ipv6-space
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# NOTE: instead of having the upstream nixpkgs postgres backup unit trigger
|
||||||
|
# another unit, it was easier to just make one ourselves
|
||||||
|
systemd.services."backup-mysql" = {
|
||||||
|
description = "Backup MySQL data";
|
||||||
|
requires = [ "mysql.service" ];
|
||||||
|
|
||||||
|
path = [
|
||||||
|
pkgs.coreutils
|
||||||
|
pkgs.rsync
|
||||||
|
pkgs.gzip
|
||||||
|
config.services.mysql.package
|
||||||
|
];
|
||||||
|
|
||||||
|
script = let
|
||||||
|
rotations = 10;
|
||||||
|
sshTarget1 = "root@isvegg.pvv.ntnu.no:/mnt/backup1/bicep/mysql";
|
||||||
|
sshTarget2 = "root@isvegg.pvv.ntnu.no:/mnt/backup2/bicep/mysql";
|
||||||
|
in ''
|
||||||
|
set -eo pipefail
|
||||||
|
|
||||||
|
mysqldump | gzip -c -9 --rsyncable > "${backupDir}/$(date --iso-8601)-dump.sql.gz"
|
||||||
|
|
||||||
|
while [ $(ls -1 "${backupDir}" | wc -l) -gt ${toString rotations} ]; do
|
||||||
|
rm $(find "${backupDir}" -type f -printf '%T+ %p\n' | sort | head -n 1 | cut -d' ' -f2)
|
||||||
|
done
|
||||||
|
|
||||||
|
rsync -avz --delete "${backupDir}" '${sshTarget1}'
|
||||||
|
rsync -avz --delete "${backupDir}" '${sshTarget2}'
|
||||||
|
'';
|
||||||
|
|
||||||
|
serviceConfig = {
|
||||||
|
Type = "oneshot";
|
||||||
|
User = "mysql";
|
||||||
|
Group = "mysql";
|
||||||
|
UMask = "0077";
|
||||||
|
ReadWritePaths = [ backupDir ];
|
||||||
|
};
|
||||||
|
|
||||||
|
startAt = "*-*-* 02:15:00";
|
||||||
|
};
|
||||||
|
|
||||||
|
systemd.tmpfiles.settings."10-mysql-backup".${backupDir}.d = {
|
||||||
|
user = "mysql";
|
||||||
|
group = "mysql";
|
||||||
|
mode = "700";
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
{ config, pkgs, ... }:
|
{ config, pkgs, lib, ... }:
|
||||||
let
|
let
|
||||||
sslCert = config.security.acme.certs."postgres.pvv.ntnu.no";
|
sslCert = config.security.acme.certs."postgres.pvv.ntnu.no";
|
||||||
|
backupDir = "/var/lib/postgresql/backups";
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
services.postgresql = {
|
services.postgresql = {
|
||||||
|
@ -89,9 +90,50 @@ in
|
||||||
networking.firewall.allowedTCPPorts = [ 5432 ];
|
networking.firewall.allowedTCPPorts = [ 5432 ];
|
||||||
networking.firewall.allowedUDPPorts = [ 5432 ];
|
networking.firewall.allowedUDPPorts = [ 5432 ];
|
||||||
|
|
||||||
services.postgresqlBackup = {
|
# NOTE: instead of having the upstream nixpkgs postgres backup unit trigger
|
||||||
enable = true;
|
# another unit, it was easier to just make one ourselves
|
||||||
location = "/var/lib/postgres/backups";
|
systemd.services."backup-postgresql" = {
|
||||||
backupAll = true;
|
description = "Backup PostgreSQL data";
|
||||||
|
requires = [ "postgresql.service" ];
|
||||||
|
|
||||||
|
path = [
|
||||||
|
pkgs.coreutils
|
||||||
|
pkgs.rsync
|
||||||
|
pkgs.gzip
|
||||||
|
config.services.postgresql.package
|
||||||
|
];
|
||||||
|
|
||||||
|
script = let
|
||||||
|
rotations = 10;
|
||||||
|
sshTarget1 = "root@isvegg.pvv.ntnu.no:/mnt/backup1/bicep/postgresql";
|
||||||
|
sshTarget2 = "root@isvegg.pvv.ntnu.no:/mnt/backup2/bicep/postgresql";
|
||||||
|
in ''
|
||||||
|
set -eo pipefail
|
||||||
|
|
||||||
|
pg_dumpall -U postgres | gzip -c -9 --rsyncable > "${backupDir}/$(date --iso-8601)-dump.sql.gz"
|
||||||
|
|
||||||
|
while [ $(ls -1 "${backupDir}" | wc -l) -gt ${toString rotations} ]; do
|
||||||
|
rm $(find "${backupDir}" -type f -printf '%T+ %p\n' | sort | head -n 1 | cut -d' ' -f2)
|
||||||
|
done
|
||||||
|
|
||||||
|
rsync -avz --delete "${backupDir}" '${sshTarget1}'
|
||||||
|
rsync -avz --delete "${backupDir}" '${sshTarget2}'
|
||||||
|
'';
|
||||||
|
|
||||||
|
serviceConfig = {
|
||||||
|
Type = "oneshot";
|
||||||
|
User = "postgres";
|
||||||
|
Group = "postgres";
|
||||||
|
UMask = "0077";
|
||||||
|
ReadWritePaths = [ backupDir ];
|
||||||
|
};
|
||||||
|
|
||||||
|
startAt = "*-*-* 01:15:00";
|
||||||
|
};
|
||||||
|
|
||||||
|
systemd.tmpfiles.settings."10-postgresql-backup".${backupDir}.d = {
|
||||||
|
user = "postgres";
|
||||||
|
group = "postgres";
|
||||||
|
mode = "700";
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue