Skip to content

Instantly share code, notes, and snippets.

@mr-exception
Created November 10, 2017 11:51
Show Gist options
  • Save mr-exception/9c6acff536bbba897e1b2bba11acd1ea to your computer and use it in GitHub Desktop.
Save mr-exception/9c6acff536bbba897e1b2bba11acd1ea to your computer and use it in GitHub Desktop.
backup from mongodb database by mongodump in python 3
import os
import time
'''
mongo backup by python
developrt: mr-exception
github: mr-exception
'''
# configs:
interval_m = 5
outputs_dir = '/home/tina/Documents/python/mongo-backup/'
host = "NA" # if host is your local machine leave it NA
port = "NA" # if mongo is on default port (37017) leave in NA
username = "NA" # if there is no username set, leave it in NA
password = "NA" # if there is no password set, leave it in NA
def render_output_locations():
return outputs_dir + time.strftime("%d-%m-%Y-%H:%M:%S")
def run_backup():
command = "mongodump"
if host != 'NA':
command += " --host " + host
if port != 'NA':
command += " --port " + port
if username != 'NA':
command += " --username " + username
if password != 'NA':
command += " --password " + password
command += " --out " + render_output_locations()
os.system(command)
print("mongo backup progress started")
print("I will backup your mongo db every {0} minutes".format(interval_m))
while True:
time.sleep(interval_m * 60)
run_backup()
@masikh
Copy link

masikh commented Jan 22, 2019

port = "NA" # if mongo is on default port (37017) leave in NA -> port = "NA" # if mongo is on default port (27017) leave in NA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment