Last active
February 24, 2021 04:54
-
-
Save iit2011081/60e59cde32632bdbe61ade32ce1601d1 to your computer and use it in GitHub Desktop.
Mongodb automatic backup script to s3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import subprocess | |
import boto3 | |
from optparse import OptionParser | |
from datetime import datetime | |
DESTINATION_FOLDER = '/tmp/' | |
#Change below variables | |
S3_BUCKET = 'your_s3_bucket_name' | |
S3_FOLDER = 'mongo-backup/' | |
DB_NAME = "your_db_name" | |
DB_HOST = "localhost:27020" | |
DB_PASSWORD = "db_password" | |
DB_USER = "db_user" | |
S3_ACCESS_KEY = 's3_access_key' | |
S3_SECRET_KEY = 's3_secret_key' | |
mongorestore_command = "mongorestore --gzip --archive=mongo-backup-2018-12-04.gz -d your_db_name" #use this command to restore | |
boto3 = boto3.Session( | |
aws_access_key_id = S3_ACCESS_KEY, | |
aws_secret_access_key = S3_SECRET_KEY, | |
) | |
#Install following things to run this script | |
# 1-sudo apt install python3-pip | |
# 2-sudo pip3 install boto3 | |
def write_zip(dry_run=True): | |
now = datetime.now() | |
date = now.strftime("%Y-%m-%d") | |
zip_file = DESTINATION_FOLDER + 'mongo-backup-' + date + '.gz' | |
if (DB_USER == '' and DB_PASSWORD == ''): | |
command = "mongodump -d " + DB_NAME + " -h " + DB_HOST + " --gzip --archive=" + zip_file | |
else: | |
command = "mongodump -d " + DB_NAME + " -h " + DB_HOST + " -u " + DB_USER + " -p '" + DB_PASSWORD + "' --gzip --archive=" + zip_file | |
output = subprocess.call(command,shell = True) | |
upload_to_s3(zip_file, date+'.zip') | |
#delete file from server | |
os.remove(zip_file) | |
remove_old_files(dry_run) | |
def upload_to_s3(source_file_path, destination_file_name): | |
print ("uploading file to s3") | |
s3 = boto3.resource('s3') | |
data = open(source_file_path, 'rb') | |
s3.Bucket(S3_BUCKET).put_object(Key=S3_FOLDER+destination_file_name, Body=data) | |
def delete_from_s3(file_name): | |
print ("deleting file from s3") | |
s3 = boto3.resource('s3') | |
s3.Object(S3_BUCKET,file_name).delete() | |
def remove_old_files(dry_run=True): | |
files_list = [] | |
s3 = boto3.resource('s3') | |
bucket = s3.Bucket(S3_BUCKET) | |
for object in bucket.objects.filter(Prefix=S3_FOLDER): | |
print(object.key) | |
files_list.append(object.key) | |
if len(files_list) <= 3: | |
return | |
print ('list : ', files_list) | |
date_arr = [] | |
for file in files_list: | |
date_str = file[0:-4] | |
date_str = date_str[len(S3_FOLDER):] | |
date_object = datetime.strptime(date_str, '%Y-%m-%d') | |
print (date_object) | |
date_arr.append(date_object) | |
smallest_date_str = get_smallest_date_str(date_arr) | |
oldest_file = S3_FOLDER+smallest_date_str + '.zip' | |
if not dry_run: | |
delete_from_s3(oldest_file) | |
remove_old_files(dry_run) | |
def get_smallest_date_str(arr): | |
s = arr[0] | |
for aa in arr: | |
if aa < s: | |
s = aa | |
return s.strftime("%Y-%m-%d") | |
def run(): | |
write_zip(dry_run=False) | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment