Automate Moodle backup

Create a Shell Script to automate the backup task i name it backup.sh


#!/bin/bash


# Get today's date in DD-MM-YY format
TODAY=$(date +"%d-%m-%y")

MOODLE_IMAGE="backup-moodle-image-$TODAY"
MARIADB_IMAGE="backup-mariadb-image-$TODAY"
BACKUP_DIR="/home/hame/backup/test"
ZIP_NAME="moodle-backup-$TODAY.zip"

echo "Commiting moodle-image..."
docker commit -p backup-mbda_moodle-1 $MOODLE_IMAGE

echo "Saving moodle-image..."
docker save -o ~/backup/test/$MOODLE_IMAGE.tar $MOODLE_IMAGE

echo "Removing moodle-image..."
docker image rm $MOODLE_IMAGE


echo "Commiting mariadb-image..."
docker commit -p backup-mbda_mariadb-1 $MARIADB_IMAGE

echo "Saving mariadb-image..."
docker save -o ~/backup/test/$MARIADB_IMAGE.tar $MARIADB_IMAGE

echo "Removing moodle-image..."
docker image rm $MARIADB_IMAGE
echo "Saving mariadb data..."
docker run --rm -v moodle_mariadb:/bitnami/mariadb -v $BACKUP_DIR:/backup ubuntu tar -czvf /backup/mysql-backup-volume-$TODAY.tar.gz /bitnami/mariadb

echo "Saving moodle_data..."
docker run --rm -v moodle_data:/bitnami/moodle -v $BACKUP_DIR:/backup ubuntu tar -czvf /backup/moodle-data-backup-volume-$TODAY.tar.gz /bitnami/moodle

echo "Saving moodledata_data..."
docker run --rm -v moodledata_data:/bitnami/moodledata -v $BACKUP_DIR:/backup ubuntu tar -czvf /backup/moodledata-data-backup-volume-$TODAY.tar.gz /bitnami/moodledata

echo "Creating a zip file with all the backups..."
zip -j $BACKUP_DIR/$ZIP_NAME $BACKUP_DIR/*.tar $BACKUP_DIR/*.gz

echo "Deleting all files except the zip..."
find $BACKUP_DIR -type f ! -name "$ZIP_NAME" -delete

echo "Backup completed and saved as $ZIP_NAME"

The above code highlighted in Red color should be name accordingly to the container and volume name of yours

*(To check your container name run this command: "docker ps" there you will get the name of the container of both moodle and mariadb) 

*(To Check your volume name run this command: "docker volume ls" there you will get the name of the volume of both moodle and mariadb example moodle_data and moodledata_data are moodle volume & mariadb_data is a mariadb volume)


Step 1: Edit the Crontab to automate when the script should run automatically

Open the crontab file using the command:

crontab -e

Step 2: Add the Cron Job

Add the following line to schedule the script to run at 4:00 PM daily:

bash
0 16 * * * /path/to/your/backup.sh

Comments

Popular posts from this blog

Moodle Backup and Restore in Docker

Custom Domain to GitHub Pages (Hostinger Edition)

Resizing partition in AWS