This project is a Node.js script that creates backups of a MySQL database and a specific directory, compresses them, and uploads the resulting files to a Google Drive folder. After uploading, the local files are deleted unless specified otherwise.
- Node.js version: 18.20.3
- Node.js (v18.20.3)
- npm (Node Package Manager)
- Google Cloud project with Drive API enabled
- MySQL database
-
Clone this repository:
git clone https://github.com/pipe-code/strapi-drive-backups.git cd strapi-drive-backups -
Install the required npm packages:
npm install -
Create a .env file in the root of the project and add the following variables:
CLIENT_ID=your_google_client_id CLIENT_SECRET=your_google_client_secret REFRESH_TOKEN=your_google_refresh_token DRIVE_FOLDER_ID=your_google_drive_folder_id DB_NAME=your_database_name DB_USER=your_database_username DB_PASSWORD=your_database_password DIRECTORY_TO_BACKUP=/path/to/your/directory KEEP_LOCAL_BACKUPS=true_or_false
- CLIENT_ID: Your Google API client ID. You can obtain this by creating OAuth 2.0 credentials in the Google Cloud Console.
- CLIENT_SECRET: Your Google API client secret. Obtain this along with the client ID.
- REFRESH_TOKEN: A refresh token to authenticate the application. You can obtain this by authorizing your application to access Google Drive.
- DRIVE_FOLDER_ID: The ID of the Google Drive folder where the backups will be uploaded. You can find this by navigating to the folder in Google Drive and copying the part of the URL after folders/.
- DB_CLIENT:
- DB_NAME: The name of your database.
- DB_USER: The username for your database.
- DB_PASSWORD: The password for your database.
- DIRECTORY_TO_BACKUP: The path to the directory you want to backup.
- KEEP_LOCAL_BACKUPS: Set to true if you want to keep the local files after uploading to Google Drive. Set to false to delete the files after uploading.
Once you have set up your environment variables and installed the necessary packages, you can run the script:
node index.js
This script will:
- Dump the MySQL database.
- Create a tar.gz file of the specified directory.
- Upload both files to Google Drive.
- Delete the local copies of the files unless KEEP_LOCAL_BACKUPS is set to true.
The backups are saved in a directory named backups in the project root. The script generates filenames in the format [YYYYMMDD_HHMMSS]_db.sql and [YYYYMMDD_HHMMSS]_public.tar.gz.
This project is licensed under the MIT License. See the LICENSE file for details.