Giter Site home page Giter Site logo

strapi-db-sqlite-cloud-backup's Introduction

strapi-db-sqlite-cloud-backup

Script for scheduled backup to Yandex Drive or Google Drive of a temporary copy of the Strapi SQLite database.

Installation

To use this package, you need to install it in your project:

npm install strapi-db-sqlite-cloud-backup

Initialization

First, you need to initialize the backup configuration by creating a .env file with the necessary environment variables. You can do this using the init command provided by the package:

npx strapi-db-sqlite-cloud-backup init

This will create a .env file in the root of your project. Fill in the required values:

GOOGLE_DRIVE_FOLDER_ID=your-google-drive-folder-id
GOOGLE_CREDENTIALS_PATH=./path/to/google-credentials.json
GOOGLE_DRIVE_ENABLED=false

YANDEX_TOKEN=your-yandex-o-auth-token
YANDEX_DISK_ENABLED=true
YANDEX_BACKUP_PATH=/backups

DB_PATH=../backend/.tmp
DB_NAME=data.db

This will also create an ecosystem.config.js file and fill it with that:

module.exports = {
  apps: [
    {
      name: 'backup',
      script: 'node_modules/strapi-db-sqlite-cloud-backup/backup.js',
      args: 'run',
      cron_restart: '0 */3 * * *', // Run every 3 hours
      watch: false,
      env: {
        NODE_ENV: 'production'
      }
    }
  ]
};

Change the backup timeout value if needed.

Usage

To run the backup script manually, use the run command:

npx strapi-db-sqlite-cloud-backup run

This command will perform a one-time backup of the database according to the steps outlined below.

Workflow

The logic that this script follows includes several key steps:

  1. Initialization of configurations and logger:

    • Environment variables are loaded from the .env file.
    • A logger is set up using the winston library.
  2. Defining paths:

    • Paths to the database, backup directory, and log files are determined.
  3. Creating a database backup:

    • The backup directory is created if it does not exist.
    • The database file is copied to the backup directory.
    • A log message is recorded about the creation of the backup.
  4. Uploading the backup to cloud storage (Google Drive and Yandex Disk):

    • If the environment variables GOOGLE_DRIVE_ENABLED and YANDEX_DISK_ENABLED are set to true, the backup is uploaded to the respective cloud storage services.
    • Log messages about the process and the result of the upload is recorded.
  5. Managing the number of backups:

    • The number of backups in the backup directory is checked.
    • If the number of backups exceeds the set limit (MAX_BACKUPS), the oldest backups are deleted.
    • Log messages about the process and result of deleting old backups are recorded.
  6. Starting the script via the pm2 process manager:

  7. To start the script at scheduled intervals and ensure it restarts in case of failures, you can use the pm2 process manager. Create an ecosystem.config.js file in your project root with the following content:

module.exports = {
  apps: [
    {
      name: 'backup',
      script: 'node_modules/strapi-db-sqlite-cloud-backup/backup.js',
      cron_restart: '0 */3 * * *', // Run every 3 hours
      watch: false,
      env: {
        NODE_ENV: 'production',
        // Environment variables for the script
        GOOGLE_DRIVE_ENABLED: process.env.GOOGLE_DRIVE_ENABLED,
        YANDEX_DISK_ENABLED: process.env.YANDEX_DISK_ENABLED,
        GOOGLE_DRIVE_FOLDER_ID: process.env.GOOGLE_DRIVE_FOLDER_ID,
        GOOGLE_CREDENTIALS_PATH: process.env.GOOGLE_CREDENTIALS_PATH,
        YANDEX_TOKEN: process.env.YANDEX_TOKEN,
        YANDEX_BACKUP_PATH: process.env.YANDEX_BACKUP_PATH,
        DB_PATH: process.env.DB_PATH,
        DB_NAME: process.env.DB_NAME
      }
    }
  ]
};

Then start the script using pm2:

pm2 start ecosystem.config.js

By following these steps, you can set up scheduled backups of your Strapi SQLite database to either Google Drive or Yandex Disk.

strapi-db-sqlite-cloud-backup's People

Contributors

champernet avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.