Created
February 25, 2019 18:24
-
-
Save erickgnavar/c7af6a0d16ef2114e29667263a1f3ee6 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import configparser | |
import os | |
import sys | |
import urllib.parse | |
import urllib.request | |
import xmlrpc.client | |
from datetime import datetime | |
import boto3 | |
AWS_ACCESS_KEY = os.environ.get("AWS_ACCESS_KEY") | |
AWS_SECRET_KEY = os.environ.get("AWS_SECRET_KEY") | |
AWS_ENDPOINT = os.environ.get("AWS_ENDPOINT") | |
AWS_REGION = os.environ.get("AWS_REGION") | |
AWS_BUCKET = os.environ.get("AWS_BUCKET") | |
def get_list_of_dbs(config): | |
url = "http://localhost:{}/xmlrpc/db".format(config["xmlrpc_port"]) | |
dbs = [] | |
try: | |
with xmlrpc.client.ServerProxy(url) as proxy: | |
dbs = proxy.list() | |
except Exception as ex: | |
sys.stderr.write(str(ex)) | |
return dbs | |
def backup_db(db_name, backup_dir, config, aws_client): | |
url = "http://localhost:{}/web/database/backup/".format( | |
config["xmlrpc_port"] | |
) | |
admin_password = config["admin_passwd"] | |
data = { | |
"master_pwd": admin_password, | |
"backup_format": "zip", | |
"name": db_name, | |
} | |
today = datetime.today() | |
encoded_data = urllib.parse.urlencode(data).encode("utf-8") | |
filename = "{}_{}.zip".format( | |
db_name, | |
today.strftime("%Y-%m-%d_%H-%M-%S"), | |
) | |
backup_dest_path = os.path.join(backup_dir, filename) | |
try: | |
path, _ = urllib.request.urlretrieve( | |
url, filename=backup_dest_path, data=encoded_data | |
) | |
remote_filename = "{}/{}".format( | |
today.strftime("%Y/%m"), | |
filename, | |
) | |
upload_file(aws_client, path, remote_filename) | |
# TODO: delte local file after upload | |
sys.stdout.write("Backup saved at {}\n".format(path)) | |
except Exception as ex: | |
sys.stderr.write(str(ex)) | |
def initialize_aws_client(): | |
session = boto3.session.Session() | |
return session.client( | |
"s3", | |
region_name=AWS_REGION, | |
endpoint_url=AWS_ENDPOINT, | |
aws_access_key_id=AWS_ACCESS_KEY, | |
aws_secret_access_key=AWS_SECRET_KEY, | |
) | |
def upload_file(client, path, filename): | |
client.upload_file(path, AWS_BUCKET, filename) | |
def main(config_path, backup_dir): | |
config_parser = configparser.ConfigParser() | |
config_parser.read(config_path) | |
aws_client = initialize_aws_client() | |
for db_name in get_list_of_dbs(config_parser["options"]): | |
sys.stdout.write("Backup of {} in progress\n".format(db_name)) | |
backup_db(db_name, backup_dir, config_parser["options"], aws_client) | |
sys.stdout.write("Backup process done!\n") | |
if __name__ == "__main__": | |
if len(sys.argv) != 3: | |
sys.stderr.write("Please enter the required args\n") | |
sys.exit(1) | |
_, config_path, backup_dir = sys.argv | |
if not os.path.isdir(backup_dir): | |
sys.stderr.write("The directory is not a directory\n") | |
sys.exit(1) | |
if not os.path.exists(config_path): | |
sys.stderr.write("The config file does not exists\n") | |
sys.exit(1) | |
main(config_path, backup_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment