universal-db-backup
v1.0.0
Published
Universal database backup tool supporting MongoDB, MySQL, and PostgreSQL
Maintainers
Readme
universal-db-backup
A universal database backup tool for Node.js that supports MongoDB, MySQL, and PostgreSQL using native dump utilities (mongodump, mysqldump, pg_dump).
Features
- Multi-database support: MongoDB, MySQL, PostgreSQL
- Parameter-based configuration: Simple object config for each database type
- Promise-based API: Clean async/await support
- Optional ZIP compression: Compress backups after creation
- Scheduling: Cron-based recurring backups via
node-cron - CLI support: Run backups from the command line
Prerequisites
Install the appropriate dump utility for your database:
- MongoDB: mongodump (included with MongoDB tools)
- MySQL: mysqldump (included with MySQL client)
- PostgreSQL: pg_dump (included with PostgreSQL)
Installation
npm install universal-db-backupAPI Usage
MongoDB
const { backup } = require('universal-db-backup');
backup({
dbType: 'mongodb',
uri: 'mongodb://localhost:27017/mydb',
output: './backup',
zip: true, // optional: compress to ZIP
gzip: true, // optional: use gzip for mongodump
}).then((result) => {
console.log('Backup path:', result.path);
if (result.zipPath) console.log('ZIP path:', result.zipPath);
}).catch(console.error);MySQL
const { backup } = require('universal-db-backup');
backup({
dbType: 'mysql',
host: 'localhost',
port: 3306,
user: 'root',
password: '123',
database: 'mydb',
output: './backup',
zip: true, // optional
}).then((result) => {
console.log('Backup path:', result.path);
}).catch(console.error);PostgreSQL
const { backup } = require('universal-db-backup');
backup({
dbType: 'postgres',
host: 'localhost',
port: 5432,
user: 'postgres',
password: 'secret',
database: 'mydb',
output: './backup',
zip: true, // optional
format: 'plain', // 'plain' | 'custom' | 'directory' | 'tar'
}).then((result) => {
console.log('Backup path:', result.path);
}).catch(console.error);Scheduled Backups
const { schedule } = require('universal-db-backup');
const job = schedule(
{
dbType: 'mongodb',
uri: 'mongodb://localhost:27017/mydb',
output: './backup',
zip: true,
},
'0 2 * * *', // Every day at 2:00 AM
{ zip: true }
);
// Stop scheduler when needed
// job.stop();async/await
const { backup } = require('universal-db-backup');
async function runBackup() {
try {
const result = await backup({
dbType: 'mysql',
host: 'localhost',
user: 'root',
password: '123',
database: 'mydb',
output: './backup',
});
console.log('Success:', result.path);
} catch (err) {
console.error('Failed:', err.message);
}
}
runBackup();CLI Usage
Install globally or use via npx:
npm install -g universal-db-backupOne-time backup
# MongoDB
universal-db-backup backup -t mongodb -u "mongodb://localhost:27017/mydb" -o ./backup
# MySQL
universal-db-backup backup -t mysql -H localhost -U root -p 123 -d mydb -o ./backup
# PostgreSQL
universal-db-backup backup -t postgres -H localhost -U postgres -p secret -d mydb -o ./backup
# With ZIP compression
universal-db-backup backup -t mongodb -u "mongodb://localhost:27017/mydb" -o ./backup -zScheduled backup
# Daily at 2:00 AM
universal-db-backup schedule -t mysql -H localhost -U root -p 123 -d mydb -o ./backup -c "0 2 * * *"
# Every 6 hours
universal-db-backup schedule -t mongodb -u "mongodb://localhost:27017/mydb" -o ./backup -c "0 */6 * * *"Short alias
udb-backup backup -t mysql -U root -d mydb -o ./backupCLI Options
| Option | Short | Description | |--------|-------|-------------| | --type | -t | Database type: mongodb, mysql, postgres | | --output | -o | Output directory | | --uri | -u | MongoDB connection URI | | --host | -H | Database host (default: localhost) | | --port | -P | Database port | | --user | -U | Database user | | --password | -p | Database password | | --database | -d | Database name | | --zip | -z | Compress backup to ZIP | | --cron | -c | Cron expression (schedule command only) |
License
MIT
