📜 ⬆️ ⬇️

Script to backup EC2-instance to AMI

Hello.

I want to share a script for $ subj. Perhaps someone will be useful.

Task setting: there are a number of EC2 servers in AWS scattered across different regions. It is required to automate their backups so that recovery is easy and fast.

Actually, the script itself: ec2-automate-backup2ami.sh
Description: README.md
Script wrapper for cron launch: ec2-backup-wrapper.sh
')
For the script to work, you must first:

The Krontab looks something like this:
 [ec2-user@backup ~]$ crontab -l PATH=$PATH:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin EC2_HOME=/usr/local SHELL=/bin/bash 00 2 * * * ./ec2-backup-wrapper.sh stage "alerts1@mydomain.cc alerts2@mydomain.cc" 


The result of the script is written to the log file type ec2-automate-backup2ami.stage.log. In case of a runtime error, it will be sent to the specified email addresses.

It is worth paying attention that the file with parameters is called . stage, and the script is called with a name without a dot.

After successful execution, an image with the name ec2ab_server.domain.cc_YYYY-MM-DD will appear in AWS AMI with the following tags:


PS The script is based on ec2-automate-backup (backup EBS-disks, without iteration by region) from colinbjohnson , for which many thanks to him!

PPS Perhaps, the script will work incorrectly under Mac OS / X (see the fourth line in the get_purge_after_date () function), but I have no opportunity to check it.

PPPS Before creating snapshots, the script does not flush the file system buffers, so the backup may be inconsistent. To create consistent snapshots, use ec2-consistent-snapshot.

UPD Added the ability to automatically copy the images created in the process of backup AMI to other regions. To enable this option, you must specify the -y option in the script launch command line, and add the corresponding tag to the instance settings. The region is selected before creating backups at random from all possible ones or from the list specified on the command line with the -o key (space separator). In other words, all copies will fall into one region within one script run.
For example:
 /usr/local/bin/ec2-automate-backup2ami.sh -s tag -t "Backup=true" -k 14d -p -h -u -n -y "CopyRegion=true" -o "us-west-1 eu-west-1" 

In the given example script:

The list of region names can be obtained using the ec2-describe-regions command.

After copying, a CopyRegion tag will be added to the original AMI with a value corresponding to the region where it was copied. And to the copied AMI - SourceRegion tag, defining the source region.

Source: https://habr.com/ru/post/256515/


All Articles