ó FRTc@s–dZddlZddlmZddlmZddlZddlZddlm Z ddl m Z dZ dZ d Zd e fd „ƒYZdS( s Automated installer to attach, format and mount an EBS volume. This installer assumes that you want the volume formatted as an XFS file system. To drive this installer, you need the following section in the boto config passed to the new instance. You also need to install dateutil by listing python-dateutil in the list of packages to be installed in the Pyami seciont of your boto config file. If there is already a device mounted at the specified mount point, the installer assumes that it is the ephemeral drive and unmounts it, remounts it as /tmp and chmods it to 777. Config file section:: [EBS] volume_id = logical_volume_name = device = mount_point = iÿÿÿÿN(tVolume(tEC2ResponseError(t Installer(tTemplates_#!/usr/bin/env python # Backup EBS volume import boto from boto.pyami.scriptbase import ScriptBase import traceback class Backup(ScriptBase): def main(self): try: ec2 = boto.connect_ec2() self.run("/usr/sbin/xfs_freeze -f ${mount_point}", exit_on_error = True) snapshot = ec2.create_snapshot('${volume_id}') boto.log.info("Snapshot created: %s " % snapshot) except Exception, e: self.notify(subject="${instance_id} Backup Failed", body=traceback.format_exc()) boto.log.info("Snapshot created: ${volume_id}") except Exception, e: self.notify(subject="${instance_id} Backup Failed", body=traceback.format_exc()) finally: self.run("/usr/sbin/xfs_freeze -u ${mount_point}") if __name__ == "__main__": b = Backup() b.main() sœ#!/usr/bin/env python import boto from boto.manage.volume import Volume # Cleans Backups of EBS volumes for v in Volume.all(): v.trim_snapshots(True) sr#!/usr/bin/env python import boto # Cleans Backups of EBS volumes ec2 = boto.connect_ec2() ec2.trim_snapshots() t EBSInstallercBseeZdZd d„Zd„Zd„Zd„Zed„Z d„Z d„Z d„Z d „Z RS( s Set up the EBS stuff cCs˜tt|ƒj|ƒtjjddƒ|_tjjdddƒ|_tjjddƒ|_tjjddƒ|_ tjjddd ƒ|_ dS( NtInstances instance-idtEBStdevices/dev/sdpt volume_idtlogical_volume_namet mount_points/ebs( tsuperRt__init__tbototconfigtgett instance_idRRR R (tselft config_file((sM/opt/freeware/lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyR hs cCs»tjƒ}|jr?ttjd|jƒƒ}|j|_n|j|jgƒd}xC|j ƒdkrtj j d|j |j fƒtjdƒq[W|j|jgƒd}t}x‘|rPy&|j|j|j|jƒt}WqÀtk rL}|jdkrCtj j d|j|j|jfƒtjdƒqM|‚qÀXqÀWtj j d |j|j|jfƒx=tjj|jƒs¶tj j d |jƒtjdƒqzWdS( Ntnameit availables1Volume %s not yet available. Current status = %s.itIncorrectStates]Attempt to attach the EBS volume %s to this instance (%s) returned %s. Trying again in a bit.is.Attached volume %s to instance %s as device %ss*%s still does not exist, waiting 2 seconds(R t connect_ec2R tnextRtfindt _volume_idRtget_all_volumestupdatetlogtinfotidtstatusttimetsleeptget_only_instancesRtTruet attach_volumeRtFalseRt error_codeterrorstostpathtexists(Rtec2tlogical_volumetvolumetinstancetattempt_attachte((sM/opt/freeware/lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pytattachps.     &&cCsMtjjdƒ|jd|jƒ}|dkrI|jd|jƒndS(Ns make_fs...sfsck %sismkfs -t xfs %s(R RRtrunR(Rthas_fs((sM/opt/freeware/lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pytmake_fs‘s cCsjttƒ}|jd|jd|jd|jƒ}tddƒ}|j|ƒ|jƒ|j dƒdS(NRRR s/usr/local/bin/ebs_backuptws"chmod +x /usr/local/bin/ebs_backup( RtBackupScriptTemplatet substituteRRR topentwritetcloseR2(Rtttstfp((sM/opt/freeware/lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pytcreate_backup_script—s    cCsMtddƒ}|r%|jtƒn |jtƒ|jƒ|jdƒdS(Ns!/usr/local/bin/ebs_backup_cleanupR5s*chmod +x /usr/local/bin/ebs_backup_cleanup(R8R9tTagBasedBackupCleanupScripttBackupCleanupScriptR:R2(Rtuse_tag_based_cleanupR=((sM/opt/freeware/lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pytcreate_backup_cleanup_script s   cCs9tjjdƒtjj|jƒsLtjjdƒ|jd|jƒn«tjjdƒ|jdƒ|jj j dƒ}xv|D]n}|j ƒ}|r…|d|jkr…|d|j kró|jd |jƒ|jd |dƒPqóq…q…W|jd ƒ|jd |j |jfƒ|jd |jƒdS(Nthandle_mount_pointsmaking directorysmkdir %ssdirectory exists alreadysmount -ls iis umount %ss mount %s /tmpschmod 777 /tmps mount %s %ss xfs_growfs %s( R RRR(R)tisdirR R2t last_commandtoutputtsplitR(RtlinestlineR;((sM/opt/freeware/lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyRC©s"     cCs:tddƒ}|jd|j|jfƒ|jƒdS(Ns /etc/fstabtas%s %s xfs defaults 0 0 (R8R9RR R:(Rtf((sM/opt/freeware/lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyt update_fstabÁscCs|jƒ|jdƒ|jƒ|jƒ|jƒtjjdddƒ}tjjdddƒ}|jddd |d |ƒtjjdd ƒ}tjjdd ƒ}|dk r|dk rtjj dd ƒ}|j |ƒ|jddd |d |ƒn|j ƒdS(Ns#apt-get -y install xfsprogs xfsdumpRtbackup_cron_minutet0tbackup_cron_hours4,16t ebs_backups/usr/local/bin/ebs_backuptminutethourtbackup_cleanup_cron_minutetbackup_cleanup_cron_hourtuse_tag_based_snapshot_cleanuptebs_backup_cleanups!/usr/local/bin/ebs_backup_cleanup( R1R2R4RCR>R RRtadd_crontNonet has_optionRBRL(RRQRRRA((sM/opt/freeware/lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pytinstallÆs      cCs=tjj|jƒs"|jƒntjjd|jƒdS(Ns5Device %s is already attached, skipping EBS Installer(R(R)R*RRZR RR(R((sM/opt/freeware/lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pytmainês N(t__name__t __module__t__doc__RXR R1R4R>R%RBRCRLRZR[(((sM/opt/freeware/lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyRcs  !    $(R^R tboto.manage.volumeRtboto.exceptionRR(R t&boto.pyami.installers.ubuntu.installerRtstringRR6R@R?R(((sM/opt/freeware/lib/python2.7/site-packages/boto/pyami/installers/ubuntu/ebs.pyt-s