Linux 如何自动快照 Amazon EC2 实例的卷?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9667390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to automatically snapshot a volume of an Amazon EC2 instance?
提问by clement
I'm trying a script to backup a volume automatically.
我正在尝试一个脚本来自动备份一个卷。
I follow this EBS-Snapshot.sh
script as found on github:
我按照EBS-Snapshot.sh
在github 上找到的这个脚本进行操作:
#!/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 }')
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 }')
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
I have the two files for X509 authentication, the instance ID but I don't understand the script and how to parameterise the volume that I want to backup.
我有用于 X509 身份验证的两个文件、实例 ID,但我不了解脚本以及如何参数化要备份的卷。
I don't understand the first line (source) and the EC2_BIN. With that configuration, it lists all the volumes and makes a snapshot of all these...
我不明白第一行(来源)和 EC2_BIN。使用该配置,它会列出所有卷并制作所有这些卷的快照......
For the comment of the snapshot, how can I change this line to add text?
对于快照的评论,如何更改此行以添加文本?
DESC=$NAME-$(date +%m-%d-%Y)
I'm sorry to be a beginner but I don't understand the whole script
很抱歉是初学者,但我不明白整个脚本
EDIT :
编辑 :
I get this error with this new code:
我用这个新代码收到这个错误:
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 –
为卷创建快照:([ec2-describe-volumes]) 与描述:-03-13-2012 快照信息如下:Client.InvalidParameterValue:参数 volumeId 的值 (([ec2-describe-volumes])) 无效。预期:'卷-...'。过程结束于 03-13-2012-08:11:35 –
And this is the code :
这是代码:
#!/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 }')
VOLUME_LIST=(`ec2-describe-volumes --filter attachment.instance-id=$MY_INSTANCE_ID | awk '{ print }'`)
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 }')
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
采纳答案by bwight
Ok well,
好吧好吧,
- The first line where he runs (source). Thats the same as . /etc/environment. Anyways all he's doing is loading a file that has a list of environmental variables that amazon requires. At least this is what i assume.
- He's making this script much more complicated than it needs to be. He doesn't need to run the ec2-describe-instances command and save the output to a file then grep the output etc....
- You can put whatever you want for the DESC. You can just replace everything to the right of the = to whatever text you want. Just make sure to put quotes around it.
- 他跑的第一行(来源)。那是一样的。/etc/环境. 无论如何,他所做的只是加载一个文件,其中包含亚马逊所需的环境变量列表。至少这是我的假设。
- 他让这个脚本变得比它需要的复杂得多。他不需要运行 ec2-describe-instances 命令并将输出保存到文件,然后 grep 输出等......
- 您可以为 DESC 放置任何您想要的东西。您可以将 = 右侧的所有内容替换为您想要的任何文本。只要确保在它周围加上引号。
I would change two things about this script.
我会改变关于这个脚本的两件事。
Get the InstanceId at runtime in the script. Don't hard code it into the script. This line will work no matter where the script is running.
MY_INSTANCE_ID=`curl http://169.254.169.254/1.0/meta-data/instance-id`
Instead of calling ec2-describe-volumes and saving the output to a temp file etc... Just use a filter on the command and tell it which instance id you want.
VOLUME_LIST=(`ec2-describe-volumes --filter attachment.instance-id=$MY_INSTANCE_ID | awk '{ print }'`)
在脚本中在运行时获取 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 }'`)
回答by Sebastian Buckpesch
the above solution did not work completely for me. After I hour chat with the amazon support, I have now this working script, which will always create snapshots of all volumes attached to the current instance:
上述解决方案对我来说并不完全有效。在与亚马逊支持人员聊天后,我现在有了这个工作脚本,它将始终创建附加到当前实例的所有卷的快照:
#!/bin/bash
# Set Environment Variables as cron doesn't load them
export JAVA_HOME=/usr/lib/jvm/java-6-sun
export EC2_HOME=/usr
export EC2_BIN=/usr/bin/
export PATH=$PATH:$EC2_HOME/bin
export EC2_CERT=/home/ubuntu/.ec2/cert-SDFRTWFASDFQFEF.pem
export EC2_PRIVATE_KEY=/home/ubuntu/.ec2/pk-SDFRTWFASDFQFEF.pem
export EC2_URL=https://eu-west-1.ec2.amazonaws.com # Setup your availability zone here
# Get instance id of the current server instance
MY_INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
# get list of locally attached volumes
VOLUMES=$(ec2-describe-volumes | grep ${MY_INSTANCE_ID} | awk '{ print }')
echo "Instance-Id: $MY_INSTANCE_ID"
# Create a snapshot for all locally attached volumes
LOG_FILE=/home/ubuntu/ebsbackup/ebsbackup.log
echo "********** Starting backup for instance $MY_INSTANCE_ID" >> $LOG_FILE
for VOLUME in $(echo $VOLUMES); do
echo "Backup Volume: $VOLUME" >> $LOG_FILE
ec2-consistent-snapshot --aws-access-key-id ASDASDASDASD --aws-secret-access-key asdfdsfasdfasdfasdfasdf --mysql --mysql-host localhost --mysql-username root --mysql-password asdfasdfasdfasdfd --description "Backup ($MY_INSTANCE_ID) $(date +'%Y-%m-%d %H:%M:%S')" --region eu-west-1 $VOLUME
done
echo "********** Ran backup: $(date)" >> $LOG_FILE
echo "Completed"
I setup a cronjob in /etc/cron.d/ebsbackup
我在 /etc/cron.d/ebsbackup 中设置了一个 cronjob
01 * * * * ubuntu /home/ubuntu/.ec2/myscriptname
This works pretty good for me... :-)
这对我来说非常好...... :-)
Hope this helps for you, Sebastian
希望这对你有帮助,塞巴斯蒂安
回答by Sirch
Heres a function i wrote in Ruby to snapshot all volumes on all instances in all regions.
这是我用 Ruby 编写的一个函数,用于对所有区域的所有实例上的所有卷进行快照。
require 'aws-sdk'
def snapshot_all_attached_volumes(region)
# For every instance in this region
AWS::EC2.new(:region => region).instances.each do |instance|
# get all the attached volumes
instance.attachments.each do |mountpoint, attachment|
# and create snapshots
attachment.volume.create_snapshot(description = "Automated snapshot #{HOSTNAME}:#{#!/bin/bash
echo "AMI Backup is starting..."
echo "taking AMI Backup..."
day_of_year=$(date +%j)
week_of_year=$(date +%U)
week_of_year=$( printf "%.0f" $week_of_year )
year=$(date +%Y)
for INST in $(ec2-describe-instances --region=sa-east-1 --filter "tag:Backup=On" | awk '/^INSTANCE/ {print }')
do
start_time=$(date +%R)
ami=$(ec2-create-image $INST --name $INST$week_of_year --no-reboot | awk '{print }')
ec2-create-tags $ami --tag Day_Year=$day_of_year > /dev/null
ec2-create-tags $ami --tag Week_Year=$week_of_year > /dev/null
ec2-create-tags $ami --tag Src_Instance=$INST > /dev/null
ec2-create-tags $ami --tag Start_Time=$start_time > /dev/null
end_time=$(date +%R)
ec2-create-tags $ami --tag End_Time=$end_time > /dev/null
echo "Created AMI $ami for volume $INST"
done
year=$(date +%Y)
expire_day=`expr $day_of_year - 2`
expire_week=`expr $week_of_year - 2`
echo "identifying AMI to be deleted"
for delete in $(ec2-describe-images --filter "tag:Week_Year=$expire_week" | awk '{ print ;exit;}')
do
ec2dereg $delete
echo "deleted $delete"
done
}")
end
end
end
regions = AWS::EC2.regions.map(&:name)
regions.each do |region|
begin
snapshot_all_attached_volumes(region)
# delete_all_old_snapshots(region)
rescue
puts "#{$!}"
end
end
回答by Havary Camara
I don't know about you, but I prefer to make AMI instead snapshot. This script came from a idea from Craig, an employee of Amazon. They were developing a snapshot script called Arche. This script is simple - you mark a tag in an EC2 Instance and tag Ec2 are AMIed. I tested it in my environment. You can change the commands in this script to backup the snapshot, too.
我不了解您,但我更喜欢制作 AMI 而不是快照。这个脚本来自亚马逊员工克雷格的一个想法。他们正在开发一个名为 Arche 的快照脚本。这个脚本很简单 - 您在 EC2 实例中标记一个标签,并且标签 Ec2 是 AMIed。我在我的环境中测试了它。您也可以更改此脚本中的命令来备份快照。
Before you run this, config the linux environment variables with cert and pk keys.
在运行之前,使用 cert 和 pk 密钥配置 linux 环境变量。
a. Choose Schedule.
b. Choose Fixed rate of and specify the schedule interval (for example, 5 minutes). Alternatively, choose Cron expression and specify a cron expression (for example, every 15 minutes Monday through Friday, starting at the current time).
回答by Sergio Troiano
I came across with many people looking for a tool to administrate the EBS snapshots. I found several tools in internet but they were just scripts and incomplete solutions. Finally I decided to create a program more flexible, centralized and easy to administrate.
我遇到过很多人在寻找管理 EBS 快照的工具。我在互联网上找到了几个工具,但它们只是脚本和不完整的解决方案。最后,我决定创建一个更灵活、更集中且易于管理的程序。
The idea is to have a centralized program to rule all the EBS snapshots (local to the instance or remotes)
这个想法是有一个集中的程序来管理所有 EBS 快照(实例本地或远程)
I have created a small Perl program, https://github.com/sciclon/EBS_Snapshots
我创建了一个小的 Perl 程序, https://github.com/sciclon/EBS_Snapshots
Some features: * Program runs in daemon mode or script mode (crontab)
一些特点: * 程序运行在守护进程模式或脚本模式(crontab)
You can chose only local attached volumes or remotes as well
You can define log file
You can define for each volume quantity of snapshots
You can define for each volume the frequency among them
Frequency and quantity will work like a "round-robin" when it reaches the limit removing the oldest snapshot.
you can readjust in one step the quantity I mean if you have 6 snapshots and you modify the quantity in 3 the process will readjust it automatically.
You can define a "prescript" execution, You can add your code to execute before executing the snapshot, for example you would like to try to umount the volume or stop some service, or maybe to check the instance load. The parent process will wait for the exit code, "0" means success, you can define if continue or not depending on the exit code.
You can define a "postscript" execution to execute any scrip after taking the snapshot (for example a email telling you about it)
You can add "Protected Snapshots" to skip the snapshot you define, I mean they will be in "read only" and they will never been erased.
you can reconfigure the script "on the fly" when it is running in daemon mode, the script accepts signals and IPC.
It has a "local-cache" to avoid requesting the API several times. You can add or modify any configuration in the config file and reload without killing the process.
您也可以只选择本地附加卷或远程
您可以定义日志文件
您可以为每个卷定义快照数量
您可以为每个卷定义其中的频率
当达到删除最旧快照的限制时,频率和数量将像“循环”一样工作。
您可以一步重新调整数量,我的意思是如果您有 6 个快照并且您在 3 个中修改数量,该过程将自动重新调整它。
您可以定义“prescript”执行,您可以在执行快照之前添加要执行的代码,例如您想尝试卸载卷或停止某些服务,或者可能检查实例负载。父进程将等待退出代码,“0”表示成功,您可以根据退出代码定义是否继续。
您可以定义“postscript”执行以在拍摄快照后执行任何脚本(例如,一封电子邮件告诉您有关它的信息)
您可以添加“受保护的快照”以跳过您定义的快照,我的意思是它们将处于“只读”状态并且永远不会被删除。
当脚本在守护程序模式下运行时,您可以“即时”重新配置脚本,脚本接受信号和 IPC。
它有一个“本地缓存”,以避免多次请求 API。您可以在配置文件中添加或修改任何配置并重新加载而无需终止进程。
回答by user855803
I think the best way now is to use AWS Lambda to take snapshots of your EC2 instances. you can find more details from this link
我认为现在最好的方法是使用 AWS Lambda 来拍摄 EC2 实例的快照。您可以从此链接找到更多详细信息
http://www.iwss.co.uk/ec2-instance-snapshot-through-aws-lambda-function-using-phyton-2-7/
http://www.iwss.co.uk/ec2-instance-snapshot-through-aws-lambda-function-using-phyton-2-7/
回答by Tarun Gupta
Create a rule that takes snapshots on a schedule. You can use a rate expression or a cron expression to specify the schedule. For more information
创建按计划拍摄快照的规则。您可以使用速率表达式或 cron 表达式来指定计划。欲了解更多信息
More information To create a rule
更多信息 创建规则
Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/.
In the navigation pane, choose Events, Create rule.
在https://console.aws.amazon.com/cloudwatch/打开 CloudWatch 控制台。
在导航窗格中,选择事件、创建规则。
For Event Source, do the following:
对于事件源,执行以下操作:
##代码##For Targets, choose Add target and then select EC2 Create Snapshot API call.
For Volume ID, type the volume ID of the targeted Amazon EBS volume.
For AWS permissions, choose the option to create a new role. The new role grants the built-in target permissions to access resources on your behalf.
对于目标,选择添加目标,然后选择 EC2 创建快照 API 调用。
对于 Volume ID,键入目标 Amazon EBS 卷的卷 ID。
对于 AWS 权限,选择创建新角色的选项。新角色授予内置目标权限以代表您访问资源。
Choose Configure details.
选择配置详细信息。
For Rule definition, type a name and description for the rule.
对于规则定义,键入规则的名称和描述。
Choose Create rule
选择创建规则