Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arashrasoulzadeh/d36224e32062e9430a30e8761cc784f9 to your computer and use it in GitHub Desktop.
Save arashrasoulzadeh/d36224e32062e9430a30e8761cc784f9 to your computer and use it in GitHub Desktop.
import os
import zipfile
import logging
import boto3
from botocore.exceptions import ClientError
import datetime
# archive the file
current_date = datetime.datetime.now()
#change this to the dump path
dump_path = "E:\\xampp\\mysql\\bin\\mysqldump.exe"
archive_name = "{}-{}-{}-{}-{}".format(current_date.year,
current_date.month, current_date.day, current_date.hour, current_date.minute)
print(archive_name, "creating backup file ...")
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file),
os.path.join(path, '..')))
zipf = zipfile.ZipFile('{}.zip'.format(archive_name),
'w', zipfile.ZIP_DEFLATED)
zipdir('backup/', zipf)
zipf.close()
def upload_file(file_name, bucket, object_name=None):
"""Upload a file to an S3 bucket
:param file_name: File to upload
:param bucket: Bucket to upload to
:param object_name: S3 object name. If not specified then file_name is used
:return: True if file was uploaded, else False
"""
# If S3 object_name was not specified, use file_name
if object_name is None:
object_name = file_name
# Upload the file
s3_client = boto3.client('s3')
try:
response = s3_client.upload_file(file_name, bucket, object_name)
except ClientError as e:
logging.error(e)
return False
return True
s3_client = boto3.client(
service_name='s3',
aws_access_key_id="",
aws_secret_access_key="",
endpoint_url="https://s3.ir-thr-at1.arvanstorage.com",
)
def upload_single_file(file_path, file_name):
with open(file_path, "rb") as f:
s3_client.upload_fileobj(f, "drjvb", file_name)
print(archive_name, "uploading backup file ...")
upload_single_file("{}.zip".format(archive_name),
"{}.zip".format(archive_name))
print(archive_name, "creating database backup file ...")
os.system("{} -u root mohem > {}.sql".format(dump_path, archive_name))
print(archive_name, "uploading database backup file ...")
upload_single_file("{}.sql".format(archive_name),
"{}.sql".format(archive_name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment