db-backup-tool
v1.1.4
Published
A Node.js library for performing database backups with configurable options. This tool supports both local and cloud-based storage options and can be scheduled to run periodically.
Downloads
982
Readme
db-backup-tool
A Node.js library for performing database backups with configurable options. This tool supports both local and cloud-based storage options and can be scheduled to run periodically.
Features
- Support for MongoDB and SQL databases
- Backup storage options: Local and Cloud (Azure)
- Backup frequency and retention schedule
- Customizable database queries for backup
- CLI support to trigger backup, show config, test connection, and schedule backups
Installation
To use the db-backup-tool
in your project, follow the steps below:
- Install the tool via npm:
npm install db-backup-tool
- Post-installation:
- The
db.config.json
file will be automatically created in the root directory of your project. You can configure this file with your database and backup settings.
- The
Configuration (db.config.json
)
The db.config.json
file allows you to configure your database settings, storage options, and backup schedule. Below is an example configuration and description of each field:
Example Configuration
{
"database": {
"type": "mongo",
"uri": "mongodb+srv://<username>:<password>@cluster0.f7fui.mongodb.net/",
"db": "sample_mflix",
"host": "localhost",
"user": "root",
"password": "root",
"port": "3306"
},
"storage": {
"type": "local",
"backupFormat": "json",
"batch": "10000",
"local": {
"path": "./backup"
},
"query": {
"name": "Yara Greyjoy"
},
"cloud": {
"provider": "azure",
"connectionKey": "DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=mykey",
"container": "backup01",
"accessKey": "your-access-key",
"secretKey": "your-secret-key",
"bucket": "your-bucket-name"
}
},
"schedule": {
"enabled": true,
"frequency": "daily",
"time": "00:04",
"interval": "1d",
"maxBackups": 10,
"backupRetention": "7d",
"query": {
"status": "active",
"age": {
"$gt": 30
}
},
"backupFormat": "json"
}
}
Configuration Fields
database:
type
: The type of database. Can bemongo
for MongoDB ormysql
for SQL databases.uri
: The connection URI for the database.db
: The name of the database to back up.host
,user
,password
,port
: Credentials and connection details for SQL databases.uri
is required for MongoDB andhost
,user
,password
, andport
for SQL databases.
storage:
type
: Specifies where the backup is stored. Can belocal
orcloud
.backupFormat
: The format of the backup. Can bejson
,bson
, or other formats.batch
: Defines the batch size for backups.local
: Defines local storage options, including the path to save backups.cloud
: Defines the cloud provider settings for backup storage (e.g., Azure), including the connection key, container, and access credentials.
schedule:
enabled
: Whether to enable scheduling for the backup.frequency
: How often the backup runs (daily
,weekly
, etc.).time
: The time at which the backup should occur.interval
: The interval between backups (e.g.,1d
for one day).maxBackups
: The maximum number of backups to keep.backupRetention
: The duration for retaining backups (e.g.,7d
for 7 days).query
: The conditions for selecting data to back up. Use query operators like$gt
,$lt
, etc.backupFormat
: The format in which to store the backup (e.g.,json
).
Usage
CLI Commands
Once installed, the tool can be used via the command line interface (CLI). The following commands are available:
Backup Command:
This command will run the backup process based on the configuration provided in
db.config.json
.db_tool backup
Show Config Command:
This command displays the current configuration from
db.config.json
.db_tool show-config
Output:
Current Configuration: { "database": { "type": "mongo", "uri": "mongodb+srv://<username>:<password>@cluster0.f7fui.mongodb.net/", "db": "sample_mflix", ... }, ... }
Test Connection Command:
This command tests the database connection based on the configuration in
db.config.json
. It supports MongoDB and MySQL databases.db_tool test-connection
- If you are using MongoDB, it will attempt to connect to the MongoDB instance.
- If you are using MySQL, it will attempt to connect to the MySQL database.
Schedule Command:
This command runs the backup process according to the schedule defined in
db.config.json
.db_tool schedule
It will check if scheduling is enabled and run the backup according to the specified frequency, time, and interval.
Example Usage
# Run a backup process
npx db_tool backup
# View the current configuration
npx db_tool show-config
# Test the database connection
npx db_tool test-connection
# Start scheduled backups
npx db_tool schedule
Troubleshooting
- Ensure that the
db.config.json
file is located in the root of your project. - Verify that the database connection details (URI or credentials) are correct.
- If using cloud storage, ensure that the cloud provider's credentials and access keys are correct.
- If the CLI commands are not working as expected, make sure the correct version of the tool is installed.