daily pastebin goal
3%
SHARE
TWEET

Untitled

a guest Mar 26th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/python
  2. import boto3
  3. import subprocess
  4. import datetime
  5. import os
  6. import zipfile
  7. import humanize
  8.  
  9. WORKING_DIR = "/tmp"
  10.  
  11. def backup_db():
  12.     current = datetime.datetime.now()
  13.     filename = "{}/odoo_dump_{}-{:0>2}-{:0>2}_{:0>2}-{:0>2}-{:0>2}.sql".format(WORKING_DIR,
  14.                                                                             current.year, current.month, current.day,
  15.                                                                             current.hour, current.minute, current.second)
  16.     print "Dumping DB to file {}".format(filename)
  17.     backup_process = subprocess.Popen(
  18.         "docker exec -t odoodb pg_dumpall -c -U postgres > {}".format(filename),
  19.         stdout=subprocess.PIPE,
  20.         shell=True
  21.     )
  22.     (output, error) = backup_process.communicate()
  23.     if error is not None:
  24.         print error
  25.         return None
  26.     return filename;
  27.  
  28. def upload_to_s3(bucket, filename):
  29.     s3 = boto3.resource("s3")
  30.     print "Uploading {} ({}) to bucket {}".format(filename,
  31.                                                   humanize.naturalsize(os.path.getsize(filename), binary=True), bucket)
  32.     data = open(filename, "rb")
  33.     s3.Bucket(bucket).put_object(Key=os.path.basename(filename), Body=data)
  34.     print "Uploaded {} ({}) to bucket {}".format(filename, humanize.naturalsize(os.path.getsize(filename), binary=True),
  35.                                                  bucket)
  36.  
  37.     os.remove(filename)
  38.     print "Remove file {}".format(filename)
  39.  
  40.  
  41. def zip(src, dst):
  42.     destZip = "{}/{}.zip".format(WORKING_DIR, dst)
  43.     zf = zipfile.ZipFile(destZip, "w", zipfile.ZIP_DEFLATED)
  44.     abs_src = os.path.abspath(src)
  45.     for dirname, subdirs, files in os.walk(src):
  46.         for filename in files:
  47.             absname = os.path.abspath(os.path.join(dirname, filename))
  48.             arcname = absname[len(abs_src) + 1:]
  49.             print "zipping %s as %s" % (os.path.join(dirname, filename),
  50.                                         arcname)
  51.             zf.write(absname, arcname)
  52.     zf.close()
  53.     return destZip;
  54.  
  55. def backup_all(bucket="dt-odoo-backup"):
  56.     now = datetime.datetime.now()
  57.     dest = "{}/odoo_backup_{}-{:0>2}-{:0>2}_{:0>2}-{:0>2}-{:0>2}.zip".format(WORKING_DIR,
  58.                                                                              now.year, now.month, now.day,
  59.                                                                              now.hour, now.minute, now.second)
  60.     db_file = backup_db()
  61.     odoo_file = backup_odoo()
  62.  
  63.     with zipfile.ZipFile(dest, 'w') as zf:
  64.         zf.write(db_file, os.path.basename(db_file));
  65.         zf.write(odoo_file, os.path.basename(odoo_file));
  66.  
  67.     upload_to_s3(bucket,dest);
  68.     os.remove(db_file)
  69.     os.remove(odoo_file)
  70.    
  71. def backup_odoo():
  72.     now = datetime.datetime.now()
  73.     dest = "odoo_files_{}-{:0>2}-{:0>2}_{:0>2}-{:0>2}-{:0>2}".format(now.year, now.month, now.day, now.hour,
  74.                                                                      now.minute, now.second)
  75.     odoo_files = zip(os.path.expanduser("~/odoo"), dest)
  76.     return odoo_files;
  77.  
  78.  
  79. if __name__ == "__main__":
  80.     bucket = "dt-odoo-backup"
  81.     backup_all(bucket)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top