This guide explains how to set up automated backups for Home Assistant OS using BorgBackup and a cron job. The backups are stored remotely on BorgBase, and old files are pruned automatically. The deployment of the backup script is automated using Ansible.
- Home Assistant OS with Advanced SSH Add-on installed.
- Automatic backups enabled in Home Assistant to local storage.
- A BorgBase repository ready for use.
- Ansible installed on your local machine.
- 
Clone the repo. 
- 
Copy the variable template file and update it with your specific settings: cp group_vars/all.yml.tpl group_vars/all.yml 
- 
Edit group_vars/all.ymlto set the required variables:- borg_repo: Your BorgBase repository URL.
- borg_passphrase: Your Borg repository passphrase.
- backup_dirs: The directory to be backed up.
 Example: borg_repo: 'ssh://[email protected]/./repo' borg_passphrase: 'YOUR-PASSPHRASE' backup_dirs: '/backup' 
Execute the Ansible playbook to deploy the backup script to your Home Assistant machine:
ansible-playbook main.ymlThis will:
- Deploy the backup script /homeassistant/borg/my-backup.shto the target machine.
- Ensure the script has executable permissions (0755).
- 
Access your Home Assistant machine: ssh root@ha-ip 
- 
Create the required directories and generate SSH keys for BorgBase: mkdir -p /homeassistant/borg/.ssh ssh-keygen -t ed25519 -f /homeassistant/borg/.ssh/borgbase-partial 
- 
Upload the public key ( /homeassistant/borg/.ssh/borgbase-partial.pub) to your BorgBase repository.
- 
Install additional packages: cronie 
- 
Add the cron job for automatic backups: crond -f & (echo '15 1 * * * /homeassistant/borg/my-backup.sh >> /dev/null 2>&1') | crontab - Choose the time for the cron job to run ( 15 1represents 1:15 AM daily).
- 
Save and restart the Advanced SSH Add-on. 
- 
Test the backup script manually: /homeassistant/borg/my-backup.sh 
- 
Confirm the cron job is running: crontab -l 
- 
Verify that backups are being uploaded to BorgBase. 
- Logs: Redirect cron output to a log file for debugging by modifying the cron job:
15 1 * * * /homeassistant/borg/my-backup.sh >> /homeassistant/borg-backup.log 2>&1