<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scripts Archives | Cloudar</title>
	<atom:link href="https://cloudar.be/tag/scripts/feed/" rel="self" type="application/rss+xml" />
	<link>https://cloudar.be/tag/scripts/</link>
	<description>100% Focus On AWS // 100% Customer Obsession</description>
	<lastBuildDate>Mon, 08 Sep 2014 12:28:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Automating snapshots of EC2 EBS volumes</title>
		<link>https://cloudar.be/awsblog/automating-snapshotsbackups-of-ec2-ebs-volumes/</link>
					<comments>https://cloudar.be/awsblog/automating-snapshotsbackups-of-ec2-ebs-volumes/#comments</comments>
		
		<dc:creator><![CDATA[Bart Van Hecke]]></dc:creator>
		<pubDate>Mon, 08 Sep 2014 12:28:11 +0000</pubDate>
				<category><![CDATA[AWS Blog]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[ebs]]></category>
		<category><![CDATA[EC2]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[snapshots]]></category>
		<category><![CDATA[tagging]]></category>
		<category><![CDATA[Tags]]></category>
		<guid isPermaLink="false">https://cloudar.be/?p=399</guid>

					<description><![CDATA[<p>Note: We stopped using this script, and started using Ansible to create snapshots. You can read more about that here. Recently we were looking for a way to have our EC2 EBS volumes snapshotted on a daily basis. Although AWS makes it easy to take snapshots of your EBS volumes, this requires some manual intervention [&#8230;]</p>
<p>The post <a href="https://cloudar.be/awsblog/automating-snapshotsbackups-of-ec2-ebs-volumes/">Automating snapshots of EC2 EBS volumes</a> appeared first on <a href="https://cloudar.be">Cloudar</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>Note: We stopped using this script, and started using Ansible to create snapshots. You can read more about that <a href="https://cloudar.be/awsblog/instance-and-snapshot-management-with-ansible/">here</a>.</strong></p>
<p>Recently we were looking for a way to have our EC2 EBS volumes snapshotted on a daily basis. Although AWS makes it easy to take snapshots of your EBS volumes, this requires some manual intervention and thus prone to human error.<br />
As we love to automate as much as possible (we firmly believe in the &#8216;set &amp; forget&#8217; principle), we were looking for a way to script the AWS snapshotting mechanism. A quick Google search showed us that there already many solutions available that can handle this. However, one of these really drew our attention: The <strong><a href="https://github.com/colinbjohnson/aws-missing-tools/tree/master/ec2-automate-backup" target="_blank" rel="noopener noreferrer">ec2-automate-backup script</a></strong> by Collin Johnson. The script has it all:</p>
<ul>
<li>snapshot one specific volume or all volumes</li>
<li>Choose which volumes to backup</li>
<li>Backup volumes for as specific region</li>
<li>Snapshot retention: delete snapshots older than x days</li>
<li>&#8230;</li>
</ul>
<p>To handle automated snapshots on a daily basis of all our EBS volumes, we&#8217;ve setup a t1.micro EC2 instance (running CentOS 6.5) on which we created a cron job that runs daily, inventarises all volumes and snapshots only those volumes that have a Backup TAG value set to TRUE. Snapshots are being retained for 7 days by default. If the script detects snapshots older than 7 days, they will be deleted.</p>
<p>This is how we&#8217;ve set this up:</p>
<h2>Prerequisites</h2>
<h3>Java</h3>
<p>The Amazon EC2 CLI tools require Java. If you don&#8217;t have Java 1.7 or later installed, download and install Java as shown below:</p>
<pre>yum install java-1.7.0-openjdk</pre>
<p>Run the file command recursively to find the binary:</p>
<pre>file $(which java)</pre>
<p>→ example output: <em>/usr/bin/java: symbolic link to &#8216;/etc/alternatives/java&#8217;</em></p>
<p>The <em>&#8216;/usr/bin/java location&#8217;</em> is actually a link to <em>&#8216;/etc/alternatives/java&#8217;</em>, so you need to run the file command on that location to see whether that is the real binary:</p>
<pre>file /etc/alternatives/java</pre>
<p>→ example output: <em>/etc/alternatives/java: symbolic link to `/usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java&#8217;</em></p>
<p>This returns a new location, which is the actual binary. Verify this by running the file command on this location:</p>
<pre>file /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java</pre>
<p>→ example output: <em>/usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped</em><br />
This location is the actual binary. The Java home directory is where bin/java lives; in this example, the Java home directory is <em>&#8216;/usr/lib/jvm/jre-1.7.0-openjdk.x86_64&#8217;</em>.</p>
<p>You will now have to set the JAVA_HOME variable to the full path of the Java home directory:</p>
<pre>export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64</pre>
<p>You can verify your JAVA_HOME setting using this command:</p>
<pre>$JAVA_HOME/bin/java -version</pre>
<p>That&#8217;s it. We can now continue installing the AWS EC2 CLI tools.</p>
<p>&nbsp;</p>
<h3>Amazon EC2 CLI Tools</h3>
<h4>Download EC2 API tools</h4>
<pre>#Go to temp directory
cd /tmp

#Download the latest version of the EC2 CLI tools
wget http://s3.amazonaws.com/ec2-downloads/ec2-api-tools.zip

#Unzip the downloaded file
unzip ec2-api-tools.zip
</pre>
<p>&nbsp;</p>
<h4>Install EC2 API tools</h4>
<pre>#Create directory to install the EC2 CLI tools
mkdir /usr/local/ec2/apitools

#Move the downloaded tools to custom directory (replace version with your own version if other)
mv ec2-api-tools-1.7.1.1/* /usr/local/ec2/apitools
</pre>
<p>&nbsp;</p>
<h4>Set variables</h4>
<pre>#Set the EC2_HOME environment variable:
export EC2_HOME=/usr/local/ec2/apitools

#Update the PATH environment variable
export PATH=$PATH:$EC2_HOME/bin
</pre>
<p>&nbsp;</p>
<h4>Add variables to startup script, so they are widely available (Optional)</h4>
<pre>#Go to profile.d directory
cd etc/profile.d

#Create a new bash script
vi aws.sh
</pre>
<p>You can now add the variables into the script (we use vi as default editor):</p>
<pre>export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
export EC2_HOME=/usr/local/ec2/apitools
export PATH=$PATH:$EC2_HOME/bin
</pre>
<p>Save and close the file</p>
<h3>Create IAM Backup User</h3>
<p>It&#8217;s a common best practice to create a separate account with limited access rights for these kind of purpose.</p>
<ul>
<li>Go to the IAM Console</li>
<li>Create a user &#8220;backup-ebs-user&#8221; (or a name of your own choice)</li>
</ul>
<p>→ Follow the <a href="http://docs.aws.amazon.com/IAM/latest/UserGuide/Using_SettingUpUser.html" target="_blank" rel="noopener noreferrer">AWS instructions</a> for adding a new IAM User<br />
→ Take a note of the user and security credentials for future usage</p>
<ul>
<li>Assign policy to the backup user, so only required access rights are assigned to the user</li>
</ul>
<p>→ Follow <a href="http://docs.aws.amazon.com/IAM/latest/UserGuide/Using_SettingUpUser.html" target="_blank" rel="noopener noreferrer">AWS instructions</a> for an overview of the IAM user polices</p>
<ul>
<li>Below is a sample policy that contains the IAM permissions required to run ec2-automate-backup.sh</li>
</ul>
<pre>{
	"Statement": [
		{
			"Action": [
				"ec2:DescribeVolumes",
				"ec2:CreateSnapshot",
				"ec2:DescribeSnapshots",
				"ec2:DeleteSnapshot",
				"ec2:CreateTags",
				"ec2:DescribeTags"
			],
			"Effect": "Allow",
			"Resource": [
				"*"
			]
		}
	]
}
</pre>
<p>&nbsp;</p>
<h2>Install the ec2-automate-backup script</h2>
<pre>#Go to EC2 directory
cd /usr/local/ec2

#Create script directory
Mkdir scripts

#Go to script directory
Cd scripts

# Download the script
wget https://raw.githubusercontent.com/colinbjohnson/aws-missing-tools/master/ec2-automate-backup/ec2-automate-backup.sh

#Make the downloaded file executable by everyone
chmod +x ec2-automate-backup.sh
</pre>
<p>The ec2-automate-backup script allows you to specify a source file for environmental configuration. This is interesting for running the script as a cronjob. An example cron primer file is located at <a href="https://github.com/colinbjohnson/aws-missing-tools/tree/master/ec2-automate-backup/Resources" target="_blank" rel="noopener noreferrer">https://github.com/colinbjohnson/aws-missing-tools/tree/master/ec2-automate-backup/Resources</a>.<br />
Let&#8217;s download the file and modify it according to our specific needs:</p>
<pre>#Download the cron-primer.sh example file
wget https://raw.githubusercontent.com/colinbjohnson/aws-missing-tools/master/ec2-automate-backup/Resources/cron-primer.sh

#Make the downloaded file executable by everyone
chmod +x cron-primer.sh
</pre>
<p>Now edit the cron-primer.sh script so custom variables are available to cron<br />
→ (don&#8217;t forget to add your Access/Secret key)<br />
Our edited cron-primer.sh file:</p>
<pre>#!/bin/bash -
# EC2_HOME required for EC2 API Tools
export EC2_HOME=/usr/local/ec2/apitools
# JAVA_HOME required for EC2 API Tools
export JAVA_HOME=/usr/lib/jvm/jre-1.7.0-openjdk.x86_64
# export PATH=/bin is required for cut, date, grep
# export PATH=/opt/aws/bin/ is required for EC2 API Tools
export PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/ec2/apitools/bin/
export AWS_ACCESS_KEY=xxxxxxxxxxxxxxxxxxxxxxxxx
export AWS_SECRET_KEY=xxxxxxxxxxxxxxxxxxxxxxxxx
</pre>
<p>&nbsp;</p>
<h2>Add custom Backup TAG to your EBS Volumes</h2>
<p>Because we only want to backup the volumes that have a Backup Tag value set to &#8216;true&#8217;, we need to add these tags to our EBS volumes. This has the advantage of leaving the scripts untouched and it enables us to manage the volumes we want snapshot through the AWS console.<br />
Just add the Backup TAG and set its value to &#8216;true&#8217; as shown in the example below:</p>
<p><a href="https://cloudar.be/wp-content/uploads/2015/05/Screen-Shot-2014-09-08-at-17.28.50.png"><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-657" src="https://cloudar.be/wp-content/uploads/2015/05/Screen-Shot-2014-09-08-at-17.28.50.png" alt="Backup Tags" width="1002" height="440" /></a></p>
<p>&nbsp;</p>
<h2>Create Cron job</h2>
<p>In the example below, we created a cron job that runs daily at 03:00AM.</p>
<pre>0 03 * * * /usr/local/ec2/scripts/ec2-automate-backup.sh -r eu-west-1 -s tag -t 'Backup=true' -k 7 -p -n -c /usr/local/ec2/scripts/cron-primer.sh
</pre>
<p>This cron job creates a snapshot of all EBS volumes which reside in the &#8216;eu-west-1&#8217; region and that have the Backup Tag value set to &#8216;true&#8217;. Snapshots are being retained for 7 days.</p>
<p>We hope this blogpost can be of some assistance in helping you to automate some manual tasks. Feel free to comment or modify the script for your own purposes.</p>
<h4>References we used</h4>
<p>→ <a title="EC2 Automatic Backup Script by Collin Johnson" href="https://github.com/colinbjohnson/aws-missing-tools/tree/master/ec2-automate-backup" target="_blank" rel="noopener noreferrer">https://github.com/colinbjohnson/aws-missing-tools/tree/master/ec2-automate-backup</a></p>
<p>→ <a title="Amazon EC2 Command Line Interface Tools Documentation" href="http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/set-up-ec2-cli-linux.html" target="_blank" rel="noopener noreferrer">http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/set-up-ec2-cli-linux.html</a></p>
<p>The post <a href="https://cloudar.be/awsblog/automating-snapshotsbackups-of-ec2-ebs-volumes/">Automating snapshots of EC2 EBS volumes</a> appeared first on <a href="https://cloudar.be">Cloudar</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://cloudar.be/awsblog/automating-snapshotsbackups-of-ec2-ebs-volumes/feed/</wfw:commentRss>
			<slash:comments>9</slash:comments>
		
		
			</item>
	</channel>
</rss>
