How to automatically backup and sync a YunoHost VPS, to a remote storage VPS (not running YunoHost).

On the remote server, that is to store the backup:

I started by creating an ECDSA key pair of 384 bits, with this command:

ssh-keygen -t ecdsa -b 384

Then i sent it to the YunoHost VPS with this command:

ssh-copy-id -i ~/.ssh/your-key-name.pub 'username'@'host-domain'

Then i created a cronjob that will sync two directories, over SSH, using rsync.

To do this, you first have to create a cronjob with this command:

sudo crontab -e

Then modify and paste this line into the crontab, save and exit:

11 1 * * * rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" 'username'@'host-domain':/home/yunohost.backup/archives/ /home/'username'/archives/

For your information:

-avz Archive mode for preserving metadata, verbosity for detailed feedback, and compression for efficient transmission.

-e Specifies the remote shell to use, in this case, SSH.

-o StrictHostKeyChecking=no Tells SSH not to bother checking if it has connected to the same host before.

-o UserKnownHostsFile=/dev/null This says, “Hey SSH, don't bother looking for known hosts in the usual place/known_hosts, we already know what's good”

Now, the remote storage VPS looks for new backups to download, everyday at 01:11


On your YunoHost VPS, you have to add a cronjob, creating backups every night, around midnight. Same procedure as last time:

sudo crontab -e

Add this line, save and exit:

11 0 * * * yunohost backup create

With this method, the YunoHost VPS backs up everything, and syncs to remote storage, everyday around midnight.

Rsync will not delete any backups from remote storage, in case they get deleted on the YunoHost VPS. (This way, if anything bad happens to the YunoHost VPS, or the yunohost.backups directory, you won't lose the backup files on the remote storage VPS next time it syncs)

This does mean, that you will have to delete backups, both on the YunoHost VPS and the remote storage VPS, as they become outdated to preserve precious space on your drives.

See the official YunoHost documentation if you want a different solution


How to automatically delete outdated backups.

I made a script that looks for any file older than 7 days, then removes it.

Please change the 'backup dir' path, and the 'days to keep value' to suit your needs!

https://raw.githubusercontent.com/tr00ls/tr00ls/main/cleanup_backups.sh

One-liner to download the script and make it executable:

wget https://raw.githubusercontent.com/tr00ls/tr00ls/main/cleanup_backups.sh && sudo chmod +x cleanup_backups.sh

To add a cronjob running this script every night at 02:00 enter:

sudo crontab -e

Then modify and paste in the following line, save and exit:

0 2 * * * /path/to/cleanup_backups.sh