Guide to Configuring Borgmatic for Automated Daily VPS Backups to Cloudflare R2
This is a detailed guide to configure Borgmatic, a backup tool based on BorgBackup, to automatically back up all VPS data to Cloudflare R2 daily. The guide covers installation,...
This is a detailed guide to configure Borgmatic, a backup tool based on BorgBackup, to automatically back up all VPS data to Cloudflare R2 daily. The guide covers installation, configuration file setup, creating a Cloudflare API account, setting up a cron job, and verifying backup results. Steps are clearly explained so users can implement secure, efficient, and automated backups for their VPS servers.
Introduction to Borgmatic and Cloudflare R2
Borgmatic is a wrapper around BorgBackup that simplifies configuring and running backup tasks. It allows users to define backup rules using a YAML configuration file, automate backups, verify integrity, and clean up old backups. Cloudflare R2 is a free object storage service designed for storing unstructured data such as backup files, images, logs, and documents.
Combining Borgmatic with Cloudflare R2 offers significant benefits: backups are stored securely on a global cloud platform with scalability and zero storage costs. Additionally, Cloudflare R2 supports the S3 protocol, enabling Borgmatic to connect directly using S3 libraries like boto3 or s3cmd.
Prerequisites and Initial Setup
To implement automated backups, you need:
- A Linux-based VPS (Ubuntu/Debian/CentOS)
- Borgmatic installed and configured
- A Cloudflare R2 account (free to create at https://dash.cloudflare.com)
- API key and secret key from Cloudflare
First, install Borgmatic using pip:
sudo apt update
sudo apt install python3-pip
pip3 install borgmatic
Run the following command to check the version:
borgmatic --version
Create API Account and Configure Cloudflare R2
In the Cloudflare Dashboard, go to the "Object Storage" section and create a new bucket (e.g., vps-backups). Then, create a new API account with "Read/Write" permissions for this bucket.
Save the Access Key and Secret Key. These will be used in the Borgmatic configuration file.
Run the following command to test the connection to R2 using s3cmd:
sudo apt install s3cmd
s3cmd --configure
Enter the following information:
- Access Key: [API key]
- Secret Key: [Secret key]
- Endpoint: s3.r2.cloudflarestorage.com
- Bucket: vps-backups
Set Up Borgmatic Configuration File
Create the configuration file at /etc/borgmatic/config.yaml:
location:
source_dir: /home
sync_remote: true
remote_url: s3://[email protected]/
remote_storage: s3
remote_storage_options:
- endpoint_url=https://s3.r2.cloudflarestorage.com
- access_key_id=[Access Key]
- secret_access_key=[Secret Key]
retention:
keep_daily: 7
keep_weekly: 4
keep_monthly: 6
archive:
compression: zstd
chunker_params: 19,23,21,4096
notifications:
email:
from: [email protected]
to: [email protected]
smtp_host: smtp.gmail.com
smtp_port: 587
smtp_username: [email protected]
smtp_password: yourpassword
smtp_starttls: true
Replace the bracketed values with your information. This configuration backs up the /home directory, keeps daily backups for 7 days, weekly backups for 4 weeks, and monthly backups for 6 months.
Run Backup and Set Up Cron Job
Run the following command to check the configuration:
borgmatic check
Run the first backup:
borgmatic
To run backups daily, add to cron:
sudo crontab -e
Add the following line:
0 2 * * * /usr/local/bin/borgmatic > /var/log/borgmatic.log 2>&1
This runs the backup at 2:00 AM every day.
Verify and Troubleshoot
To verify the backup results, check the log at /var/log/borgmatic.log. If there are errors, check the following:
- API key and secret key are correct
- Endpoint address is accurate
- Bucket permissions are sufficient
- No network or firewall issues
If you encounter S3-related errors, test the connection using s3cmd:
s3cmd ls s3://vps-backups/
Refer to the official Borgmatic documentation for more information.
Related infrastructure & engineering guides: NVMe Cloud VPS for Docker, Nginx & Databases · Real-Time Data Sync Between DuckDB and PostgreSQL for Exam Performance Reporting
Recommended NVMe Cloud VPS Plans to Deploy This Setup
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
