Skip to content

Instantly share code, notes, and snippets.

@zenatuz
Last active March 8, 2022 14:39
Show Gist options
  • Save zenatuz/70911685d573fe01488f82bd807d5df7 to your computer and use it in GitHub Desktop.
Save zenatuz/70911685d573fe01488f82bd807d5df7 to your computer and use it in GitHub Desktop.
from datetime import datetime,timedelta
from requests.auth import HTTPBasicAuth
import argparse
import json
import os
import requests
import sys
# Getting ENV Variables
ELASTIC_API_URL = os.getenv('ELASTIC_API_URL')
ELASTIC_API_USER = os.getenv('ELASTIC_API_USER')
ELASTIC_API_PASS = os.environ.get('ELASTIC_API_PASS')
# Getting values from parameters
parser = argparse.ArgumentParser()
parser.add_argument("--date", dest="Date", required=False, help = "Specific a day (YYYY-MM-DD) for Backup")
parser.add_argument("--group", dest="Group", required=False, help = "Chose a group name, for example (apm, infra or logstash). Those might change, check the Indexes on Elasticsearch.")
parser.add_argument("--index-name", dest="IndexName", required=False, help = "Define an Index Name")
args = parser.parse_args()
# Printing --help if no parameter provided
if len(sys.argv)==1:
parser.print_help()
parser.exit()
# Aborting if ENV Variables don't exists
if not ELASTIC_API_URL or not ELASTIC_API_USER or not ELASTIC_API_PASS:
print("The ENV Variables should be populated: ELASTIC_API_URL, ELASTIC_API_USER and ELASTIC_API_PASS. ABORTING!")
sys.exit("1")
# Defining backup function
def backup(date,GroupResult,IndexResult):
if IndexResult == False:
# print("Specified Index: % s" % args.IndexName)
print("Specified Index: % s, DOESN'T EXIST. ABORTING!!!" % args.IndexName)
sys.exit("1")
if GroupResult == False:
# print("Specified Group: % s" % args.Group)
print("INVALID GROUP: %s isn't a valid option. ABORTING!!!" % args.Group)
print("You need to choose one of those options:", *groups,end=". \n" )
sys.exit("1")
if args.Date == False:
# print('Specified Date:', date)
print("Specified Date: No date provided, will proced with backup using yesterday date, %s" %yesterday_fmt)
# else:
# print(os.system("/usr/local/bin/elasticdump --version"))
# Getting JSON Indexes from URL
# url = requests.get(ELASTIC_API_URL, auth=HTTPBasicAuth(ELASTIC_API_USER,ELASTIC_API_PASS))
url = "https://gist.githubusercontent.com/zenatuz/d860e8d473169750f539068032e931d0/raw/ff11075d96c19b9cb14e09446157029aecefb2f8/indexes.json"
text = url.text
data = json.loads(text)
indexes = []
for key, value in data.items() :
indexes.append(key)
groups = {}
for i in indexes:
group = i.split("-")[0]
if not group in groups.keys():
groups[group] = []
groups[group].append(i)
# VALIDATIONS
##################################
# Validating Date
if args.Date:
format = "%Y-%m-%d"
res = True
try:
res = bool(datetime.strptime(args.Date, format))
except ValueError:
res = False
print("INVALID DATE FORMAT: %s!!! \nIt should be: YYYY-MM-DD." % args.Date)
print("Aborting!!!")
sys.exit("1")
else:
yesterday = datetime.now() - timedelta(1)
yesterday_fmt=datetime.strftime(yesterday,'%Y-%m-%d')
# Validating Group
if args.Group:
for i in groups:
print("Group:", i)
print("Argumento:", args.Group)
if i == args.Group:
GroupResult = True
else:
GroupResult = False
print(GroupResult)
# # Printing Indexes
# for i in groups:
# print(i)
# print(groups[i])
# Validating Index
if args.IndexName:
for i in groups:
if args.IndexName in groups[i]:
IndexResult = True
break
else:
IndexResult = False
else:
IndexResult = True
# Validating Args
if args.Date and not args.Group:
# ArgsResult = False
print("When a Date is provided, a Groupname should also be provided. ABORTING!")
sys.exit("1")
backup(args.Date,GroupResult,IndexResult)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment