feat: make paperless have only one master node to enable syncing

This commit is contained in:
Benjamin Bädorf 2023-08-29 13:56:18 +02:00
parent 0f430a625e
commit 2b2e3c4e4e
No known key found for this signature in database
GPG key ID: 4406E80E13CD656C

View file

@ -32,7 +32,7 @@ in {
description = "Directory to save data in"; description = "Directory to save data in";
type = types.str; type = types.str;
example = "/home/pub_solar/Paperless"; example = "/home/pub_solar/Paperless";
default = "/home/${psCfg.user.name}/Paperless"; default = "${xdg.dataHome}/Paperless";
}; };
consumptionDir = mkOption { consumptionDir = mkOption {
@ -42,6 +42,23 @@ in {
default = "/var/lib/paperless/consume"; default = "/var/lib/paperless/consume";
}; };
sync = {
enable = mkEnableOption ''
You can use this option to sync several paperless instances, for example via nextcloud.
It will sync the media directory and database, automatically merging sqlite dbs via dump and import.
Logs, the classification model, and other files are left unsynced.
'';
masterNode = mkEnableOption "If this node is the master node, it will only export paperless data, otherwise it will only import";
directory = mkOption {
description = "Directory to sync with.";
type = types.str;
example = "/home/pub_solar/Nextcloud/Paperless";
default = "/home/${psCfg.user.name}/Paperless";
};
};
scannerDefaultDevice = mkOption { scannerDefaultDevice = mkOption {
description = '' description = ''
The scanner device. To find this, use `scanimage -L`. The scanner device. To find this, use `scanimage -L`.
@ -91,5 +108,41 @@ in {
SCANNER_OUTPUT_DIR = cfg.consumptionDir; SCANNER_OUTPUT_DIR = cfg.consumptionDir;
}; };
}; };
systemd = let
copy-in = pkgs.writeShellScriptBin "copy-in" ''
${pkgs.systemd}/bin/systemctl stop paperless-web.service paperless-task-queue.service paperless-scheduler.service paperless-consumer.service
cp -r ${cfg.sync.directory}/media ${cfg.dataDir}/media
cp ${cfg.sync.directory}/db.sqlite3 ${cfg.dataDir}/db.sqlite3
${pkgs.systemd}/bin/systemctl start paperless-web.service paperless-task-queue.service paperless-scheduler.service paperless-consumer.service
'';
copy-out = pkgs.writeShellScriptBin "copy-out" ''
${pkgs.systemd}/bin/systemctl stop paperless-web.service paperless-task-queue.service paperless-scheduler.service paperless-consumer.service
cp -r ${cfg.dataDir}/media ${cfg.sync.directory}/media
cp ${cfg.dataDir}/db.sqlite3 ${cfg.sync.directory}/db.sqlite3
${pkgs.systemd}/bin/systemctl start paperless-web.service paperless-task-queue.service paperless-scheduler.service paperless-consumer.service
'';
in mkIf cfg.sync.enable {
services.nextcloud-paperless-autosync = {
unitConfig = {
Description = "Auto sync paperless to or from Nextcloud";
After = "network-online.target";
};
serviceConfig = {
Type = "simple";
ExecStart= if cfg.sync.masterNode then "${copy-out}/bin/copy-out" else "${copy-in}/bin/copy-in";
TimeoutStopSec = "180";
KillMode = "process";
KillSignal = "SIGINT";
};
wantedBy = ["multi-user.target"];
};
timers.nextcloud-paperless-autosync = {
unitConfig.Description = "Automatic sync files with Nextcloud when booted up after 5 minutes then rerun every 30 minutes";
timerConfig.OnUnitActiveSec = "30min";
wantedBy = ["multi-user.target" "timers.target"];
};
};
}; };
} }