def awsmysqlclone(dict_source_ttSettings, dict_target_ttSettings, source_hostname, target_hostname, my_logger, log_file): my_logger.info("In awsmysqlclone()...") #print "awsmysqlclone start" drop_args="mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] \ + " --ssl-ca=" + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 -e \"DROP DATABASE IF EXISTS ebdb\"" #mysql --user="******"mysql"]["dba"]["username"] + " -p" + dict_ttSettings["mysql"]["dba"]["password"] + " --force --ssl-ca=" + aws_cert+ " --host=" + target_hostname + " --port=3306 -e \"DROP DATABASE IF EXISTS ebdb\"" #print drop_args try: clone_drop_out=callSubprocess(drop_args) except: e = str(sys.exc_info()) my_logger.info("Error encountered in awsmysqlclone_drop table: %s. Exiting!" %e) print("Error encountered. Exiting! Logfile location: %s" %log_file) sys.exit(-1) #print "drop success" my_logger.info("Drop database success!") create_args="mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] \ + " --ssl-ca=" + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 -e \"CREATE DATABASE IF NOT EXISTS ebdb\"" #print create_args try: clone_create_out=callSubprocess(create_args) except: e = str(sys.exc_info()) my_logger.info("Error encountered in awsmysqlclone_create table: %s. Exiting!" %e) print("Error encountered. Exiting! Logfile location: %s" %log_file) sys.exit(-1) #print "create success" my_logger.info("Create database success! Output: %s"%clone_create_out) priv_args1 = "mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] + " --ssl-ca=" \ + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + \ " --port=3306 -e \"USE ebdb; GRANT SELECT, LOCK TABLES, SHOW VIEW, EVENT, TRIGGER ON *.* TO 'replnyc'@'%' IDENTIFIED BY \'" + dict_source_ttSettings["mysql"]["users"]["r"]["password"] + "\'; FLUSH PRIVILEGES;\" " #print priv_args1 try: clone_priv_out = callSubprocess(priv_args1) except: e = str(sys.exc_info()) my_logger.info("Error encountered in awsmysqlclone_grant privileges: %s. Exiting!" %e) print("Error encountered. Exiting! Logfile location: %s" %log_file) sys.exit(-1) #print "grant success" my_logger.info( "Grant privileges success!") final_clone="mysqldump --user=replnyc -p" + dict_source_ttSettings["mysql"]["users"]["r"]["password"] + " --ssl-ca=" + os.path.expanduser(dict_source_ttSettings["mysql"]["certs"]["public"]) \ + " --host=" + source_hostname + " --port=3306 --single-transaction=TRUE ebdb | mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" \ + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] + " --ssl-ca=" + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 ebdb" #print final_clone #final_clone_out=callSubprocess(final_clone) try: os.system(final_clone) except: e = str(sys.exc_info()) my_logger.info("Error Encountered %s. Exiting...."%e) print("Error encountered awsmysqlclone_final_clone. Exiting! Logfile location: %s" %log_file) sys.exit(-1) #print "clone success" my_logger.info("MySQL Clone success!") return("Returning to mysql_operations()")
def awsmysqlclone(dict_source_ttSettings, dict_target_ttSettings, source_hostname, target_hostname, my_logger, log_file): my_logger.info("In awsmysqlclone()...") drop_args="mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] \ + " --ssl-ca=" + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 -e \"DROP DATABASE IF EXISTS ebdb\"" try: clone_drop_out = callSubprocess(drop_args) except: e = str(sys.exc_info()) my_logger.info( "Error encountered in awsmysqlclone_drop table: %s. Exiting!" % e) print("Error encountered. Exiting! Logfile location: %s" % log_file) sys.exit(-1) my_logger.info("Drop database success!") create_args="mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] \ + " --ssl-ca=" + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 -e \"CREATE DATABASE IF NOT EXISTS ebdb\"" try: clone_create_out = callSubprocess(create_args) except: my_logger.info( "Error encountered in awsmysqlclone_create table. Exiting!") print("Error encountered. Exiting! Logfile location: %s" % log_file) sys.exit(-1) my_logger.info("Create database success! Output: %s" % clone_create_out) priv_args1 = "mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] + " --ssl-ca=" \ + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + \ " --port=3306 -e \"USE ebdb; GRANT SELECT, LOCK TABLES, SHOW VIEW, EVENT, TRIGGER ON *.* TO 'replnyc'@'%' IDENTIFIED BY \'" + dict_source_ttSettings["mysql"]["users"]["r"]["password"] + "\'; FLUSH PRIVILEGES;\" " try: clone_priv_out = callSubprocess(priv_args1) except: my_logger.error( "Error encountered in awsmysqlclone_grant privileges. Exiting!") sys.exit( "Error encountered in awsmysqlclone_grant privileges. Exiting! Logfile location: %s" % log_file) my_logger.info("Grant privileges success!") final_clone="mysqldump --user=replnyc -p" + dict_source_ttSettings["mysql"]["users"]["r"]["password"] + " --ssl-ca=" + os.path.expanduser(dict_source_ttSettings["mysql"]["certs"]["public"]) \ + " --host=" + source_hostname + " --port=3306 --single-transaction=TRUE ebdb | mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" \ + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] + " --ssl-ca=" + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 ebdb" try: os.system(final_clone) except: my_logger.error( "Error encountered awsmysqlclone_final_clone. Exiting! Logfile location: %s" % log_file) sys.exit( "Error encountered awsmysqlclone_final_clone. Exiting! Logfile location: %s" % log_file) my_logger.info("MySQL Clone success!") return ("Returning to mysql_operations()")
def validate_mysql_source(dict_ttSettings, source_hostname): try: aws_cert = dict_ttSettings["mysql"]["certs"]["public"] echo_OP=callSubprocess("echo $USER") val_s_cmd="mysql --user="******"mysql"]["users"]["r"]["username"] + " -p" + dict_ttSettings["mysql"]["users"]["r"]["password"] \ + " --ssl-ca=" + aws_cert + " --host=" + source_hostname + " --port=3306 -e \"CONNECT;\"" val_s_cmd_out=callSubprocess(val_s_cmd) except: my_logger.error("Encountered Error in validate_mysql_source(): %s. Exiting!") sys.exit("Encountered Error in validate_mysql_source(): %s. Exiting!") my_logger.debug("Accounts validation successful for service: MySQL source with output: %s"%val_s_cmd_out) return("Accounts validation successful for service: MySQL source with output: %s"%val_s_cmd_out)
def mysqldump(suffix, dict_ttSettings, hostname, my_logger, log_file, backup_dir): my_logger.debug("Received suffix: %s" % suffix) try: my_logger.info("In mysqldump_source()...") dump_cmd="mysqldump --user="******"mysql"]["users"]["r"]["username"] + " -p" + dict_ttSettings["mysql"]["users"]["r"]["password"] \ + " --ssl-ca=" + os.path.expanduser(dict_ttSettings["mysql"]["certs"]["public"]) + " --host=" + hostname + " --port=3306 --single-transaction=TRUE --routines --events ebdb" if ('source' in suffix) or ('target' in suffix): filename = suffix + "-ebdb" filepath = os.path.join(swapTimeStampDirPath, filename) elif ('backup' in suffix): filename = "ebdb.sql" filepath = backup_dir + "/" + filename my_logger.info("Create file success! %s " % filename) my_logger.info("Backup file path: %s " % filepath) dump_out = callSubprocess(dump_cmd) f = open(filepath, "wb") f.write(dump_out) f.close() my_logger.info("Backup successful!") except: my_logger.info("Encountered Error in mysqldump(). Exiting!") sys.exit("Encountered Error in mysqldump(). Exiting!") my_logger.debug("mysqldump() %s for %s successful! Backup path: %s" % (suffix, hostname, filepath))
def mongo_restore(t_host, dict_target_ttSettings, my_logger, log_file, restore_dir): l_host = "skanupuru-lxu1.nyc.3top.com" my_logger.debug(" In mongoclone()...") try: filename = "apis_cache" filepath = os.path.join(restore_dir, filename) #my_logger.debug("Create file success! %s " % filename) #f=open(filepath, "wb") #f.write("printjson(db = db.getSiblingDB('apis_cache'));") #f.write("\nprintjson(db.getCollectionNames());") #f.write("\nprintjson(db.dropDatabase());") #f.write("\nprintjson(db.copyDatabase (\"%s\", \"apis_cache\"));"\ # %(filepath+"/apis_cache")) #f.write("\nprintjson(db.getCollectionNames());") #f.close() clone_cmd="mongorestore --username " + dict_target_ttSettings["mongodb"]["users"]["dba"]["username"] + " -p " + dict_target_ttSettings["mongodb"]["users"]["dba"]["password"] \ + " --host " + t_host + " --verbose --port 27017 " + filepath print clone_cmd clone_OP2 = callSubprocess(clone_cmd) except: my_logger.error("Error encountered in mongoclone(). Exiting! ") sys.exit("Error encountered in mongoclone(). Exiting! ") my_logger.info("Mongoclone() process completed successfully!")
def mongo_restore(t_host, dict_target_ttSettings, my_logger,log_file, restore_dir): l_host = "skanupuru-lxu1.nyc.3top.com" my_logger.debug(" In mongoclone()...") try: filename="apis_cache" filepath=os.path.join(restore_dir, filename) #my_logger.debug("Create file success! %s " % filename) #f=open(filepath, "wb") #f.write("printjson(db = db.getSiblingDB('apis_cache'));") #f.write("\nprintjson(db.getCollectionNames());") #f.write("\nprintjson(db.dropDatabase());") #f.write("\nprintjson(db.copyDatabase (\"%s\", \"apis_cache\"));"\ # %(filepath+"/apis_cache")) #f.write("\nprintjson(db.getCollectionNames());") #f.close() clone_cmd="mongorestore --username " + dict_target_ttSettings["mongodb"]["users"]["dba"]["username"] + " -p " + dict_target_ttSettings["mongodb"]["users"]["dba"]["password"] \ + " --host " + t_host + " --verbose --port 27017 " + filepath print clone_cmd clone_OP2=callSubprocess(clone_cmd) except: my_logger.error("Error encountered in mongoclone(). Exiting! ") sys.exit("Error encountered in mongoclone(). Exiting! ") my_logger.info("Mongoclone() process completed successfully!")
def mysqldump_target(suffix, dict_ttSettings, target_hostname, my_logger, log_file): try: my_logger.info("In mysqldump()...") dump_cmd = ( "mysqldump --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_ttSettings["mysql"]["users"]["dba"]["password"] + " --ssl-ca=" + os.path.expanduser(dict_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 --single-transaction=TRUE --routines --events ebdb" ) filename = suffix + "-ebdb.sql" filepath = os.path.join(swapTimeStampDirPath, filename) my_logger.info("Create file success! %s " % filename) dump_out = callSubprocess(dump_cmd) f = open(filepath, "wb") f.write(dump_out) f.close() my_logger.info("Backup successful!") except: my_logger.info("Encountered Error in mysqldump_target(). Exiting!") sys.exit("Encountered Error in mysqldump_target(). Exiting!") my_logger.debug("mysqldump_target() %s for %s successful! Backup path: %s" % (suffix, target_hostname, filepath)) return "Returning to mysql_operations"
def validate_mysql_nyc_accounts(dict_ttSettings, target_hostname): my_logger.debug("validating mysql_nyc_accounts()...") for k, v in dict_ttSettings["mysql"]["users"].items(): try: my_logger.debug("Validating mysql nyc %s account for %s" % (target_hostname, k)) conn_cmd = ( "mysql --user="******"username"] + " -p" + v["password"] + " --host=" + target_hostname + ' --port=3306 -e "CONNECT;"' ) conn_out = callSubprocess(conn_cmd) except: my_logger.error( "Validation of mysql_nyc_accounts() failed for service:%s, username: %s, hostname: %s" % (k, v["username"], target_hostname) ) sys.exit( "Validation of mysql_nyc_accounts() failed for service:%s, username: %s, hostname: %s" % (k, v["username"], target_hostname) ) my_logger.debug( "Validation of mysql_nyc_accounts() was successful for service:%s, username: %s, hostname: %s" % (k, v["username"], target_hostname) ) my_logger.debug("Accounts validation successful for service: MySQL !")
def collect_checksum(dict_ttSettings, hostname): aws_cert = dict_ttSettings["mysql"]["certs"]["public"] """GET LIST OF TABLES""" list_tables_cmd = "mysql --user="******"mysql"]["users"]["r"][ "username"] + " -p" + dict_ttSettings["mysql"]["users"]["r"][ "password"] + " --ssl-ca=" + os.path.expanduser( dict_ttSettings["mysql"]["certs"]["public"] ) + " --host=" + hostname + " --port=3306 -e \"show tables from ebdb;\"" """ conn_cmd="mysql --user="******"username"] + " -p" + v["password"] + " --ssl-ca=" + aws_cert + " --host=" + host + " --port=3306 -e \"CONNECT;\"" """ list_tables_out = callSubprocess(list_tables_cmd) #print list_tables_out list_tables = list_tables_out.split("\n") checksum_list = list() len_tables = len(list_tables) dict_checksum = dict() for i in range(len_tables - 1): if len(list_tables[i + 1]) != 0: table_name = list_tables[i + 1] #print "%s. Table_name: %s"%(i+1, table_name) checksum_cmd = "mysql --user="******"mysql"]["users"][ "r"]["username"] + " -p" + dict_ttSettings["mysql"]["users"][ "r"]["password"] + " --ssl-ca=" + os.path.expanduser( dict_ttSettings["mysql"]["certs"]["public"] ) + " --host=" + hostname + " --port=3306 -e \"checksum table ebdb." + table_name + ";\"" checksum_out = callSubprocess(checksum_cmd) checksum1 = (checksum_out.split("\t")) checksum2 = list() #ict_checksum = dict() for k in checksum1: #print "k: %s"%k if "\n" in k: for l in k.split("\n"): if len(l) != 0: checksum2.append(l) else: checksum2.append(k) for j in checksum2: if ("Table" not in j) and ("Checksum" not in j) and (table_name not in j): checksum = int(j) dict_checksum[table_name] = checksum return dict_checksum
def validate_mysql_source(dict_ttSettings, source_hostname): try: aws_cert = dict_ttSettings["mysql"]["certs"]["public"] echo_OP = callSubprocess("echo $USER") val_s_cmd="mysql --user="******"mysql"]["users"]["r"]["username"] + " -p" + dict_ttSettings["mysql"]["users"]["r"]["password"] \ + " --ssl-ca=" + aws_cert + " --host=" + source_hostname + " --port=3306 -e \"CONNECT;\"" val_s_cmd_out = callSubprocess(val_s_cmd) except: my_logger.error( "Encountered Error in validate_mysql_source(): %s. Exiting!") sys.exit("Encountered Error in validate_mysql_source(): %s. Exiting!") my_logger.debug( "Accounts validation successful for service: MySQL source with output: %s" % val_s_cmd_out) return ( "Accounts validation successful for service: MySQL source with output: %s" % val_s_cmd_out)
def validate_mongodb_source_accounts(s_host, dict_ttSettings): my_logger.debug("In validate_mongodb_source_accounts()...") try: conn_cmd="mongo " + s_host + ":27017/apis_cache -u "+ dict_ttSettings["mongodb"]["users"]["rw"]["username"] \ + " -p " + dict_ttSettings["mongodb"]["users"]["rw"]["password"] + " --eval \"printjson(db.getCollectionNames());\"" conn_OP=callSubprocess(conn_cmd) except: my_logger.error("Validating mongodb_source_accounts failed. Exiting!") sys.exit("Validating mongodb_source_accounts failed. Exiting!") my_logger.debug("Source accounts validation successful for service: MongoDB !")
def validate_mongodb_target_accounts(t_host, dict_ttSettings): my_logger.debug("validating mongodb_target_accounts()...") try: conn_cmd="mongo " + t_host + ":27017/admin -u " + dict_ttSettings["mongodb"]["users"]["dba"]["username"] + " -p " \ + dict_ttSettings["mongodb"]["users"]["dba"]["password"] + " --eval \"printjson(db.getCollectionNames());\"" conn_OP=callSubprocess(conn_cmd) except: my_logger.error("Validating mongodb_target_accounts failed. Exiting!") sys.exit("Validating mongodb_target_accounts failed. Exiting!") my_logger.debug("Target accounts validation successful for service: MongoDB !")
def validate_mongodb_source_accounts(s_host, dict_ttSettings): my_logger.debug("In validate_mongodb_source_accounts()...") try: conn_cmd="mongo " + s_host + ":27017/apis_cache -u "+ dict_ttSettings["mongodb"]["users"]["rw"]["username"] \ + " -p " + dict_ttSettings["mongodb"]["users"]["rw"]["password"] + " --eval \"printjson(db.getCollectionNames());\"" conn_OP = callSubprocess(conn_cmd) except: my_logger.error("Validating mongodb_source_accounts failed. Exiting!") sys.exit("Validating mongodb_source_accounts failed. Exiting!") my_logger.debug( "Source accounts validation successful for service: MongoDB !")
def validate_mongodb_target_accounts(t_host, dict_ttSettings): my_logger.debug("validating mongodb_target_accounts()...") try: conn_cmd="mongo " + t_host + ":27017/admin -u " + dict_ttSettings["mongodb"]["users"]["dba"]["username"] + " -p " \ + dict_ttSettings["mongodb"]["users"]["dba"]["password"] + " --eval \"printjson(db.getCollectionNames());\"" conn_OP = callSubprocess(conn_cmd) except: my_logger.error("Validating mongodb_target_accounts failed. Exiting!") sys.exit("Validating mongodb_target_accounts failed. Exiting!") my_logger.debug( "Target accounts validation successful for service: MongoDB !")
def validate_mysql_accounts(dict_ttSettings, host): print host aws_cert = dict_ttSettings["mysql"]["certs"]["public"] for k,v in dict_ttSettings["mysql"]["users"].items(): my_logger.debug("Validating mysql aws %s account for %s"%(host, k)) conn_cmd="mysql --user="******"username"] + " -p" + v["password"] + " --ssl-ca=" + aws_cert + " --host=" + host + " --port=3306 -e \"CONNECT;\"" my_logger.debug("Conn_cmd: %s" %conn_cmd) print v['username'], conn_out=callSubprocess(conn_cmd) print "validated!" my_logger.debug("mysql aws account for %s validated successfully for %s!"%(k, host))
def validate_mysql_aws_accounts(dict_ttSettings, target_hostname): aws_cert = dict_ttSettings["mysql"]["certs"]["public"] for k,v in dict_ttSettings["mysql"]["users"].items(): try: my_logger.debug("Validating mysql aws %s account for %s"%(target_hostname, k)) conn_cmd="mysql --user="******"username"] + " -p" + v["password"] + " --ssl-ca=" + aws_cert + " --host=" + target_hostname + " --port=3306 -e \"CONNECT;\"" conn_out=callSubprocess(conn_cmd) except: my_logger.error("Error Encountered in validate_mysql_aws_accounts(): %s. Exiting!") sys.exit("Error Encountered in validate_mysql_aws_accounts(): %s. Exiting!") my_logger.debug("mysql aws account for %s validated successfully for %s!"%(k, target_hostname))
def collect_checksum(dict_ttSettings, hostname): aws_cert = dict_ttSettings["mysql"]["certs"]["public"] """GET LIST OF TABLES""" list_tables_cmd ="mysql --user="******"mysql"]["users"]["r"]["username"] + " -p" + dict_ttSettings["mysql"]["users"]["r"]["password"] + " --ssl-ca=" + os.path.expanduser(dict_ttSettings["mysql"]["certs"]["public"]) + " --host=" + hostname + " --port=3306 -e \"show tables from ebdb;\"" """ conn_cmd="mysql --user="******"username"] + " -p" + v["password"] + " --ssl-ca=" + aws_cert + " --host=" + host + " --port=3306 -e \"CONNECT;\"" """ list_tables_out=callSubprocess(list_tables_cmd) #print list_tables_out list_tables = list_tables_out.split("\n") checksum_list = list() len_tables = len(list_tables) dict_checksum = dict() for i in range(len_tables-1): if len(list_tables[i+1]) != 0: table_name = list_tables[i+1] #print "%s. Table_name: %s"%(i+1, table_name) checksum_cmd = "mysql --user="******"mysql"]["users"]["r"]["username"] + " -p" + dict_ttSettings["mysql"]["users"]["r"]["password"] + " --ssl-ca=" + os.path.expanduser(dict_ttSettings["mysql"]["certs"]["public"]) + " --host=" + hostname + " --port=3306 -e \"checksum table ebdb." + table_name + ";\"" checksum_out = callSubprocess(checksum_cmd) checksum1 = (checksum_out.split("\t")) checksum2 = list() #ict_checksum = dict() for k in checksum1: #print "k: %s"%k if "\n" in k: for l in k.split("\n"): if len(l) != 0: checksum2.append(l) else: checksum2.append(k) for j in checksum2: if ("Table" not in j) and ("Checksum" not in j) and (table_name not in j): checksum = int(j) dict_checksum[table_name] = checksum return dict_checksum
def validate_mysql_accounts(dict_ttSettings, host): print host aws_cert = dict_ttSettings["mysql"]["certs"]["public"] for k, v in dict_ttSettings["mysql"]["users"].items(): my_logger.debug("Validating mysql aws %s account for %s" % (host, k)) conn_cmd = "mysql --user="******"username"] + " -p" + v[ "password"] + " --ssl-ca=" + aws_cert + " --host=" + host + " --port=3306 -e \"CONNECT;\"" my_logger.debug("Conn_cmd: %s" % conn_cmd) print v['username'], conn_out = callSubprocess(conn_cmd) print "validated!" my_logger.debug( "mysql aws account for %s validated successfully for %s!" % (k, host))
def mongodump_target(suffix, dict_target_ttSettings, t_host, my_logger, log_file): my_logger.debug("In mongodump_target()...") try: filename="mongodb-" + suffix filepath=os.path.join(swapTimeStampDirPath,filename) my_logger.debug("Create file success! %s " % filepath) dump_cmd="mongodump --host " + t_host + " --port 27017 --username "+ dict_target_ttSettings["mongodb"]["users"]["dba"]["username"] + " --password " \ + dict_target_ttSettings["mongodb"]["users"]["dba"]["password"] + " --out " + filepath """ mongodump --host mongodb.b.prod.aws.3top.com:27017 --username superuser1 --password HnFBg67tZJ0NZvPC33YvM2hy5H1Sh48mwEe --out mongodump/ """ dump_OP=callSubprocess(dump_cmd) except: my_logger.error("Error encountered in mongodump_target(). Exiting! ") sys.exit("Error encountered in mongodump_target(). Exiting! ") my_logger.info("Mongodump() process completed successfully!")
def validate_mongodb_accounts(dict_ttSettings, hostname): my_logger.debug("validating mongodb_accounts()...") for k, v in dict_ttSettings["mongodb"]["users"].items(): my_logger.debug("Validating %s account for host: %s"%(k, hostname)) try: print("Mongodb account: %s for host: %s"%(k,hostname)), conn_cmd="mongo " + hostname + ":27017/" + v["database"] + " -u " + v["username"] + " -p " \ + v["password"] + " --eval \"printjson(db.getCollectionNames());\"" #print "Running command: %s"%conn_cmd, conn_OP=callSubprocess(conn_cmd) my_logger.debug("Received output: %s"%conn_OP) my_logger.debug("Account: %s for host: %s...validated"%(k, hostname)) print "...Validated!" except: my_logger.error("Validating mongodb_accounts failed. Exiting!") sys.exit("Validating mongodb_accounts failed. Exiting!") my_logger.debug("Accounts validation successful for service: MongoDB !")
def mongodump_target(suffix, dict_target_ttSettings, t_host, my_logger, log_file): my_logger.debug("In mongodump_target()...") try: filename = "mongodb-" + suffix filepath = os.path.join(swapTimeStampDirPath, filename) my_logger.debug("Create file success! %s " % filepath) dump_cmd="mongodump --host " + t_host + " --port 27017 --username "+ dict_target_ttSettings["mongodb"]["users"]["dba"]["username"] + " --password " \ + dict_target_ttSettings["mongodb"]["users"]["dba"]["password"] + " --out " + filepath """ mongodump --host mongodb.b.prod.aws.3top.com:27017 --username superuser1 --password HnFBg67tZJ0NZvPC33YvM2hy5H1Sh48mwEe --out mongodump/ """ dump_OP = callSubprocess(dump_cmd) except: my_logger.error("Error encountered in mongodump_target(). Exiting! ") sys.exit("Error encountered in mongodump_target(). Exiting! ") my_logger.info("Mongodump() process completed successfully!")
def validate_mongodb_accounts(dict_ttSettings, hostname): my_logger.debug("validating mongodb_accounts()...") for k, v in dict_ttSettings["mongodb"]["users"].items(): my_logger.debug("Validating %s account for host: %s" % (k, hostname)) try: print("Mongodb account: %s for host: %s" % (k, hostname)), conn_cmd="mongo " + hostname + ":27017/" + v["database"] + " -u " + v["username"] + " -p " \ + v["password"] + " --eval \"printjson(db.getCollectionNames());\"" #print "Running command: %s"%conn_cmd, conn_OP = callSubprocess(conn_cmd) my_logger.debug("Received output: %s" % conn_OP) my_logger.debug("Account: %s for host: %s...validated" % (k, hostname)) print "...Validated!" except: my_logger.error("Validating mongodb_accounts failed. Exiting!") sys.exit("Validating mongodb_accounts failed. Exiting!") my_logger.debug("Accounts validation successful for service: MongoDB !")
def validate_mysql_aws_accounts(dict_ttSettings, target_hostname): aws_cert = dict_ttSettings["mysql"]["certs"]["public"] for k, v in dict_ttSettings["mysql"]["users"].items(): try: my_logger.debug("Validating mysql aws %s account for %s" % (target_hostname, k)) conn_cmd = "mysql --user="******"username"] + " -p" + v[ "password"] + " --ssl-ca=" + aws_cert + " --host=" + target_hostname + " --port=3306 -e \"CONNECT;\"" conn_out = callSubprocess(conn_cmd) except: my_logger.error( "Error Encountered in validate_mysql_aws_accounts(): %s. Exiting!" ) sys.exit( "Error Encountered in validate_mysql_aws_accounts(): %s. Exiting!" ) my_logger.debug( "mysql aws account for %s validated successfully for %s!" % (k, target_hostname))
def mysqldump_target(suffix, dict_ttSettings, hostname, my_logger, log_file): try: my_logger.info("In mysqldump()...") dump_cmd="mysqldump --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_ttSettings["mysql"]["users"]["dba"]["password"] \ + " --ssl-ca=" + os.path.expanduser(dict_ttSettings["mysql"]["certs"]["public"]) + " --host=" + hostname + " --port=3306 --single-transaction=TRUE --routines --events ebdb" filename = suffix + "-ebdb.sql" filepath = os.path.join(swapTimeStampDirPath, filename) my_logger.info("Create file success! %s " % filename) dump_out = callSubprocess(dump_cmd) f = open(filepath, "wb") f.write(dump_out) f.close() my_logger.info("Backup successful!") except: my_logger.info("Encountered Error in mysqldump_target(). Exiting!") sys.exit("Encountered Error in mysqldump_target(). Exiting!") my_logger.debug( "mysqldump_target() %s for %s successful! Backup path: %s" % (suffix, hostname, filepath)) return ("Returning to mysql_operations")
def validate_mysql_nyc_accounts(dict_ttSettings, target_hostname): my_logger.debug("validating mysql_nyc_accounts()...") for k, v in dict_ttSettings["mysql"]["users"].items(): try: my_logger.debug("Validating mysql nyc %s account for %s" % (target_hostname, k)) conn_cmd = "mysql --user="******"username"] + " -p" + v[ "password"] + " --host=" + target_hostname + " --port=3306 -e \"CONNECT;\"" conn_out = callSubprocess(conn_cmd) except: my_logger.error( "Validation of mysql_nyc_accounts() failed for service:%s, username: %s, hostname: %s" % (k, v["username"], target_hostname)) sys.exit( "Validation of mysql_nyc_accounts() failed for service:%s, username: %s, hostname: %s" % (k, v["username"], target_hostname)) my_logger.debug( "Validation of mysql_nyc_accounts() was successful for service:%s, username: %s, hostname: %s" % (k, v["username"], target_hostname)) my_logger.debug("Accounts validation successful for service: MySQL !")
def mongoclone(s_host, t_host, dict_source_ttSettings, dict_target_ttSettings, my_logger,log_file): my_logger.debug(" In mongoclone()...") try: filename="mongoclone.js" filepath=os.path.join(swapTimeStampDirPath,filename) my_logger.debug("Create file success! %s " % filename) f=open(filepath, "wb") f.write("printjson(db = db.getSiblingDB('apis_cache'));") f.write("\nprintjson(db.getCollectionNames());") f.write("\nprintjson(db.dropDatabase());") f.write("\nprintjson(db.copyDatabase (\"apis_cache\", \"apis_cache\", \"%s\", \"%s\", \"%s\"));"\ %(s_host, dict_source_ttSettings["mongodb"]["users"]["rw"]["username"],dict_source_ttSettings["mongodb"]["users"]["rw"]["password"])) f.write("\nprintjson(db.getCollectionNames());") f.close() clone_cmd="mongo admin -u " + dict_target_ttSettings["mongodb"]["users"]["dba"]["username"] + " -p " + dict_target_ttSettings["mongodb"]["users"]["dba"]["password"] \ + " --host " + t_host + " --verbose --port 27017 " + filepath clone_OP=callSubprocess(clone_cmd) except: my_logger.error("Error encountered in mongoclone(). Exiting! ") sys.exit("Error encountered in mongoclone(). Exiting! ") my_logger.info("Mongoclone() process completed successfully!")
def mongoclone(s_host, t_host, dict_source_ttSettings, dict_target_ttSettings, my_logger, log_file): my_logger.debug(" In mongoclone()...") try: filename = "mongoclone.js" filepath = os.path.join(swapTimeStampDirPath, filename) my_logger.debug("Create file success! %s " % filename) f = open(filepath, "wb") f.write("printjson(db = db.getSiblingDB('apis_cache'));") f.write("\nprintjson(db.getCollectionNames());") f.write("\nprintjson(db.dropDatabase());") f.write("\nprintjson(db.copyDatabase (\"apis_cache\", \"apis_cache\", \"%s\", \"%s\", \"%s\"));"\ %(s_host, dict_source_ttSettings["mongodb"]["users"]["rw"]["username"],dict_source_ttSettings["mongodb"]["users"]["rw"]["password"])) f.write("\nprintjson(db.getCollectionNames());") f.close() clone_cmd="mongo admin -u " + dict_target_ttSettings["mongodb"]["users"]["dba"]["username"] + " -p " + dict_target_ttSettings["mongodb"]["users"]["dba"]["password"] \ + " --host " + t_host + " --verbose --port 27017 " + filepath clone_OP = callSubprocess(clone_cmd) except: my_logger.error("Error encountered in mongoclone(). Exiting! ") sys.exit("Error encountered in mongoclone(). Exiting! ") my_logger.info("Mongoclone() process completed successfully!")
def mysqldump(suffix, dict_ttSettings, hostname, my_logger, log_file, backup_dir): my_logger.debug("Received suffix: %s"%suffix) try: my_logger.info("In mysqldump_source()...") dump_cmd="mysqldump --user="******"mysql"]["users"]["r"]["username"] + " -p" + dict_ttSettings["mysql"]["users"]["r"]["password"] \ + " --ssl-ca=" + os.path.expanduser(dict_ttSettings["mysql"]["certs"]["public"]) + " --host=" + hostname + " --port=3306 --single-transaction=TRUE --routines --events ebdb" if ('source' in suffix) or ('target' in suffix): filename = suffix + "-ebdb" filepath=os.path.join(swapTimeStampDirPath,filename) elif ('backup' in suffix): filename = "ebdb.sql" filepath = backup_dir + "/" + filename my_logger.info("Create file success! %s " % filename) my_logger.info("Backup file path: %s " % filepath) dump_out=callSubprocess(dump_cmd) f=open(filepath, "wb") f.write(dump_out) f.close() my_logger.info("Backup successful!") except: my_logger.info("Encountered Error in mysqldump(). Exiting!") sys.exit("Encountered Error in mysqldump(). Exiting!") my_logger.debug("mysqldump() %s for %s successful! Backup path: %s"%(suffix, hostname, filepath))
def mysql_restore(dict_target_ttSettings, target_hostname, my_logger, log_file, restore_dir): """ Check if the folder with database ebdb.sql exists """ my_logger.debug("Received restore_dir: %s" % restore_dir) print "path: %s" % os.path.join(restore_dir + "/ebdb.sql") if os.path.exists(os.path.join(restore_dir + "/ebdb.sql")): my_logger.info( "%s/ebdb.sql database location confirmed on local host...continuing" % restore_dir) print( "%s/ebdb.sql database location confirmed on local host...continuing" % restore_dir) else: my_logger.error( "%s+/ebdb.sql database location confirmed on local host...continuing" % restore_dir) sys.exit("%s+/ebdb.sql does not exist on local host. Exiting!") confirm_restore = raw_input( "Confirm if you want to continue with database restore [y/n]: ") if (confirm_restore == 'y') or (confirm_restore == 'Y'): my_logger.info("In mysql_restore()...") """ DROP DATABASE ebdb """ drop_args="mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] \ + " --ssl-ca=" + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 -e \"DROP DATABASE IF EXISTS ebdb\"" print "Dropping database ebdb.sql on %s" % target_hostname try: clone_drop_out = callSubprocess(drop_args) except: my_logger.error( "Unexpected error encountered in dropping ebdb.sql from %s. Exiting!" % target_hostname) sys.exit( "Unexpected error encountered in dropping ebdb.sql from %s. Exiting!" % target_hostname) my_logger.info("Drop database ebdb.sql from %s successful!" % target_hostname) print("Drop database ebdb.sql from %s successful!" % target_hostname) print("Creating database ebdb.sql on %s" % target_hostname) create_args="mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] \ + " --ssl-ca=" + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 -e \"CREATE DATABASE IF NOT EXISTS ebdb\"" try: clone_create_out = callSubprocess(create_args) except: my_logger.error( "Unexpected error encountered in Creating database ebdb.sql on %s. Exiting!" % target_hostname) sys.exit( "Unexpected error encountered in Creating database ebdb.sql on %s. Exiting!" % target_hostname) my_logger.info("Create database ebdb.sql successful!") print("Create database ebdb.sql successful!") print "Creating read-only user \'replnyc\' on %s" % target_hostname priv_args1 = "mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] + " --ssl-ca=" \ + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + \ " --port=3306 -e \"USE ebdb; GRANT SELECT, LOCK TABLES, SHOW VIEW, EVENT, TRIGGER ON *.* TO 'replnyc'@'%' IDENTIFIED BY \'" + dict_target_ttSettings["mysql"]["users"]["r"]["password"] + "\'; FLUSH PRIVILEGES;\" " try: clone_priv_out = callSubprocess(priv_args1) except: my_logger.error( "Unexpected error encountered in Creating read-only user \'replnyc\' on %s. Exiting!" % target_hostname) sys.exit( "Unexpected error encountered in Creating read-only user \'replnyc\' on %s. Exiting!" % target_hostname) my_logger.info( "Creating read-only user \'replnyc\' on %s successful!" % target_hostname) print("Creating read-only user \'replnyc\' on %s successful!" % target_hostname) print("Restoring data from %s/ebdb.sql to %s" % (restore_dir, target_hostname)) final_clone = "mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" \ + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] + " --ssl-ca=" + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 ebdb < " + restore_dir + "/ebdb.sql" try: os.system(final_clone) except: my_logger.error( "Unexpected encountered in Restoring data from %s/ebdb.sql to %s. Exiting!" % (restore_dir, target_hostname)) sys.exit( "Unexpected encountered in Restoring data from %s/ebdb.sql to %s. Exiting!" % (restore_dir, target_hostname)) my_logger.info("Restoring data from %s/ebdb.sql to %s successful!!" % (restore_dir, target_hostname)) print("Restoring data from %s/ebdb.sql to %s successful!" % (restore_dir, target_hostname)) elif (confirm_restore == 'n') or (confirm_restore == 'N'): my_logger.info("Received restore confirmation: %s. Exiting!" % confirm_restore) sys.exit("Received restore confirmation: %s. Exiting!" % confirm_restore) else: my_logger.error( "Unidentified response. Received restore confirmation: %s. Exiting!" % confirm_restore) sys.exit( "Unidentified response. Received restore confirmation: %s. Exiting!" % confirm_restore)
def mysql_restore(dict_target_ttSettings, target_hostname, my_logger, log_file, restore_dir): """ Check if the folder with database ebdb.sql exists """ my_logger.debug("Received restore_dir: %s"%restore_dir) print "path: %s"%os.path.join(restore_dir + "/ebdb.sql") if os.path.exists(os.path.join(restore_dir + "/ebdb.sql")): my_logger.info("%s/ebdb.sql database location confirmed on local host...continuing"%restore_dir) print ("%s/ebdb.sql database location confirmed on local host...continuing"%restore_dir) else: my_logger.error("%s+/ebdb.sql database location confirmed on local host...continuing"%restore_dir) sys.exit("%s+/ebdb.sql does not exist on local host. Exiting!") confirm_restore = raw_input("Confirm if you want to continue with database restore [y/n]: ") if (confirm_restore == 'y') or (confirm_restore == 'Y'): my_logger.info("In mysql_restore()...") """ DROP DATABASE ebdb """ drop_args="mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] \ + " --ssl-ca=" + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 -e \"DROP DATABASE IF EXISTS ebdb\"" print"Dropping database ebdb.sql on %s"%target_hostname try: clone_drop_out=callSubprocess(drop_args) except: my_logger.error("Unexpected error encountered in dropping ebdb.sql from %s. Exiting!" %target_hostname) sys.exit("Unexpected error encountered in dropping ebdb.sql from %s. Exiting!" %target_hostname) my_logger.info("Drop database ebdb.sql from %s successful!"%target_hostname) print("Drop database ebdb.sql from %s successful!"%target_hostname) print("Creating database ebdb.sql on %s"%target_hostname) create_args="mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] \ + " --ssl-ca=" + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 -e \"CREATE DATABASE IF NOT EXISTS ebdb\"" try: clone_create_out=callSubprocess(create_args) except: my_logger.error("Unexpected error encountered in Creating database ebdb.sql on %s. Exiting!"%target_hostname) sys.exit("Unexpected error encountered in Creating database ebdb.sql on %s. Exiting!"%target_hostname) my_logger.info("Create database ebdb.sql successful!") print("Create database ebdb.sql successful!") print"Creating read-only user \'replnyc\' on %s"%target_hostname priv_args1 = "mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] + " --ssl-ca=" \ + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + \ " --port=3306 -e \"USE ebdb; GRANT SELECT, LOCK TABLES, SHOW VIEW, EVENT, TRIGGER ON *.* TO 'replnyc'@'%' IDENTIFIED BY \'" + dict_target_ttSettings["mysql"]["users"]["r"]["password"] + "\'; FLUSH PRIVILEGES;\" " try: clone_priv_out = callSubprocess(priv_args1) except: my_logger.error("Unexpected error encountered in Creating read-only user \'replnyc\' on %s. Exiting!"%target_hostname) sys.exit("Unexpected error encountered in Creating read-only user \'replnyc\' on %s. Exiting!"%target_hostname) my_logger.info("Creating read-only user \'replnyc\' on %s successful!"%target_hostname) print ("Creating read-only user \'replnyc\' on %s successful!"%target_hostname) print("Restoring data from %s/ebdb.sql to %s"%(restore_dir, target_hostname)) final_clone = "mysql --user="******"mysql"]["users"]["dba"]["username"] + " -p" \ + dict_target_ttSettings["mysql"]["users"]["dba"]["password"] + " --ssl-ca=" + os.path.expanduser(dict_target_ttSettings["mysql"]["certs"]["public"]) + " --host=" + target_hostname + " --port=3306 ebdb < " + restore_dir + "/ebdb.sql" try: os.system(final_clone) except: my_logger.error("Unexpected encountered in Restoring data from %s/ebdb.sql to %s. Exiting!"%(restore_dir, target_hostname)) sys.exit("Unexpected encountered in Restoring data from %s/ebdb.sql to %s. Exiting!"%(restore_dir, target_hostname)) my_logger.info("Restoring data from %s/ebdb.sql to %s successful!!"%(restore_dir, target_hostname)) print("Restoring data from %s/ebdb.sql to %s successful!"%(restore_dir, target_hostname)) elif (confirm_restore == 'n') or (confirm_restore == 'N'): my_logger.info("Received restore confirmation: %s. Exiting!"%confirm_restore) sys.exit("Received restore confirmation: %s. Exiting!"%confirm_restore) else: my_logger.error("Unidentified response. Received restore confirmation: %s. Exiting!"%confirm_restore) sys.exit("Unidentified response. Received restore confirmation: %s. Exiting!"%confirm_restore)
def findActiveEnv(): my_logger.debug("Finding active environment...") try: my_logger.debug("Evaluating fp_internal_dns....") dns_int = callSubprocess(_fp_dns_internal_cmd) _fp_dns_internal = dns_int.split("\n")[2] my_logger.debug("fp_dns_internal: %s" % _fp_dns_internal) my_logger.debug("Evaluating fp_external_dns....") dns_ext = callSubprocess(_fp_dns_external_cmd) _fp_dns_external = dns_ext.split("\n")[1] my_logger.debug("fp_dns_external: %s" % _fp_dns_external) my_logger.debug("Evaluating _fp_live.....") live = callSubprocess(_fp_live_cmd) _fp_live = live.split("\n")[0][3] my_logger.debug("fp_live: %s" % _fp_live) my_logger.debug("Evaluating _fp_live_dns.....") live_dns = callSubprocess(_fp_live_dns_cmd) _fp_live_dns = live_dns.split("\n")[0] my_logger.debug("live_dns: %s" % _fp_live_dns) """ ORIGINAL CODE WITH EXTERNAL AND INTERNAL CHECK ********************************************** if _fp_dns_internal != _fp_dns_external: my_logger.error("The Internal Active Directory DNS and external AWS Route 53 DNS servers think different environments are live! Internal DNS says %s and external says %s. EXITING!" %(_fp_dns_internal, _fp_dns_external)) sys.exit("ERROR: The Internal Active Directory DNS and external AWS Route 53 DNS servers think different environments are live! Internal DNS says %s and external says %s. EXITING!" %(_fp_dns_internal, _fp_dns_external)) else: if _fp_live != "a" and _fp_live != "b": my_logger.error("ERROR: Could not resolve which prod environment is live, A or B. Script got %s from %s. EXITING!" %(_fp_live,_fp_live_dns)) sys.exit("ERROR: Could not resolve which prod environment is live, A or B. Script got %s from %s. EXITING!" %(_fp_live,_fp_live_dns)) else: if _fp_live == "a": _copy_source_prefix = "prod-a" elif _fp_live == "b": _copy_source_prefix = "prod-b" """ """ #NEW CODE W/O EXTERNAL AND INTERNAL CHECK START **********************************************""" if _fp_live != "a" and _fp_live != "b": my_logger.error("Could not resolve which prod environment is live, A or B. Script got %s from %s. Run with option \"-p\" if you are in maintenance mode (to set production environment). EXITING!" %(_fp_live,_fp_live_dns)) sys.exit("ERROR: Could not resolve which prod environment is live, A or B. Script got %s from %s. Run with option \"-p\" if you are in maintenance mode (to set production environment). EXITING! " %(_fp_live,_fp_live_dns)) else: if _fp_live == "a": _copy_source_prefix = "prod-a" elif _fp_live == "b": _copy_source_prefix = "prod-b" """ #NEW CODE W/O EXTERNAL AND INTERNAL CHECK STOP **********************************************""" except: my_logger.error("Unexpected Error Encountered. Cannot deter Exiting!") sys.exit("Unexpected Error Encountered. Exiting!") """ #new insert start _copy_source_prefix = "prod-b" #new insert stop """ my_logger.debug("Returning to main() with _copy_source_prefix: %s" % _copy_source_prefix) return _copy_source_prefix