Operation System : Windows 10

IDE : VsCode

Requirements : a computer, internet, notepad

Book | Tutorial : see links below

Git Project :None

Goal

Create a python program that backs-up a given database. This program should be called at intervals ( daily or weekly or monthly) by the Window task scheduler and should be reusable against different mongodbs.

What I learned

A batch script

This is a file with a “.bat” extension which can be created with a simple text editor like notepad and ran on windows by clicking it. It allows one to run multiple scripts / commands, that you would on the command line or terminal, but in sequence. Thus one can use this during an automation process.

Difference between mongodump and mongoexport

Mongodump

good for backing up a mongo database ( bson and json formats)

Mongoexport

exports the data as json files

Solution

Python program : index.py

from typing import Dict, Tuple
import subprocess
import sys 
import os
import argparse
from datetime import datetime

parser = argparse.ArgumentParser(prog="backup.py", description="MongoDB Backup program in python")
parser.add_argument('--uri', help='Host name and port', required=True)
parser.add_argument('--database', help='Database name', required=True)
parser.add_argument('--username', help='Username', required=True)
parser.add_argument('--password', help='Password', required=True)
parser.add_argument('--outputDir', help='output directory: where the files should go')
args = parser.parse_args()
def readHostDatabaseUsernameAndPassword(argv):
    args = parser.parse_args()
    argsAsDict = vars(args)
    output = argsAsDict.get("outputDir")
    if output == None:
        output = os.path.abspath("./backups_{}_{}".format( argsAsDict.get("database"), datetime.now(tz=None).timestamp()))
    else:
        output = os.path.join(output, "{}_{}".format(argsAsDict.get("database"), datetime.now(tz=None).timestamp()))
    print("All backups are in {}".format(output)) 

    connectToDatabase(argsAsDict.get("uri"), argsAsDict.get("database"), argsAsDict.get("password"),argsAsDict.get("username"), output)
     



def connectToDatabase(uri: str, db:str, password:str, username:str, outputdir:str) -> Tuple:
    if uri == None:
        raise Exception("Uri is required ")
    if db == None:
        raise Exception("Database name is required")
    if password == None:
        raise Exception("Password is required")
    if username == None:
        raise Exception("Username is required")
    print("Running....")
    print("mongodump -h {} -d {} -u {} -p {} -o {}".format(uri, db, username, password, outputdir))
    try:
        subprocess.call("mongodump -h {} -d {} -u {} -p {} -o {}".format(uri, db, username, password, outputdir), shell=True)
    except (RuntimeError, TypeError, NameError):
        print("Something went wrong while connnecting")


if __name__ == "__main__":
    readHostDatabaseUsernameAndPassword(sys.argv[1:])

Batch script file

ECHO "backing up <database name>"
python "C:\....\index.py" --uri <database1 host: database1 port> --username <database1 username> --password <database1 password> --database <database1 name> --outputDir C:\...\backups
PAUSE
python "C:\....\index.py" --uri <database2 host: database2 port> --username <database2 username> --password <database2 password> --database <database2 name> --outputDir C:\...\backups

Do keep in mind that the ECHO and PAUSE commands can be left out. I added the PAUSE because I want to see the terminal open whenever the script runs. With the pause command, the terminal waits till I press enter then close.

Windows Task Scheduler

Follow the instructions below:

  1. Search for “Task Scheduler” ( DE: Aufgabenplanung) and open
  2. Click on create basic task on the right pane
  3. Fill in the name of the task
  4. Click on the trigger tab, click new and set the schedule intended for your script to run. E.g. weekly, what date, what time etc
  5. Click ok
  6. Go to the action tab and click new. Here you set what script should be run.
  7. In the Program/script field,type in the absolute path ( open the directory where your script is and copy the path on the path tree on explorer, then paste in the field)
  8. Since the .bat script needs no argument, skip the arguments field and click ok
  9. Optional : Assuming your db is not local but remote you will require the internet. So you could add a condition which would be “run this script at the scheduled time only when <wlan/wifi name> is functional”
  10. Save
  11. Done!

Author Notes

This is most likely not the cleanest implementation, just a heads up.

Links

  1. typing — Support for type hints
  2. Python Raise an Exception
  3. Copying a Database
  4. Errors and Exceptions
  5. errors – Exceptions raised by the pymongo package
  6. Using % and .format() for great good!
  7. Command Line Arguments
  8. C-style parser for command line options
  9. Parser for command-line options, arguments and sub-commands
  10. How to Write a Batch Script on Windows
  11. How to create an automated task using Task Scheduler on Windows 10
  12. Batch Script
  13. What is difference between mongodump and mongoexport?