restore.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/python
  2. import os
  3. import subprocess
  4. import sys
  5. from datetime import datetime
  6. BACKUP_DIR = os.environ["BACKUP_DIR"]
  7. S3_PATH = os.environ["S3_PATH"]
  8. S3_EXTRA_OPTIONS = os.environ.get("S3_EXTRA_OPTIONS") or ""
  9. DB_USE_ENV = os.environ.get("DB_USE_ENV") or False
  10. DB_NAME = os.environ["DB_NAME"] if "DB_NAME" in os.environ else os.environ.get("PGDATABASE")
  11. if not DB_NAME:
  12. raise Exception("DB_NAME must be set")
  13. if not DB_USE_ENV:
  14. DB_HOST = os.environ["DB_HOST"]
  15. DB_PASS = os.environ["DB_PASS"]
  16. DB_USER = os.environ["DB_USER"]
  17. DB_PORT = os.environ.get("DB_PORT") or "5432"
  18. file_name = sys.argv[1]
  19. backup_file = os.path.join(BACKUP_DIR, file_name)
  20. if not S3_PATH.endswith("/"):
  21. S3_PATH = S3_PATH + "/"
  22. def cmd(command, **kwargs):
  23. try:
  24. subprocess.check_output([command], shell=True, stderr=subprocess.STDOUT, **kwargs)
  25. except subprocess.CalledProcessError as e:
  26. sys.stderr.write("\n".join([
  27. "Command execution failed. Output:",
  28. "-" * 80,
  29. e.output,
  30. "-" * 80,
  31. ""
  32. ]))
  33. raise
  34. def backup_exists():
  35. return os.path.exists(backup_file)
  36. def restore_backup():
  37. if not backup_exists():
  38. sys.stderr.write("Backup file doesn't exists!\n")
  39. sys.exit(1)
  40. # restore postgres-backup
  41. env = os.environ.copy()
  42. if DB_USE_ENV:
  43. env.update({key: os.environ[key] for key in os.environ.keys() if key.startswith('PG')})
  44. else:
  45. env.update(
  46. {'PGPASSWORD': DB_PASS, 'PGHOST': DB_HOST, 'PGUSER': DB_USER, 'PGDATABASE': DB_NAME, 'PGPORT': DB_PORT})
  47. cmd("pg_restore -Fc -d %s %s" % (DB_NAME, backup_file), env=env)
  48. def download_backup():
  49. cmd("aws s3 cp %s %s%s %s" % (S3_EXTRA_OPTIONS, S3_PATH, file_name, backup_file))
  50. def log(msg):
  51. print("[%s]: %s" % (datetime.now().strftime("%Y-%m-%d %H:%M:%S"), msg))
  52. def main():
  53. start_time = datetime.now()
  54. if backup_exists():
  55. log("Backup file already exists in filesystem %s" % backup_file)
  56. else:
  57. log("Downloading database dump")
  58. download_backup()
  59. log("Restoring database")
  60. restore_backup()
  61. log("Restore complete, took %.2f seconds" % (datetime.now() - start_time).total_seconds())
  62. if __name__ == "__main__":
  63. main()