38 lines
1.6 KiB
Python
38 lines
1.6 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""Offline PostgreSQL backup/verify/recovery CLI for administrators."""
|
||
|
|
from __future__ import annotations
|
||
|
|
# Allow direct `python scripts/name.py` from a fresh source checkout.
|
||
|
|
import sys as _sys
|
||
|
|
from pathlib import Path as _Path
|
||
|
|
_sys.path.insert(0, str(_Path(__file__).resolve().parents[1]))
|
||
|
|
import argparse
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
from pathlib import Path
|
||
|
|
from app import pg_disaster_recovery as pg
|
||
|
|
|
||
|
|
def main(argv=None):
|
||
|
|
p=argparse.ArgumentParser(description='SiteForge offline PostgreSQL disaster recovery')
|
||
|
|
sub=p.add_subparsers(dest='operation',required=True)
|
||
|
|
b=sub.add_parser('backup');b.add_argument('--data',required=True);b.add_argument('--output',required=True)
|
||
|
|
b.add_argument('--confirmed-offline',action='store_true')
|
||
|
|
v=sub.add_parser('verify');v.add_argument('archive')
|
||
|
|
r=sub.add_parser('restore-new');r.add_argument('archive');r.add_argument('--data',required=True)
|
||
|
|
r.add_argument('--confirmed-offline',action='store_true')
|
||
|
|
args=p.parse_args(argv)
|
||
|
|
try:
|
||
|
|
if args.operation=='verify':value=pg.verify(args.archive)
|
||
|
|
else:
|
||
|
|
url=os.getenv('SITEFORGE_DATABASE_URL','')
|
||
|
|
if args.operation=='backup':
|
||
|
|
value=pg.backup(url,args.data,args.output,confirmed_offline=args.confirmed_offline)
|
||
|
|
else:
|
||
|
|
value=pg.restore_new(url,args.archive,args.data,confirmed_offline=args.confirmed_offline)
|
||
|
|
print(json.dumps(value,ensure_ascii=False,indent=2))
|
||
|
|
return 0
|
||
|
|
except (pg.RecoveryError,OSError,ValueError) as exc:
|
||
|
|
print('ERROR: '+str(exc),file=__import__('sys').stderr)
|
||
|
|
return 1
|
||
|
|
|
||
|
|
if __name__=='__main__':raise SystemExit(main())
|