DEV Community

Cover image for Full Backup in Nodejs(Files and Database backup)
Hossein Mobarakian
Hossein Mobarakian

Posted on

Full Backup in Nodejs(Files and Database backup)

NodeFullBackup is an npm package crafted specifically for comprehensive backup solutions. It seamlessly integrates MongoDB backup functionality with file backup capabilities, ensuring that all your critical data is securely preserved. This package is ideal for anyone looking to enhance their data resilience strategy without the hassle of managing multiple backup tools.

Installation

Getting started with NodeFullBackup is quick and easy. Simply install the package via npm using the following command:

npm i @double-man/node-full-backup
Enter fullscreen mode Exit fullscreen mode

Simple Usage

Integrating NodeFullBackup into your project requires minimal effort. Just copy the snippet below into your codebase:

import FullBackup from '@double-man/node-full-backup';

const backup = new FullBackup({
    outputPath: path.resolve('./backup'),
    folders: [path.resolve('./public')],
    expireDays: '1d',
    cronExpression: '0 */6 * * *',
    database: {
        username: 'database_username',
        password: 'database_password',
        database: 'database_name',
        host: 'localhost',
        port: 27017
    }
});

backup.start();
Enter fullscreen mode Exit fullscreen mode

Your support helps this repository grow. Please consider starring it if you find it useful!

Parameters

NodeFullBackup offers a wide range of parameters to tailor your backup process according to your specific needs. Here’s a breakdown of the available options:

Parameter Type Description
outputPath String Destination folder path for backup files.
outputNamePrefix String Prefix for backup file names.
cronExpression String Cron expression for scheduling backups.
outputType String Format of the output file ('zip' or 'tar').
files String[] Array of file paths to include in the backup.
folders String[] Array of folder paths to include in the backup.
expireDays String Number of days after which old backup files will be removed.
afterBackup Function Callback function providing access to the backup file path.
database Object Configuration details for MongoDB database backup.

Database Configuration

When configuring MongoDB backups, ensure you provide the necessary details:

Parameter Type Description
username String Username for database access.
password String Password for database access.
database String Name of the MongoDB database to backup.
host String Host address of the MongoDB database.
port Number Port number for MongoDB connection.

Upload Backup To Google Drive

NodeFullBackup supports seamless integration with Google Drive for automated backup uploads. Implement the following within the afterBackup callback:

import FullBackup, { uploader } from '@double-man/node-full-backup';

const backup = new FullBackup({
    ...
    afterBackup: (filePath) => {
        uploader.googleDrive(path_of_google_key_json, filePath, google_drive_folder_id);
    }
});

backup.start();
Enter fullscreen mode Exit fullscreen mode

Ensure you obtain your Google JSON key as described in the guide and activate Google Drive service to enable automated uploads.

Conclusion

NodeFullBackup offers a robust solution for MongoDB database and file backups, tailored to meet the needs of developers and organizations alike. By integrating NodeFullBackup into your workflow, you can enhance data protection with minimal effort, ensuring your projects are safeguarded against data loss effectively.

Explore our documentation further to learn how NodeFullBackup can strengthen your backup strategy and provide a reliable safety net for your digital assets. Get started today and fortify your data resilience with NodeFullBackup.

Top comments (0)