Automated Nextcloud Backups on VPS with BorgBackup and AWS S3
Learn how to automatically back up your Nextcloud instance on VPS with BorgBackup and AWS S3. Step-by-step guide for disaster recovery.
Why automated backups for Nextcloud are essential
Your Nextcloud instance stores valuable data – from documents and photos to calendars. An outage or data loss can have catastrophic consequences. With an automated backup system on your VPS, you ensure that your data can be restored at any time. In this article, we show you how to use BorgBackup in combination with AWS S3 for reliable disaster recovery.
BorgBackup: Efficient and secure
BorgBackup is a powerful tool for deduplicating backups. It compresses and encrypts your data, so you only need minimal storage capacity. Deduplication recognizes identical data blocks and stores them only once – ideal for regular backups of your Nextcloud database and files.
Installing BorgBackup
Log into your VPS via SSH and install BorgBackup:
- Ubuntu/Debian:
sudo apt update && sudo apt install borgbackup - CentOS/RHEL:
sudo yum install epel-release && sudo yum install borgbackup
AWS S3 as external backup target
For disaster recovery, it is important to store backups off your server. AWS S3 offers a cost-effective and scalable storage solution. You need an S3 bucket and an IAM user with access rights. Note down the bucket name, Access Key ID, and Secret Access Key.
Create backup script
Create a script that connects BorgBackup with S3. Here is an example Bash script (/usr/local/bin/nextcloud-backup.sh):
#!/bin/bash
# Variables
BORG_REPO='s3:bucket-name:borg-repo'
BORG_PASSPHRASE='your-password'
NEXTCLOUD_DIR='/var/www/nextcloud'
DB_NAME='nextcloud'
DB_USER='nextcloud'
DB_PASS='your-db-password'
# Backup database
mysqldump -u $DB_USER -p$DB_PASS $DB_NAME > $NEXTCLOUD_DIR/nextcloud-sqlbkp.sql
# Run Borg backup
borg create --stats --progress $BORG_REPO::'{now:%Y-%m-%d_%H:%M:%S}' $NEXTCLOUD_DIR
# Prune old backups (e.g., older than 30 days)
borg prune --keep-daily 7 --keep-weekly 4 --keep-monthly 6 $BORG_REPO
Don't forget to make the script executable: chmod +x /usr/local/bin/nextcloud-backup.sh
Automation with Cron
Set up a cron job to run the script daily. Open the crontab with crontab -e and add the following line:
0 3 * * * /usr/local/bin/nextcloud-backup.shThe backup now runs every morning at 3 AM.
Restoration in case of disaster
If data loss occurs, you can restore the backup as follows:
- Connect to your S3 bucket.
- Mount the Borg repository:
borg mount s3:bucket-name:borg-repo::Backup-Date /mnt/borg - Copy the data back:
rsync -a /mnt/borg/ /var/www/nextcloud/ - Restore the database:
mysql -u nextcloud -p nextcloud < nextcloud-sqlbkp.sql
Test the restoration process regularly to ensure everything works.
Conclusion
With BorgBackup and AWS S3, you have a robust backup strategy for your Nextcloud instance on the VPS. If you don't have a VPS yet, check out our VPS Server – ideal for Nextcloud and other applications. For further assistance, we are always available.