Skip to main content

Database Backup

The Store wire can automatically back up your MongoDB database on a schedule and upload the backups to S3 or local storage.

Configuration

store({
backup: {
enabled: true,
schedule: "0 2 * * *", // Cron expression: daily at 2 AM
retention: 30, // Keep backups for 30 days
storage: {
provider: "s3",
bucket: "my-app-backups",
region: "us-east-1",
accessKeyId: process.env.S3_ACCESS_KEY,
secretAccessKey: process.env.S3_SECRET_KEY,
},
collections: ["all"], // Back up all collections (default)
compression: true, // Compress with gzip (default: true)
},
});

Manual Backup

Programmatic

const backup = await app.store.backup.create();

console.log(backup._id); // Backup ID
console.log(backup.filename); // "backup-2025-01-15T02-00-00.gz"
console.log(backup.size); // Size in bytes
console.log(backup.url); // Download URL

REST API

curl -X POST http://localhost:3000/store/backup \
-H "Authorization: Bearer eyJhbGci..."

Response (201):

{
"_id": "65a1b2c3d4e5f6a7b8c9d0e1",
"filename": "backup-2025-01-15T10-30-00.gz",
"size": 10485760,
"collections": ["auth_users", "products", "messaging_logs"],
"url": "https://my-app-backups.s3.amazonaws.com/backup-2025-01-15T10-30-00.gz",
"createdAt": "2025-01-15T10:30:00.000Z"
}

List Backups

curl http://localhost:3000/store/backups \
-H "Authorization: Bearer eyJhbGci..."

Response (200):

{
"backups": [
{
"_id": "65a1b2c3d4e5f6a7b8c9d0e1",
"filename": "backup-2025-01-15T02-00-00.gz",
"size": 10485760,
"collections": ["all"],
"createdAt": "2025-01-15T02:00:00.000Z"
}
],
"total": 15
}

Restore

Programmatic

await app.store.backup.restore(backupId);

REST API

curl -X POST http://localhost:3000/store/backup/65a1b2c3d4e5f6a7b8c9d0e1/restore \
-H "Authorization: Bearer eyJhbGci..."

Response (200):

{
"message": "Backup restored successfully",
"collections": ["auth_users", "products", "messaging_logs"],
"documents": 1542
}

Selective Backup

Back up only specific collections:

store({
backup: {
collections: ["products", "orders"],
},
});

Or trigger a selective backup programmatically:

const backup = await app.store.backup.create({
collections: ["products"],
});

Retention Policy

Backups older than the retention period are automatically deleted. Set retention: 0 to keep backups indefinitely.

VariableDefaultDescription
BACKUP_SCHEDULE--Cron expression for scheduled backups
BACKUP_RETENTION30Days to retain backups
BACKUP_COMPRESSIONtrueEnable gzip compression