How to set up automated database backups on DigitalOcean
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)
Step-by-Step Instructions
Access your DigitalOcean control panel
Configure managed database backups
Select Enable Point-in-Time Recovery for additional protection, which allows restoration to any specific moment within the retention window.
Set up backup notifications
Create backup script for self-managed databases
#!/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.Configure automated execution with cron
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.Configure Spaces for backup storage
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 digitaloceanTest and verify backup functionality
/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.
Monitor backup status and maintenance
Consider setting up additional monitoring with DigitalOcean's monitoring agent or third-party tools to track backup job success rates and storage usage.
Common Issues & 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.