Last active
March 22, 2019 03:33
-
-
Save quangthe/fa15df1c0470b3c50084ad6e26aa4530 to your computer and use it in GitHub Desktop.
Backup db and odoo files. Odoo 11. Docker
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/python | |
import boto3 | |
import subprocess | |
import datetime | |
import os | |
import zipfile | |
import humanize | |
WORKING_DIR = "/tmp" | |
def backup_db(): | |
current = datetime.datetime.now() | |
filename = "{}/odoo_dump_{}-{:0>2}-{:0>2}_{:0>2}-{:0>2}-{:0>2}.sql".format(WORKING_DIR, | |
current.year, current.month, current.day, | |
current.hour, current.minute, current.second) | |
print "Dumping DB to file {}".format(filename) | |
backup_process = subprocess.Popen( | |
"docker exec -t odoodb pg_dumpall -c -U postgres > {}".format(filename), | |
stdout=subprocess.PIPE, | |
shell=True | |
) | |
(output, error) = backup_process.communicate() | |
if error is not None: | |
print error | |
return None | |
return filename; | |
def upload_to_s3(bucket, filename): | |
s3 = boto3.resource("s3") | |
print "Uploading {} ({}) to bucket {}".format(filename, | |
humanize.naturalsize(os.path.getsize(filename), binary=True), bucket) | |
data = open(filename, "rb") | |
s3.Bucket(bucket).put_object(Key=os.path.basename(filename), Body=data) | |
print "Uploaded {} ({}) to bucket {}".format(filename, humanize.naturalsize(os.path.getsize(filename), binary=True), | |
bucket) | |
os.remove(filename) | |
print "Remove file {}".format(filename) | |
def zip(src, dst): | |
destZip = "{}/{}.zip".format(WORKING_DIR, dst) | |
zf = zipfile.ZipFile(destZip, "w", zipfile.ZIP_DEFLATED) | |
abs_src = os.path.abspath(src) | |
for dirname, subdirs, files in os.walk(src): | |
for filename in files: | |
absname = os.path.abspath(os.path.join(dirname, filename)) | |
arcname = absname[len(abs_src) + 1:] | |
print "zipping %s as %s" % (os.path.join(dirname, filename), | |
arcname) | |
zf.write(absname, arcname) | |
zf.close() | |
return destZip; | |
def backup_all(bucket="dt-odoo-backup"): | |
now = datetime.datetime.now() | |
dest = "{}/odoo_backup_{}-{:0>2}-{:0>2}_{:0>2}-{:0>2}-{:0>2}.zip".format(WORKING_DIR, | |
now.year, now.month, now.day, | |
now.hour, now.minute, now.second) | |
db_file = backup_db() | |
odoo_file = backup_odoo() | |
with zipfile.ZipFile(dest, 'w') as zf: | |
zf.write(db_file, os.path.basename(db_file)); | |
zf.write(odoo_file, os.path.basename(odoo_file)); | |
upload_to_s3(bucket,dest); | |
os.remove(db_file) | |
os.remove(odoo_file) | |
def backup_odoo(): | |
now = datetime.datetime.now() | |
dest = "odoo_files_{}-{:0>2}-{:0>2}_{:0>2}-{:0>2}-{:0>2}".format(now.year, now.month, now.day, now.hour, | |
now.minute, now.second) | |
odoo_files = zip(os.path.expanduser("~/odoo"), dest) | |
return odoo_files; | |
if __name__ == "__main__": | |
bucket = "dt-odoo-backup" | |
backup_all(bucket) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment