Skip to content

Commit

Permalink
hosts (srv-test-2): Add NextCloud synx script (#78)
Browse files Browse the repository at this point in the history
The script uses ssh, scp, and rsync to connect to and transfer data from
srv-prod-2. Data being present on srv-test-2 NextCloud is dropped in the
process.
  • Loading branch information
britter authored Nov 29, 2024
1 parent ac48c72 commit 4fb7d74
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions systems/x86_64-linux/srv-prod-2/configuration.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
};
};

users.users.nextcloud.openssh.authorizedKeys.keyFiles = [../srv-test-2/ssh_srv-test-2_ed25519_key.pub];

# This value determines the NixOS release from which the default
# settings for stateful data, like file locations and database versions
# on your system were taken. It‘s perfectly fine and recommended to leave
Expand Down
1 change: 1 addition & 0 deletions systems/x86_64-linux/srv-test-2/configuration.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{...}: {
imports = [
../../../modules
./nextcloud-sync.nix
];

my = {
Expand Down
29 changes: 29 additions & 0 deletions systems/x86_64-linux/srv-test-2/nextcloud-sync.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
config,
pkgs,
...
}: let
nextcloud-sync = let
prodHost = config.my.homelab.srv-prod-2.ip;
nextcloudUser = "nextcloud";
dataDir = "/var/lib/nextcloud/data";
dbName = "nextcloud";
dbDump = "/tmp/nextcloud-prod-dump.sql";
in
pkgs.writeShellApplication {
name = "nextcloud-sync";
runtimeInputs = [pkgs.rsync];
text = ''
ssh ${nextcloudUser}@${prodHost} "pg_dump --username=${nextcloudUser} --file=${dbDump} ${dbName}"
scp ${nextcloudUser}@${prodHost}:${dbDump} ${dbDump}
ssh ${nextcloudUser}@${prodHost} "rm -f ${dbDump}"
rsync -avz --delete ${nextcloudUser}@${prodHost}:${dataDir} ${dataDir}
sudo -u ${nextcloudUser} psql --command="DROP DATABASE IF EXISTS ${dbName};"
sudo -u ${nextcloudUser} psql --command="CREATE DATABASE ${dbName} OWNER ${nextcloudUser};"
sudo -u ${nextcloudUser} psql --dbname=${dbName} --file=${dbDump}
rm -f ${dbDump}
'';
};
in {
environment.systemPackages = [nextcloud-sync];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIE49YvpRLFot3FIVI16tmHEOxvO2LCKP2+40C08QT97d bene@pulse-14

0 comments on commit 4fb7d74

Please sign in to comment.