backing up a nextcoud instance using rsync, remotely

So, all these years my hosted nextcloud has worked perfectly fine. But, as life goes, the old server reached its end of support, and I needed a new solution.

I fiddled around with several hosters, tried self-hosting at home, but our Vodafone Internet isn’t the most reliable. So I got into the oracle cloud, and using their “always free” tier created a really cheap but powerful ARM Ubuntu server to host my nextcloud.

Now that the comfort of hosting is gone, I needed a new solution for backups. And what I basically did is use the steps from the nextcloud docs. Automated, in a script on my nextcloud server, over VPN per rsync on my home NAS. And I make use of hardlinks and versioned backups.

Find my script here for your convenience: https://github.com/maybeageek/RsyncBackupScript/blob/main/ncbackup.sh

So what I basically did was:

  • Create a VCN in the oracle cloud and setup a site2site VPN to my home
  • create a user on my home Proxmox that only has rights to login via ssh using a keyfile, and access rights to one folder alone, and nothing else.
  • ssh-copy-id the key onto my home machine
  • Setup the script on my nextcloud machine in the oracle cloud to run once a night
  • the script then performs the backup tasks
    • nextlcoud into maintenance mode
    • copy nextcloud folder
    • copy data folder
    • dump database and copy over
    • exit maintance mode

Using rsync and its ability to link to previous backups and only copy over what has changed has some nice benefits:

  • Duration: While the initial backup took over 2 hours, each consecutive run is shorter than 5 minutes.
  • Versioned backups: I have a copy of all three (nc folder, data folder and db dump) that is dated and I could chose to restore at any given point
  • Deduplication of sorts: While it is not really a filesystem dedup, every backup really only takes some headroom for the new links (about 300K) and what’s new.
  • Convenience: Using hardlinks on the fs means I don’t have to care about the backup chain like in a traditional backup using full and then incremental backups. I can delete old backups if I like without destroying the chain, as every file points to the inode on the disk, and the data gets deleted once the last reference to the inode was deleted.
user@pve:/tank/ncbackup/nextcloud# ls -lisa
total 65771
59391 9 drwxr-xr-x 14 ncbackup ncbackup 21 Dec 30 04:00 .
34 1 drwxr-xr-x 3 root root 3 Dec 29 11:30 ..
167275 1 lrwxrwxrwx 1 ncbackup ncbackup 49 Dec 30 04:00 current-data -> /tank/ncbackup/nextcloud/data-2023-12-30-03-00-01
160962 1 lrwxrwxrwx 1 ncbackup ncbackup 54 Dec 30 04:00 current-nextcloud -> /tank/ncbackup/nextcloud/nextcloud-2023-12-30-03-00-01
84610 9 drwxrwx--- 8 ncbackup ncbackup 13 Dec 28 11:49 data-2023-12-29
84387 9 drwxrwx--- 8 ncbackup ncbackup 13 Dec 29 14:25 data-2023-12-29-13-25-40
133899 9 drwxrwx--- 8 ncbackup ncbackup 13 Dec 29 14:29 data-2023-12-29-13-29-25
133947 9 drwxrwx--- 8 ncbackup ncbackup 13 Dec 29 14:30 data-2023-12-29-13-30-37
84463 9 drwxrwx--- 8 ncbackup ncbackup 13 Dec 29 15:43 data-2023-12-29-14-43-14
152463 9 drwxrwx--- 8 ncbackup ncbackup 13 Dec 30 04:00 data-2023-12-30-03-00-01
35843 9 drwxr-xr-x 14 ncbackup ncbackup 32 Dec 27 12:52 nextcloud-2023-12-29
119171 9 drwxr-xr-x 14 ncbackup ncbackup 32 Dec 29 14:25 nextcloud-2023-12-29-13-25-40
84447 9 drwxr-xr-x 14 ncbackup ncbackup 32 Dec 29 14:29 nextcloud-2023-12-29-13-29-25
116822 9 drwxr-xr-x 14 ncbackup ncbackup 32 Dec 29 14:30 nextcloud-2023-12-29-13-30-37
146244 9 drwxr-xr-x 14 ncbackup ncbackup 32 Dec 29 15:43 nextcloud-2023-12-29-14-43-14
156804 9 drwxr-xr-x 14 ncbackup ncbackup 32 Dec 30 04:00 nextcloud-2023-12-30-03-00-01
84462 12953 -rw-r--r-- 1 ncbackup ncbackup 31024065 Dec 29 14:29 nextcloud-sqlbkp_20231229-13-29-32.bak
84631 12949 -rw-r--r-- 1 ncbackup ncbackup 31012268 Dec 29 14:30 nextcloud-sqlbkp_2023-12-29-13-30-44.bak
152462 13409 -rw-r--r-- 1 ncbackup ncbackup 32043065 Dec 29 15:43 nextcloud-sqlbkp_2023-12-29-14-43-33.bak
84446 12941 -rw-r--r-- 1 ncbackup ncbackup 31000102 Dec 29 14:25 nextcloud-sqlbkp_20231229.bak
84636 13409 -rw-r--r-- 1 ncbackup ncbackup 32090235 Dec 30 04:00 nextcloud-sqlbkp_2023-12-30-03-00-10.bak
user@pve:/tank/ncbackup/nextcloud#

voila: automated versioned backups, securely transferred over an encrypted VPN using SSH.