This script allows you to perform a backup of your MongoDB database and create a password-protected zip file for the backup. It uses the mongodump
command to create the backup and the zip
command to compress the backup into a zip file.
- MongoDB should be installed and running on your system.
- Ensure that you have the necessary permissions to access the MongoDB database.
-
Open a terminal or command prompt.
-
Make sure you have execute permissions for the script. If not, you can grant the permissions using the following command:
chmod +x backup.sh
-
Modify the script variables according to your MongoDB setup:
-
MONGO_HOST
: The hostname or IP address of your MongoDB server. By default, it is set to "localhost". -
MONGO_PORT
: The port on which MongoDB is running. By default, it is set to "27020". -
MONGO_DATABASE
: The name of the database you want to backup. Modify this according to your database name. By default, it is set to "test". -
BACKUP_DIR
: The directory where you want to store the backup files. Modify this to specify the desired directory path. By default, it is set to "/home/username/mongo_backup/backups". -
ZIP_PASSWORD
: The password to protect the zip file. Modify this to set your desired password. By default, it is set to "password". -
Note: If your database has authentication, enter the username and password and update the mongodump command:
-
MONGO_USERNAME
: The username to authenticate with MongoDB. Modify this if your MongoDB requires authentication. By default, it is set to "test". -
MONGO_PASSWORD
: The password to authenticate with MongoDB. Modify this if your MongoDB requires authentication. By default, it is set to "test".
-
-
-
Run the script using the following command:
./backup.sh
-
The script will perform the following steps:
- Connect to the MongoDB server specified by
MONGO_HOST
andMONGO_PORT
. - Authenticate with the MongoDB server using the provided
MONGO_USERNAME
andMONGO_PASSWORD
. - Perform a backup of the specified
MONGO_DATABASE
using themongodump
command. - Compress the backup into a zip file using the
zip
command with the specifiedZIP_PASSWORD
. - Remove the temporary backup directory created during the process.
- Connect to the MongoDB server specified by
-
Once the backup is completed, the script will display the path to the created zip file.
To schedule regular backups, you can use the cron utility on Linux systems. Cron allows you to automate the execution of tasks at specified intervals. Here's an example of how you can set up a cron job to run the backup script every 3 hours:
-
Open the cron configuration file by running the following command:
crontab -e
-
If prompted, choose an editor to edit the crontab file.
-
Add the following line to the file:
0 */3 * * * /path/to/backup.sh
Replace
/path/to/backup.sh
with the actual path to the script on your system.This cron schedule expression will run the script every 3 hours, at the top of the hour (e.g., 1:00 AM, 4:00 AM, 7:00 AM, etc.).
-
Save the file and exit the editor.
The cron job is now set up, and the backup script will be executed automatically at the specified intervals. Make sure to review and update the script variables according to your