Skip to content

Instantly share code, notes, and snippets.

@hughdbrown
Forked from alq666/pgsnap.py
Last active August 29, 2015 14:13
Show Gist options
  • Save hughdbrown/cf63b7a7651e4e90bce3 to your computer and use it in GitHub Desktop.
Save hughdbrown/cf63b7a7651e4e90bce3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import sys
import time
import subprocess
import urllib2
import boto
def setup_logger():
import logging
from logging.handlers import SysLogHandler
logger = logging.getLogger(LOGGER_NAME)
handler = SysLogHandler(address='/dev/log')
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
return logger
logger = setup_logger()
def who_am_i():
"""
Return one's instance id
This mysterious bit of code is best explained in this 2009-03-09 stackoverflow answer:
http://stackoverflow.com/questions/625644/find-out-the-instance-id-from-within-an-ec2-machine
"""
return urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id', timeout=1).read().strip()
def list_ebs(conn, blacklist=None):
"""Get all ebs volumes for oneself
@return [(device, ebs-id), ...]
"""
if blacklist is None:
blacklist = ["/dev/sda1"]
instance_id = who_am_i()
r = conn.get_all_instances([instance_id])[0]
assert len(r.instances) == 1, r.instances
i = r.instances[0]
return [(device, volume) for device, volume in i.block_device_mapping.items() if device not in blacklist]
def snap_ebs(device, volume, conn):
return conn.create_snapshot(volume.volume_id, device)
def freeze(mnt):
logger.info("Calling xfs_freeze -f")
return subprocess.call(["sudo", "xfs_freeze", "-f", mnt]) == 0
def thaw(mnt):
logger.info("Calling xfs_freeze -u")
return subprocess.call(["sudo", "xfs_freeze", "-u", mnt]) == 0
def main(xfs_mount_point):
assert os.path.isdir(xfs_mount_point), "Usage: pgsnap.py xfs_mount_point"
conn = boto.connect_ec2(os.environ["AWS_ACCESS_KEY_ID"], os.environ["AWS_SECRET_KEY_ID"])
vols = list_ebs(conn)
frozen = False
start = time.time()
try:
frozen = freeze(xfs_mount_point)
assert frozen, "xfs_freeze failed"
snaps = [snap_ebs(device, volume, conn) for device, volume in vols]
logger.info("Snapshots: {0}".format(snaps))
finally:
if frozen:
thaw(xfs_mount_point)
logger.info("XFS was frozen for {0}".format(time.time() - start))
if __name__ == '__main__':
for arg in sys.argv[1:]:
main(arg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment