您的位置:首页 > 其它

使用awscli创建ec2自动快照(全量备份)

2017-12-15 14:16 639 查看

awscli介绍

官方说明:The AWS Command Line Interface is a unified tool to manage your AWS services.
基本语法为:aws [options] <command> <subcommand> [parameters]

选项

--debug (boolean)
打开debug日志.
--endpoint-url (string)
使用指定的url替换默认url.
--no-verify-ssl (boolean)
默认情况,aws cli使用 SSL连接aws服务,每个SSL连接 awscli都会去认证SSL 证书,此选项更改连接是不去认证。
--no-paginate (boolean)
取消自动分页
--output (string)
输出格式.
json
text
table
--query (string)
用于筛选响应数据的 JMESPath 查询.
--profile (string)
使用凭据文件中的特定配置文件.
--region (string)
使用指定region替换配置文件、环境变量中的设置
--version (string)
显示awscli版本号
--color (string)
打开、关闭输出颜色控制.
on
off
auto
--no-sign-request (boolean)
不对请求进行签名认证。如果使用了此参数, 则不会加载凭据.
--ca-bundle (string)
验证 SSL 证书时要使用的 CA 证书捆绑。替换配置/环境设置。
--cli-read-timeout (int)
最大套接字读取时间 (秒)。如果该值设置为 0, 则套接字读取将被阻塞, 而不是超时。
--cli-connect-timeout (int)
最大套接字连接时间 (秒)。如果该值设置为 0, 则套接字读取将被阻塞, 而不是超时。

ec2 ebs快照

使用awscli创建ec2 ebs快照非常方便,使用ec2命令中的create-snapshot即可
语法:
create-snapshot [--description <value>] --volume-id <value> [--dry-run | --no-dry-run] [--cli-input-json <value>] [--generate-cli-skeleton <value>]
创建快照
如给卷 ID 为
vol-1234567890abcdef0
创建快照并添加描述.
命令:
aws ec2 create-snapshot --volume-id vol-1234567890abcdef0 --description "This is my root volume snapshot."
输出:
{    "Description": "This is my root volume snapshot.",    "Tags": [],    "VolumeId": "vol-1234567890abcdef0",    "State": "pending",    "VolumeSize": 8,    "StartTime": "2014-02-28T21:06:01.000Z",    "OwnerId": "012345678910",    "SnapshotId": "snap-066877671789bd71b" }

全量备份

由于ebs快照为增量快照,其实也不需要删除原来的快照,删除原来的快照也不会释放多余的存储空间,新的快照仍然会使用原快照的存储的数据,实现完整数据备份。
我的全量备份策略如下:
1、每周一次ebs快照,每个ebs仅保留两周(两个)快照;
2、每个快照的打上标签,标签为ec2 Name标签+卷ID+备份时间
3、每个快照描述为 : 备份时间+UTC create snapshot for ec2 标签 卷 ID by awscli用户 on host: 执行命令用户@执行命令服务器名。
4、删除已不存在卷(实例删除,卷删除)的历史自动快照,不影响手动快照
用到命令:
aws iam :https://docs.aws.amazon.com/cli/latest/reference/iam/index.html
aws ec2 describe-instances :https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html
aws ec2 create-snapshot : https://docs.aws.amazon.com/cli/latest/reference/ec2/create-snapshot.html
aws ec2 create-tags : https://docs.aws.amazon.com/cli/latest/reference/ec2/create-tags.html
aws ec2 delete-snapshot : https://docs.aws.amazon.com/cli/latest/reference/ec2/create-snapshot.html
具体实现:
#!/bin/bash
#create ec2 ebs snapshot per week.
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
#init awscli
#yum install -y python-pip
#pip install awscli
#aws configure
log_dir=/tmp/ec2-snapshot/
[ -d ${log_dir} ] || mkdir -p ${log_dir}
aws_user=$(aws iam get-user | awk -F\" '/UserName/{print $4}')
#获取实例id
for instance_id in $(aws ec2 describe-instances | awk -F\" '/InstanceId/{print $4}') ; do
#获取实例标签名
tag=$(aws ec2 describe-instances --instance-ids ${instance_id} | grep -C1 "\"Key\": \"Name\"" | awk -F\" '/Value/{print $4}')
#通过实例id获取该实例挂载的卷id
volumes=$(aws ec2 describe-instances --instance-ids  ${instance_id} | awk -F\" '/VolumeId/{print $4}')
#通过卷id创建快照
for volume in $volumes ; do
aws ec2 create-snapshot \
--volume-id $volume \
--description "$(date +%F\ %T) UTC create snapshot for $tag $volume by $aws_user on host: $(whoami)@$(hostname)" \
&>> ${log_dir}create-snapshot-history.log
#获取快照id
snap_shotId=$(tail -11 ${log_dir}create-snapshot-history.log | awk -F\" '/SnapshotId/{print $4}')
sleep 10
#创建快照标签名字,实例标签_时期
aws ec2 create-tags --resources ${snap_shotId} --tags Key=Name,Value=${tag}_${volume}_$(date +%Y%m%d)
echo ${snap_shotId} >> ${log_dir}$volume.log
#判断每个卷的快照数,大于3个,删除超过3个的最久的快照
num_snap=$(wc -l ${log_dir}$volume.log | awk '{print $1}')
if [ ${num_snap} -gt 3 ] ;then
old_snapshotid=$(head -$[${num_snap}-3] ${log_dir}$volume.log)
for delid in ${old_snapshotid} ;do
aws ec2 delete-snapshot --snapshot-id ${delid} 2>> ${log_dir}del-snapshot-error.log
sed -i '/'"$delid"'/d' ${log_dir}$volume.log
done
fi
done
done
#删除已不存在卷的历史自动快照,不影响手动快照
#获取现有卷列表
vol_n=$(aws ec2 describe-instances | awk -F\" '/VolumeId/{print $4}')
cd ${log_dir}
for vol_log in vol-*.log ; do
#历史卷id
vol_id=${vol_log%.log}
#判断历史卷id是否在现有卷列表中,不存在则删除这个卷的所有快照
if [ "${vol_n}" == "${vol_n/${vol_id}/#}" ] ; then
while read sanp_his ; do
aws ec2 delete-snapshot --snapshot-id ${sanp_his} 2>> ${log_dir}del-snapshot-error.log
done < ${vol_log}
rm -f ${vol_log}
fi
done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  aws ec2 快照