设为首页 收藏本站
查看: 1737|回复: 0

[经验分享] 使用awscli创建ec2自动快照(全量备份)

[复制链接]

尚未签到

发表于 2019-2-22 11:35:44 | 显示全部楼层 |阅读模式
awscli介绍
  官方说明:The AWS Command Line Interface is a unified tool to manage your AWS services.
  基本语法为:aws [options]   [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 ] --volume-id  [--dry-run | --no-dry-run] [--cli-input-json ] [--generate-cli-skeleton ]  创建快照
  如给卷 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



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-675763-1-1.html 上篇帖子: re:Invent 2018:Amgen和大韩航空押注AWS云计算 下篇帖子: Linux服务器通过aws命令行上传文件至S3
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表