Full Backup in Nodejs(Files and Database backup)
Tags: javascript, webdev, tutorial, node
Published: 2024-06-16 07:08:01

[NodeFullBackup](https://github.com/hosseinmobarakian/node-full-backup) 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](https://github.com/hosseinmobarakian/node-full-backup) is quick and easy. Simply install the package via npm using the following command:

```bash
npm i @double-man/node-full-backup
```

### Simple Usage

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

```javascript
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();
```

> Your support helps this [repository](https://github.com/hosseinmobarakian/node-full-backup) 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:

```javascript
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();
```

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.