Skip to content

Instantly share code, notes, and snippets.

@boldfield
Last active December 30, 2015 07:19
Show Gist options
  • Save boldfield/7794810 to your computer and use it in GitHub Desktop.
Save boldfield/7794810 to your computer and use it in GitHub Desktop.
Script to pull down the latest database restore spec from backup for use with the WAL-E backup-fetch command.
#!/usr/bin/env python
import sys
import os
import json
from collections import namedtuple
from wal_e.storage.base import StorageLayout
ENV_MAP = {
'WABS_ACCESS_KEY': 'access_key',
'AWS_SECRET_ACCESS_KEY': 'access_key',
'WABS_ACCOUNT_NAME': 'account_name',
'AWS_ACCESS_KEY_ID': 'account_name',
'WALE_WABS_PREFIX': 'prefix',
'WALE_S3_PREFIX': 'prefix'
}
env = namedtuple('env', ['account', 'access_key', 'prefix'])
def load_env(env_path):
fnames = os.listdir(env_path)
for fname in fnames:
if os.path.isfile(os.path.join(env_path, fname)) and fname in ENV_MAP:
with open(os.path.join(env_path, fname), 'r') as fs:
val = fs.read().strip('\n').strip()
setattr(env, ENV_MAP[fname], val)
return env
def main():
if len(sys.argv) != 2:
print "Usage: ./retrieve_spec.py <wal-e env path>"
sys.exit(1)
env_path = sys.argv[1]
env = load_env(env_path)
layout = StorageLayout(env.prefix)
if layout.is_s3:
from wal_e.blobstore import s3
from wal_e.operator.s3_operator import S3Backup
creds = s3.Credentials(env.account_name, env.access_key, None)
backup_ctx = S3Backup(layout, creds, None)
elif layout.is_wabs:
from wal_e.blobstore import wabs
from wal_e.operator.wabs_operator import WABSBackup
creds = wabs.Credentials(env.account_name, env.access_key)
backup_ctx = WABSBackup(layout, creds, None)
else:
print "Unknown blob store..."
sys.exit(1)
backup = list(backup_ctx._backup_list(False).find_all('LATEST'))[0]
backup.load_detail(backup_ctx.new_connection())
spec = backup.spec
if 'base_prefix' in spec:
del spec['base_prefix']
with open('spec.json', 'w') as fs:
json.dump(spec, fs)
print "Recovery spec written to ./spec.json"
print "You can now retrieve the latest full backup for the target"
print "database by running:"
print " /usr/bin/envdir <path to wal-e env dir> /usr/local/bin/wal-e backup-fetch \\"
print " --restore-spec spec.json <path to PostgreSQL env dir> LATEST"
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment