velero_restore_new.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import datetime
  2. import os
  3. import json
  4. import subprocess
  5. import sys
  6. namespaces = ["vaultwarden", "postgres"]
  7. k3s_env = {"KUBECONFIG": "/etc/rancher/k3s/k3s.yaml"}
  8. ntfy_topic = "https://ntfy.jibby.org/velero-restore"
  9. ntfy_auth = os.environ["NTFY_AUTH"]
  10. restart_deployments_in = ["vaultwarden"]
  11. def main():
  12. if sys.version_info.major < 3 or sys.version_info.minor < 11:
  13. raise RuntimeError("Python 3.11 or greater required")
  14. velero_str = subprocess.run(
  15. ["/usr/local/bin/velero", "backup", "get", "-o", "json"],
  16. env=k3s_env,
  17. check=True,
  18. capture_output=True,
  19. ).stdout
  20. velero = json.loads(velero_str)
  21. backups_by_timestamp = {
  22. backup['metadata']['creationTimestamp']: backup
  23. for backup in velero['items']
  24. }
  25. if not backups_by_timestamp:
  26. raise ValueError("no backups?")
  27. newest_backup_timestamp = max(backups_by_timestamp.keys())
  28. one_week_ago = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=7)
  29. if datetime.datetime.fromisoformat(newest_backup_timestamp) < one_week_ago:
  30. raise ValueError(f"no backups < 1 week old? {newest_backup_timestamp=}")
  31. newest_backup = backups_by_timestamp[newest_backup_timestamp]
  32. print(f"Using newest backup {newest_backup['metadata']['name']}, taken at {newest_backup['metadata']['creationTimestamp']}")
  33. # delete namespaces
  34. for namespace in namespaces:
  35. subprocess.run(
  36. ["/usr/local/bin/kubectl", "delete", "namespace", namespace],
  37. env=k3s_env,
  38. check=True,
  39. )
  40. subprocess.run(
  41. ["/usr/local/bin/velero", "restore", "create", "--from-backup", newest_backup['metadata']['name'], "--include-namespaces", ",".join(namespaces), "--wait"],
  42. env=k3s_env,
  43. check=True,
  44. )
  45. for namespace in restart_deployments_in:
  46. subprocess.run(
  47. ["/usr/local/bin/kubectl", "-n", namespace, "rollout", "restart", "deployment"],
  48. env=k3s_env,
  49. check=True,
  50. )
  51. ntfy_send(
  52. f"Successfully ran velero restore for backup {newest_backup['metadata']['name']}, "
  53. f"{newest_backup['metadata']['creationTimestamp']}"
  54. )
  55. def ntfy_send(data):
  56. # auth & payload formatting is awful in urllib. just use curl
  57. subprocess.run(["curl", "-u", ntfy_auth, "-d", data, ntfy_topic], check=True)
  58. if __name__ == '__main__':
  59. try:
  60. main()
  61. except Exception as e:
  62. ntfy_send(f"Error running velero restore: {str(e)}")
  63. raise