linux – 如何自动为Amazon EC2实例的卷创建快照?

前端之家收集整理的这篇文章主要介绍了linux – 如何自动为Amazon EC2实例的卷创建快照?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试使用脚本自动备份卷.

我按照github上的EBS-Snapshot.sh脚本进行操作:

#!/bin/bash

# export EC2_HOME='/etc/ec2'  # Make sure you use the API tools,not the AMI tools
# export EC2_BIN=$EC2_HOME/bin
# export PATH=$PATH:$EC2_BIN
# I know all of the above is good to have solution,but not re-usable
# I have captured all of the above in a particular file and lemme execute it
source /etc/environment

PURGE_SNAPSHOT_IN_DAYS=10

EC2_BIN=$EC2_HOME/bin

# store the certificates and private key to your amazon account
MY_CERT='/path/to/certificate-file'
MY_KEY='/path/to/private-file'
# fetching the instance-id from the Metadata repository
MY_INSTANCE_ID='your ec2-instance-id'

# temproary file
TMP_FILE='/tmp/rock-ebs-info.txt'

# get list of locally attached volumes via EC2 API:
$EC2_BIN/ec2-describe-volumes -C $MY_CERT -K $MY_KEY > $TMP_FILE
VOLUME_LIST=$(cat $TMP_FILE | grep ${MY_INSTANCE_ID} | awk '{ print $2 }')

sync

#create the snapshots
echo "Create EBS Volume Snapshot - Process started at $(date +%m-%d-%Y-%T)"
echo ""
echo $VOLUME_LIST
for volume in $(echo $VOLUME_LIST); do
   NAME=$(cat $TMP_FILE | grep Name | grep $volume | awk '{ print $5 }')
   DESC=$NAME-$(date +%m-%d-%Y)
   echo "Creating Snapshot for the volume: $volume with description: $DESC"
   echo "Snapshot info below:"
   $EC2_BIN/ec2-create-snapshot -C $MY_CERT -K $MY_KEY -d $DESC $volume
   echo ""
done

echo "Process ended at $(date +%m-%d-%Y-%T)"
echo ""

rm -f $TMP_FILE

#remove those snapshot which are $PURGE_SNAPSHOT_IN_DAYS old

我有两个用于X509身份验证的文件,实例ID,但我不了解脚本以及如何参数化我要备份的卷.

我不明白第一行(源)和EC2_BIN.
通过该配置,它列出了所有这些卷并创建了所有这些卷的快照……

对于快照的注释,如何更改此行以添加文本?

DESC=$NAME-$(date +%m-%d-%Y)

我很抱歉成为一名初学者,但我不明白整个剧本

编辑:

我用这个新代码得到了这个错误

Creating Snapshot for the volume: ([ec2-describe-volumes]) with
description: -03-13-2012 Snapshot info below:
Client.InvalidParameterValue: Value (([ec2-describe-volumes])) for
parameter volumeId is invalid. Expected: ‘vol-…’. Process ended at
03-13-2012-08:11:35 –

这是代码

#!/bin/bash

#Java home for debian default install path:
export JAVA_HOME=/usr
#add ec2 tools to default path
#export PATH=~/.ec2/bin:$PATH


#export EC2_HOME='/etc/ec2'  # Make sure you use the API tools,not the AMI tools
export EC2_BIN=/usr/bin/
#export PATH=$PATH:$EC2_BIN
# I know all of the above is good to have solution,but not re-usable
# I have captured all of the above in a particular file and lemme execute it
source /etc/environment

PURGE_SNAPSHOT_IN_DAYS=60

#EC2_BIN=$EC2_HOME/bin

# store the certificates and private key to your amazon account
MY_CERT='cert-xx.pem'
MY_KEY='pk-xx.pem'
# fetching the instance-id from the Metadata repository

MY_INSTANCE_ID=`curl http://169.254.169.254/1.0/Meta-data/instance-id`

# temproary file
TMP_FILE='/tmp/rock-ebs-info.txt'

# get list of locally attached volumes via EC2 API:
$EC2_BIN/ec2-describe-volumes -C $MY_CERT -K $MY_KEY > $TMP_FILE

#VOLUME_LIST=$(cat $TMP_FILE | grep ${MY_INSTANCE_ID} | awk '{ print $2 }')
VOLUME_LIST=(`ec2-describe-volumes --filter attachment.instance-id=$MY_INSTANCE_ID | awk '{ print $2 }'`)

sync

#create the snapshots
echo "Create EBS Volume Snapshot - Process started at $(date +%m-%d-%Y-%T)"
echo ""
echo $VOLUME_LIST
echo "-------------"
for volume in $(echo $VOLUME_LIST); do
   NAME=$(cat $TMP_FILE | grep Name | grep $volume | awk '{ print $5 }')
   DESC=$NAME-$(date +%m-%d-%Y)
   echo "Creating Snapshot for the volume: $volume with description: $DESC"
   echo "Snapshot info below:"
   $EC2_BIN/ec2-create-snapshot -C $MY_CERT -K $MY_KEY -d $DESC $volume
   echo ""
done

echo "Process ended at $(date +%m-%d-%Y-%T)"
echo ""

rm -f $TMP_FILE

#remove those snapshot which are $PURGE_SNAPSHOT_IN_DAYS old
最佳答案
好吧好吧,

>他跑的第一行(来源).那是一样的.在/ etc /环境.无论如何,他所做的只是加载一个文件,其中包含亚马逊所需的环境变量列表.至少这是我的假设.
>他使这个脚本比它需要的复杂得多.他不需要运行ec2-describe-instances命令并将输出保存到文件然后grep输出等….
>你可以把任何你想要的东西放到DESC.您可以将=右侧的所有内容替换为您想要的任何文本.只要确保在它周围加上引号.

我会改变关于这个脚本的两件事.

>在脚本中运行时获取InstanceId.不要硬编码到脚本中.无论脚本在何处运行,此行都将起作用.

MY_INSTANCE_ID=`curl http://169.254.169.254/1.0/Meta-data/instance-id`

>而不是调用ec2-describe-volumes并将输出保存到临时文件等…只需在命令上使用过滤器并告诉它您想要的实例ID.

VOLUME_LIST=(`ec2-describe-volumes --filter attachment.instance-id=$MY_INSTANCE_ID | awk '{ print $2 }'`)

猜你在找的Linux相关文章