nestjs-database-dump
v1.0.3
Published
## Description
Readme
AutoDatabaseDumpModule
Description
AutoDatabaseDumpModule is a NestJS module for automatically creating dumps of a PostgreSQL database at a specified interval. The database dump is saved to a specified directory, and old files are automatically deleted after a certain retention period. It also includes an optional feature for sending notifications via Telegram about the dump process..
Installation
npm install auto-database-dumpUsage
Importing the module in AppModule
import { Module } from '@nestjs/common';
import { AutoDatabaseDumpModule } from '@your-org/auto-database-dump';
@Module({
imports: [
AutoDatabaseDumpModule.forRoot({
dialect: 'postgres',
username: 'your_username',
password: 'your_password',
database: 'your_database',
host: 'localhost',
port: 5432,
dumpPathDir: '/path/to/dumps',
projectName: 'my_project',
telegramBotToken: 'your-telegram-bot-token', // Optional: For sending notifications
telegramChatId: 'your-chat-id', // Optional: For sending notifications
}),
],
})
export class AppModule {}Parameter Description
| Parameter | Type | Description | |------------------|------------|----------------------------------------------------------| | dialect | 'postgres' | The type of database (only PostgreSQL is supported). | | username | string | The username for connecting to the database. | | password | string | The password for the user. | | database | string | The name of the database. | | host | string | The database host. | | port | number | The database port. | | dumpPathDir | string | The directory path to store the dumps. | | projectName | string | The project name used in the dump file names. | | telegramBotToken | string | (Optional) The Telegram bot token to send notifications. | | telegramChatId | string | (Optional) The chat ID to send notifications to. |
How It Works
- The module creates a dump of the database every day at 8:00 PM (configured via @Cron('0 20 * * *')).
- The dump file is saved in the directory {dumpPathDir}/{projectName}/dump_YYYY-MM-DD_HH:mm:ss.sql.
- Old dumps older than 30 days are automatically deleted.
- If the telegramBotToken and telegramChatId are provided, the module will send notifications to the specified Telegram chat about the status of the dump process.
Logging
- ✅ Successful dump creation: Database dump created: /path/to/dump.sql (Optional) Sends a notification to Telegram: “Database dump created for project {projectName} at {dumpTime} (server time)”
- ❌ Error creating dump: Error creating dump: (Optional) Sends a notification to Telegram: “Error creating dump from project {projectName}: {error} at {errorTime} (server time)”
- 🗑️ Old dump deletion: Old dump deleted: dump_YYYY-MM-DD_HH:mm:ss.sql (Optional) Sends a notification to Telegram: “Old dump deleted from project {projectName}: {fileName} at {deletionTime} (server time)”
License
MIT
