Skip to content

Commit 1e44f03

Browse files
alanbchristiea.b.christie
andauthored
Fix recovery filter (#7)
* fix: Fix recovery (s3 fuse) * fix: Fix "current user cannot be dropped" on recovery * fix: Fix recovery typo * fix: Fix for IF EXISTS * fix: Fix filter command --------- Co-authored-by: a.b.christie <alan.christie@matildapeak.com>
1 parent 6aa0ba1 commit 1e44f03

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

sql-recovery/recovery.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
# The recovery root dir, where backups are unpacked.
206206
RECOVERY_ROOT_DIR = '/recovery'
207207
DECOMPRESSED_RECOVERY_NAME = 'recovery'
208+
DECOMPRESSED_FILTERED_NAME = 'filtered'
208209
BACKUP_FILE_PREFIX = 'backup'
209210

210211
# Recovery commands for the various database flavours...
@@ -551,8 +552,9 @@ def error(error_no):
551552
# cause the recovery to fail with "current user cannot be dropped".
552553
# We only need to do this if the recovery is from a dumpall.
553554
if not DATABASE:
554-
TARGET_FILE = "%s/%s.sql" % (RECOVERY_ROOT_DIR, DECOMPRESSED_RECOVERY_NAME)
555-
FILTER_PG_CMD = "egrep -v '^(CREATE|DROP) ROLE IF EXISTS %s;' %s > filtered.sql ; mv filtered.sql %s" % (RECOVERY_DATABASE_ROOT_USER, TARGET_FILE, TARGET_FILE)
555+
RECOVERY_FILE = "%s/%s.sql" % (RECOVERY_ROOT_DIR, DECOMPRESSED_RECOVERY_NAME)
556+
FILTERED_FILE = "%s/%s.sql" % (RECOVERY_ROOT_DIR, DECOMPRESSED_FILTERED_NAME)
557+
FILTER_PG_CMD = "egrep -v '^(CREATE ROLE|DROP ROLE IF EXISTS) %s;' %s > %s ; mv %s %s" % (RECOVERY_DATABASE_ROOT_USER, RECOVERY_FILE, FILTERED_FILE, FILTERED_FILE, RECOVERY_FILE)
556558
print(" $", FILTER_PG_CMD)
557559
COMPLETED_PROCESS = subprocess.run(FILTER_PG_CMD,
558560
shell=True,
@@ -626,6 +628,16 @@ def error(error_no):
626628
print('--] OK - Found the expected number of databases (%s).'
627629
% DATABASE_EXPECTED_COUNT)
628630

631+
# Remove any files we may have created.
632+
RECOVERY_FILE = "%s/%s.sql" % (RECOVERY_ROOT_DIR, DECOMPRESSED_RECOVERY_NAME)
633+
FILTERED_FILE = "%s/%s.sql" % (RECOVERY_ROOT_DIR, DECOMPRESSED_FILTERED_NAME)
634+
if os.path.isfile(RECOVERY_FILE):
635+
print('--] Removing %s' % RECOVERY_FILE)
636+
os.remove(RECOVERY_FILE)
637+
if os.path.isfile(FILTERED_FILE):
638+
print('--] Removing %s' % FILTERED_FILE)
639+
os.remove(FILTERED_FILE)
640+
629641
# Success if we get here
630642
write_termination_message()
631643

0 commit comments

Comments
 (0)