Intermediate
How to set up automated database backups on DigitalOcean
Quick Answer
DigitalOcean offers automated backups for managed databases through their control panel, while self-managed databases require setting up cron jobs with backup scripts. You can enable daily automated backups with point-in-time recovery for managed databases directly in the DigitalOcean dashboard.
Prerequisites
- Active DigitalOcean account with billing set up
- Database droplet or managed database running
- Basic understanding of Linux command line
- SSH access to your droplet (if using self-managed database)
1
Access your DigitalOcean control panel
Log into your DigitalOcean account and navigate to the Databases section in the left sidebar. If you're using a managed database, select your database cluster from the list. For self-managed databases on droplets, go to Droplets and select your database server.
Tip
Managed databases automatically include backup features, while droplet-based databases require manual configuration.
2
Configure managed database backups
For managed databases, click on your database cluster and go to the Settings tab. Under Backup Configuration, enable Automated Backups by toggling the switch. Set your preferred backup window during low-traffic hours and configure the retention period (7-35 days available).
Select Enable Point-in-Time Recovery for additional protection, which allows restoration to any specific moment within the retention window.
Select Enable Point-in-Time Recovery for additional protection, which allows restoration to any specific moment within the retention window.
Tip
Schedule backups during your application's lowest usage periods to minimize performance impact.
3
Set up backup notifications
In the Settings tab, scroll to Alerting and click Add Alert. Configure email notifications for backup failures by selecting Backup Status as the metric and entering your email address. Set the alert threshold to trigger on backup failures or warnings.
4
Create backup script for self-managed databases
For droplet-based databases, SSH into your server and create a backup script:
Save this script as
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/var/backups/mysql"
mkdir -p $BACKUP_DIR
# For MySQL
mysqldump -u root -p[PASSWORD] --all-databases > $BACKUP_DIR/backup_$DATE.sql
# For PostgreSQL
pg_dumpall -U postgres > $BACKUP_DIR/backup_$DATE.sql
# Compress backup
gzip $BACKUP_DIR/backup_$DATE.sql
# Remove backups older than 7 days
find $BACKUP_DIR -name "*.gz" -mtime +7 -deleteSave this script as
/usr/local/bin/db_backup.sh and make it executable with chmod +x /usr/local/bin/db_backup.sh.Tip
Store database credentials in a secure configuration file rather than hardcoding them in the script.
5
Configure automated execution with cron
Open the crontab editor with
This runs the backup script every day at 2 AM and logs output to
crontab -e and add a line to schedule daily backups:0 2 * * * /usr/local/bin/db_backup.sh >> /var/log/db_backup.log 2>&1This runs the backup script every day at 2 AM and logs output to
/var/log/db_backup.log. Verify the cron job is scheduled by running crontab -l.Tip
Test your backup script manually before scheduling it to ensure it works correctly.
6
Configure Spaces for backup storage
Create a DigitalOcean Space for storing backups by going to Spaces in your control panel and clicking Create a Space. Choose a datacenter region and name your space. Install the AWS CLI tool on your droplet with
Modify your backup script to upload compressed backups to Spaces using:
apt install awscli and configure it with your Spaces access keys:aws configure --profile digitaloceanModify your backup script to upload compressed backups to Spaces using:
aws s3 cp $BACKUP_DIR/backup_$DATE.sql.gz s3://your-space-name/ --profile digitaloceanTip
Generate Spaces API keys from the API section in your DigitalOcean control panel.
7
Test and verify backup functionality
For managed databases, trigger a manual backup from the Backups tab to verify the process works. For self-managed databases, run your backup script manually with
Verify backups are being created and stored properly by checking your backup directory or Spaces bucket. Test restoration from a backup to ensure data integrity.
/usr/local/bin/db_backup.sh and check the output in /var/log/db_backup.log.Verify backups are being created and stored properly by checking your backup directory or Spaces bucket. Test restoration from a backup to ensure data integrity.
Tip
Document your backup and restoration procedures for your team and test the restoration process regularly.
8
Monitor backup status and maintenance
Set up monitoring by checking the Metrics tab in your database dashboard for managed databases. For self-managed setups, create a monitoring script that checks backup log files and sends alerts on failures. Regularly review backup sizes and adjust retention policies as needed.
Consider setting up additional monitoring with DigitalOcean's monitoring agent or third-party tools to track backup job success rates and storage usage.
Consider setting up additional monitoring with DigitalOcean's monitoring agent or third-party tools to track backup job success rates and storage usage.
Tip
Create a monthly task to verify that backups can be successfully restored and contain expected data.
Troubleshooting
Managed database backup fails with insufficient space error
Check your database cluster's disk usage in the Metrics tab. If approaching capacity, either clean up old data or resize your cluster by going to Settings > Resize and selecting a larger plan.
Cron job backup script not executing
Verify cron service is running with
systemctl status cron. Check cron logs with grep CRON /var/log/syslog and ensure your script has proper file permissions and the correct shebang line #!/bin/bash.Backup uploads to Spaces failing with authentication errors
Verify your Spaces access keys are correct by running
aws configure list --profile digitalocean. Ensure your Spaces bucket permissions allow write access and check that the endpoint URL is properly configured for your region.Point-in-time recovery option missing for managed database
Point-in-time recovery is only available for certain managed database types and plans. Check DigitalOcean's documentation for supported databases and upgrade to a compatible plan if necessary. PostgreSQL and MySQL managed databases support this feature on most plans.
Ready to get started with DigitalOcean?
Put this tutorial into practice. Visit DigitalOcean and follow the steps above.
Visit DigitalOcean →