Automate Daily MongoDB & PostgreSQL Backups to S3 with Node.js
Automate daily MongoDB and PostgreSQL backups with Node.js, compress them, and upload to Amazon S3 for reliable, unattended protection.
6 min read
Keeping production databases safe means protecting against hardware failure, accidental deletion, and ransomware. Manual dumps are fine for occasional snapshots, but most sites need a repeatable, unattended backup routine that runs every day. In this article we’ll show how to automate daily backups for MongoDB and PostgreSQL and store the resulting archive in Amazon S3 using simple Node.js scripts.
Why Node.js?
Cross‑platform – The same JavaScript runs on Ubuntu, Debian, AlmaLinux, Rocky Linux, and Windows Server.
Rich ecosystem – Packages like aws-sdk, child_process, and node-cron let you call native dump utilities and upload files with minimal code.
Extensible – Add compression, encryption, or notification hooks without touching the core logic.
Prerequisites
A Linux server (Ubuntu/Debian or AlmaLinux/Rocky/RHEL) with root or sudo access.
MongoDB and PostgreSQL installed and running.
An AWS account and an S3 bucket for backups.
Node.js (v18 or later) and npm installed.
Install Required Packages
1. Install MongoDB and PostgreSQL client utilities
These command‑line tools are invoked by the Node.js script.
On Windows Server, download the binaries from the official sites and add them to %PATH%.
2. Set up a Node.js project
# Create a directory for the backup scripts
mkdir ~/db-backup && cd ~/db-backup
# Initialise a new npm project
npm init -y
# Install AWS SDK v3 and a cron helper
npm install @aws-sdk/client-s3 @aws-sdk/lib-storage node-cron
Check system logs: journalctl -u db-backup.service -f for real‑time output.
Monitoring and Alerts (Optional)
The script logs to stdout. To receive notifications, extend it to send alerts via:
AWS SNS, Slack webhook, or email (e.g., nodemailer)
Local desktop alerts with node-notifier (development only)
Wrapping each major step in try/catch already provides clear error reporting; just forward the error message to your chosen channel.
Conclusion
Automating daily backups for MongoDB and PostgreSQL with Node.js offers a lightweight, portable solution that fits naturally into a DevOps workflow. By leveraging native dump utilities, compressing the output, and uploading to Amazon S3, you get:
Zero‑touch daily protection.
Centralised, durable storage that can be versioned or replicated.
Flexibility to add encryption, retention policies, or alerting without rewriting the core logic.
Deploy the script on your AtoZNode VPS or dedicated server, adjust the configuration for your environment, and you’ll have reliable backups that run automatically every day.