o ckF[&@sldZddlZddlmZddlmZddlZddlZddlm Z ddl m Z dZ dZ d ZGd d d e ZdS) a 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 = N)Volume)EC2ResponseError) Installer)Templateac#!/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 as e: self.notify(subject="${instance_id} Backup Failed", body=traceback.format_exc()) boto.log.info("Snapshot created: ${volume_id}") except Exception as 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() z#!/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) zr#!/usr/bin/env python import boto # Cleans Backups of EBS volumes ec2 = boto.connect_ec2() ec2.trim_snapshots() csdeZdZdZdfdd ZddZddZd d Zdd d ZddZ ddZ ddZ ddZ Z S) EBSInstallerz Set up the EBS stuff Ncshtt||tjdd|_tjddd|_tjdd|_tjdd|_ tjddd |_ dS) NInstancez instance-idEBSdevicez/dev/sdp volume_idlogical_volume_name mount_pointz/ebs) superr__init__botoconfigget instance_idr r r r )self config_file __class__B/usr/lib/python3/dist-packages/boto/pyami/installers/ubuntu/ebs.pyrhs zEBSInstaller.__init__c Csft}|jrttj|jd}|j|_||jgd}| dkr:tj d|j |j ftd| dks#||jgd}d}|rz||j|j|jd}Wn-ty}z!|jdkrvtj d |j|j|jftd n|WYd}~nd}~ww|sGtj d |j|j|jftj|jstj d |jtd tj|jrdSdS) N)namer availablez1Volume %s not yet available. Current status = %s.TFIncorrectStatez]Attempt to attach the EBS volume %s to this instance (%s) returned %s. Trying again in a bit.z.Attached volume %s to instance %s as device %sz*%s still does not exist, waiting 2 seconds)r connect_ec2r nextrfind _volume_idr get_all_volumesupdateloginfoidstatustimesleepget_only_instancesr attach_volumer r error_codeerrorsospathexists)rec2logical_volumevolumeinstanceattempt_attacherrrattachps:        zEBSInstaller.attachcCs<tjd|d|j}|dkr|d|jdSdS)Nz make_fs...zfsck %srzmkfs -t xfs %s)rr$r%runr )rhas_fsrrrmake_fss zEBSInstaller.make_fscCsHtt}|j|j|j|jd}tdd}|||| ddS)N)r rr /usr/local/bin/ebs_backupwz"chmod +x /usr/local/bin/ebs_backup) rBackupScriptTemplate substituter rr openwritecloser8)rtsfprrrcreate_backup_scripts   z!EBSInstaller.create_backup_scriptFcCs:tdd}|r |tn|t||ddS)N!/usr/local/bin/ebs_backup_cleanupr<z*chmod +x /usr/local/bin/ebs_backup_cleanup)r?r@TagBasedBackupCleanupScriptBackupCleanupScriptrAr8)ruse_tag_based_cleanuprDrrrcreate_backup_cleanup_scripts   z)EBSInstaller.create_backup_cleanup_scriptcCstjdtj|jstjd|d|jn>tjd|d|jj d}|D])}| }|rY|d|jkrY|d|j krY|d |j|d |dnq0|d |d |j |jf|d |jdS)Nhandle_mount_pointzmaking directoryzmkdir %szdirectory exists alreadyzmount -l rrz umount %sz mount %s /tmpzchmod 777 /tmpz mount %s %sz xfs_growfs %s) rr$r%r.r/isdirr r8 last_commandoutputsplitr )rlineslinerBrrrrKs$     zEBSInstaller.handle_mount_pointcCs,tdd}|d|j|jf|dS)Nz /etc/fstabaz%s %s xfs defaults 0 0 )r?r@r r rA)rfrrr update_fstabs  zEBSInstaller.update_fstabcCs||d|||tjddd}tjddd}|jdd||d tjdd }tjdd }|durY|durYtj dd }| ||jd d||d | dS)Nz#apt-get -y install xfsprogs xfsdumprbackup_cron_minute0backup_cron_hourz4,16 ebs_backupr;)minutehourbackup_cleanup_cron_minutebackup_cleanup_cron_houruse_tag_based_snapshot_cleanupebs_backup_cleanuprF) r7r8r:rKrErrradd_cron has_optionrJrU)rrZr[rIrrrinstalls   zEBSInstaller.installcCs0tj|js |dStjd|jdS)Nz5Device %s is already attached, skipping EBS Installer)r.r/r0r rbrr$r%)rrrrmains zEBSInstaller.main)N)F)__name__ __module__ __qualname____doc__rr7r:rErJrKrUrbrc __classcell__rrrrrcs!  $r)rgrboto.manage.volumerboto.exceptionrr.r(&boto.pyami.installers.ubuntu.installerrstringrr=rHrGrrrrrs