Database Backup Contract
This NixOS contract represents a backup job that will backup everything in one database on a regular schedule.
It is a contract between a service that has database dumps to be backed up and a service that backs up databases dumps.
Contract Reference
These are all the options that are expected to exist for this contract to be respected.
-
shb.contracts.databasebackup -
Contract for database backup between a requester module and a provider module.
The requester communicates to the provider how to backup the database through the
requestoptions.The provider reads from the
requestoptions and backs up the database as requested. It communicates to the requester what script is used to backup and restore the database through theresultoptions.Type: submodule
Declared by:
<selfhostblocks/modules/contracts/databasebackup/dummyModule.nix> -
shb.contracts.databasebackup.request -
Request part of the database backup contract.
Options set by the requester module enforcing how to backup files.
Type: submodule
Default:
""Declared by:
<selfhostblocks/modules/contracts/databasebackup/dummyModule.nix> -
shb.contracts.databasebackup.request.backupCmd -
Command that produces the database dump on stdout.
Type: string
Default:
""Example:
${pkgs.postgresql}/bin/pg_dumpall | ${pkgs.gzip}/bin/gzip --rsyncableDeclared by:
<selfhostblocks/modules/contracts/databasebackup/dummyModule.nix> -
shb.contracts.databasebackup.request.backupName -
Name of the backup in the repository.
Type: string
Default:
"dump"Example:
"postgresql.sql"Declared by:
<selfhostblocks/modules/contracts/databasebackup/dummyModule.nix> -
shb.contracts.databasebackup.request.restoreCmd -
Command that reads the database dump on stdin and restores the database.
Type: string
Default:
""Example:
${pkgs.gzip}/bin/gunzip | ${pkgs.postgresql}/bin/psql postgresDeclared by:
<selfhostblocks/modules/contracts/databasebackup/dummyModule.nix> -
shb.contracts.databasebackup.request.user -
Unix user doing the backups.
This should be an admin user having access to all databases.
Type: string
Default:
"root"Example:
"postgres"Declared by:
<selfhostblocks/modules/contracts/databasebackup/dummyModule.nix> -
shb.contracts.databasebackup.result -
Result part of the database backup contract.
Options set by the provider module that indicates the name of the backup and restore scripts.
Type: submodule
Default:
""Declared by:
<selfhostblocks/modules/contracts/databasebackup/dummyModule.nix> -
shb.contracts.databasebackup.result.backupService -
Name of service backing up the database.
This script can be ran manually to backup the database:
$ systemctl start backup.serviceType: string
Default:
"backup.service"Declared by:
<selfhostblocks/modules/contracts/databasebackup/dummyModule.nix> -
shb.contracts.databasebackup.result.restoreScript -
Name of script that can restore the database. One can then list snapshots with:
$ restore snapshotsAnd restore the database with:
$ restore restore latestType: string
Default:
"restore"Declared by:
<selfhostblocks/modules/contracts/databasebackup/dummyModule.nix> -
shb.contracts.databasebackup.settings -
Optional attribute set with options specific to the provider.
Type: anything
Declared by:
<selfhostblocks/modules/contracts/databasebackup/dummyModule.nix>
Usage
A database that can be backed up will provide a databasebackup option.
What this option defines is, from the user perspective - that is you - an implementation detail but it will at least define how to create a database dump, the user to backup with and how to restore from a database dump.
Here is an example module defining such a databasebackup option,
which defines the user to backup with (user)
and how to backup (backupCmd) and restore (restoreCmd) the database:
{
options = {
myservice.databasebackup = mkOption {
type = shb.contracts.databasebackup.mkRequester = {
user = "myservice";
backupCmd = ''
${pkgs.postgresql}/bin/pg_dumpall | ${pkgs.gzip}/bin/gzip --rsyncable
'';
restoreCmd = ''
${pkgs.gzip}/bin/gunzip | ${pkgs.postgresql}/bin/psql postgres
'';
};
};
};
};
Now, on the other side we have a service that uses this databasebackup option and actually backs up files.
This service is a provider of this contract and will provide a result option.
Let’s assume such a module is available under the databaseBackupService option
and that one can create multiple backup instances under databaseBackupService.instances.
Then, to actually backup the myservice service, one would write:
databaseBackupService.instances.myservice = {
request = myservice.databasebackup.request;
settings = {
enable = true;
repository = {
path = "/srv/backup/myservice";
};
# ... Other options specific to backupservice like scheduling.
};
};
It is advised to backup files to different location, to improve redundancy.
Thanks to using contracts, this can be made easily either with the same databaseBackupService:
databaseBackupService.instances.myservice_2 = {
request = myservice.databasebackup.request;
settings = {
enable = true;
repository = {
path = "<remote path>";
};
};
};
Or with another module databaseBackupService_2!