Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Version History

Version 1 Current »

To create the backups, you will need to create a file in ktt:

/home/ktt/mongo/mongoBackup.sh
chmod +x mongoBackup.sh

And then past next code:

#!/bin/bash

# variables
MONGO_HOST="localhost"
MONGO_PORT="27017"
MONGO_DB1="db1"
MONGO_DB_OTHERS="dbOthers"
MONGO_USER="<mongo-user>"
MONGO_PWD="<mongo-pwd>"
BACKUP_DIR="/home/ktt/mongoBackups"
BACKUP_FILE1="$(date +%Y-%m-%d-%H-%M)_$1-MOH-ZWE-MONGO-db1.gz"
BACKUP_FILE2="$(date +%Y-%m-%d-%H-%M)_$1-MOH-ZWE-MONGO-dbOthers.gz"
REMOTE_SERVER="<remote-user>@<remote-ip>:/backups/vmmc"
REMOTE_PWD='<remote-pwd>'


# Step1: mongodump files
mongodump --host $MONGO_HOST --port $MONGO_PORT --username $MONGO_USER --password $MONGO_PWD --authenticationDatabase admin  --db $MONGO_DB1 --archive=$BACKUP_DIR/$BACKUP_FILE1 --gzip
mongodump --host $MONGO_HOST --port $MONGO_PORT --username $MONGO_USER --password $MONGO_PWD --authenticationDatabase admin  --db $MONGO_DB_OTHERS --archive=$BACKUP_DIR/$BACKUP_FILE2 --gzip


if [ $? -eq 0 ]; then
    echo "created correctly the backups $BACKUP_DIR/$BACKUP_FILE1"
else
    echo "Error creating bakcups"
    exit 1
fi

# step 2: Encrypt files

# step 3: send ifles to remote server
#scp -i $REMOTE_SERVER_PPK $BACKUP_DIR/$ENCRYPTED_FILE $REMOTE_SERVER
sshpass -p $REMOTE_PWD scp $BACKUP_DIR/$BACKUP_FILE1 $REMOTE_SERVER
sshpass -p $REMOTE_PWD scp $BACKUP_DIR/$BACKUP_FILE2 $REMOTE_SERVER

if [ $? -eq 0 ]; then
    echo "Files sent correctly"
else
    echo "Error sending files to remote server"
    exit 1
fi

# Step 4: Drop files
#rm $BACKUP_DIR/$BACKUP_FILE1
#rm $BACKUP_DIR/$BACKUP_FILE2
#rm $BACKUP_DIR/$ENCRYPTED_FILE1

echo "Temporal files removed correctly"

Remember, change <mongo-user>, <mongo-pwd>, <remote-user>, <remote-pwd> and <remote-ip>. Contact with the admin to know the values.

And finally, create a scheduler in the host. To do it, execute this command:

crontab -e

An editor will open with the created schedulers, so we only have to add the following lines to the end of the file:

0 1 * * * /home/ktt/mongoBackup.sh d >> /home/ktt/mongoBackups/cronD.log 2>&1
0 1 * * 1 /home/ktt/mongoBackup.sh w >> /home/ktt/mongoBackups/cronW.log 2>&1
0 1 1 * * /home/ktt/mongoBackup.sh m >> /home/ktt/mongoBackups/cronM.log 2>&1
  • No labels