NextCloud Backup function

Hi all,

I would highly appreciate having a way to backup my config folder to Nextcloud with auto backup and manual backup possibility.

did you do this with Nextcloud ?

No… unfortunately I don’t have the time at the moment to investigate

How abut hosting the files directly on nextcloud?

Or connect to it with smb
https://docs.nextcloud.com/server/12/admin_manual/configuration_files/external_storage/smb.html

everything is possible… I shut down my next cloud instance as I realized I don’t use it at all.

Hi everybody !
Did you finaly succeed to backup automaticaly to Nextcloud ?

Hi. I just made a workaround. It is not as simple as I would like, nor elegant, but it works and at the end of the day the snapshot is sent to nextcloud automatically…

Basically the issue is that home-assistant container can not access the backup folder. This means you can not create any type of script or automation or shell command that can be called from within home assistant to interact with your backups. The only way would be an hassio-add on, but this is way over my skillset.
However, thanks to the ssh addon, it is possible to access the /backup/ folder through ssh from an other machine.

So, the idea is to use a second raspberry (a zero is enough). This secondary raspberry will run a script on your home-assistant system (raspberry, nuc, whatever) through ssh once a day. All the script does is find the latest file in the backup folder and send it to your nextcloud instance.

So here is a walkthrough
HA_sys : machine that hosts HA
Sec_pi : a raspberry pi zero or whatever will run the script

  1. Create SSH keys on Sec_pi and copy the public key in the ssh addon config of Home Assistant.
  2. On Sec_pi, create a script snapshot_to_nextcloud.sh with the following code
#!/bin/bash
# Gets the latest file in the backup directory and send it to nextcloud

# Parameters
HOST='https://yournextcloud'
NEXTCLOUD_DIR='folder/where/your/want/your/backups'
USERNAME='yourUsername'
USER=yourUser
PASSWORD=yourPassword


# Get newest file in backup directory
filename=$(ls -t /backup/ | head -n1)

# Send file to Nextcloud
/usr/bin/curl -u $USER:$PASSWORD -T /backup/$filename "$HOST/remote.php/dav/files/$USERNAME/$NEXTCLOUD_DIR/$filename"

Edit the 5 parameters to fit your nextcloud instance. Note that for security reasons, it is recommanded to create specific app password in nextcloud instead of using your account password.

  1. Save the script and make it executable with chmod +x snapshot_to_nextcloud.sh

  2. Create a cronjob on Sec_pi that will execute this script every night
    crontab -e
    then at the end of the file, just add
    0 0 * * * /usr/bin/ssh root@YOUR_HA_IP < /root/snapshot_to_nextcloud.sh
    Do not forget to input your HA_sys IP. Also, if you placed the snapshot_to_nextcloud.sh in an other directory on your Sec_pi, you need to use its right path instead of /root/.

  3. Save.

This is it. Every night at midnight your Sec_pi will ssh to HA_sys and execute the script that will pull the latest snapshot available and send it to your nextcloud. It is not pretty and it is a bit convoluted, but in the end it works like a charm.

If you have trouble setting this up, reach me out.

EDIT: Publish a snippet on gitlab for easier maintenance/updates.

2 Likes