aws ec2 snapshots
- Category: 電腦相關
- Last Updated: Friday, 05 January 2018 22:01
- Published: Thursday, 04 January 2018 14:57
- Written by sam
剛好公司有幾個不同訂閱帳號
試試弄一下拆開的instances region aws-passwd
for i 指的是拆開的帳號,並讀出裡面的值 instances, region, markname
第二個是對映instance 需要使用的帳號
最後是將這個bash排程到cron,並重啟cron即可
當然如果可以在一開始就將instances 打上tag,就可以直接把instances清單撈出來
當然也可以後補一下,但在做的時候沒補…就只能手動了
#!/bin/bash
#################################
#Createby Sam. 2018/01/03#
#################################
export PATH=$PATH:/usr/local/bin/:/usr/bin
set -ue
set -o pipefail
#
logfile="/var/log/ebs-snapshot.log"
logfile_max_lines="5000"
log_setup() {
echo "" > "$logfile" && ( [ -e "$logfile" ] || touch "$logfile" ) && [ ! -w "$logfile" ] && echo "ERROR: Cannot write to $logfile. Check permissions or sudo access." && exit 1
tmplog=$(tail -n $logfile_max_lines $logfile 2>/dev/null) && echo "${tmplog}" > $logfile
exec > >(tee -a $logfile)
exec 2>&1
}
log_setup
{
for i in banclogix-testlab banclogix-dm2 banclogix-efx banclogix-gc banclogix-website; do
ACC=`cat banclogix-passwd |grep $i |awk '{print $2}'`
ACCKEY=`cat banclogix-passwd |grep $i |awk '{print $3}'`
export AWS_ACCESS_KEY_ID=$ACC
export AWS_SECRET_ACCESS_KEY=$ACCKEY
while read instance_id region markname; do
snapshot_volumes() {
for volume_id in $volume_list; do
echo "$(date +"%Y-%m-%d""%T")-subscriptionacc:$i Volume ID is $volume_id instances-name:$markname "
device_name=$(aws ec2 describe-volumes --region $region --output=text --volume-ids $volume_id --query 'Volumes[0].{Devices:Attachments[0].Device}')
snapshot_description="Create-from-$instance_id-$volume_id-$device_name-backup-$(date +%Y-%m-%d)"
snapshot_id=$(aws ec2 create-snapshot --region $region --output=text --description $snapshot_description --volume-id $volume_id --query SnapshotId)
echo "$(date +"%Y-%m-%d""%T")-New snapshot is $snapshot_id"
aws ec2 create-tags --region $region --resource $snapshot_id --tags Key=CreatedBy,Value=AutomatedBackupSam
done
}
#log_setup
volume_list=$(aws ec2 describe-volumes --region $region --filters Name=attachment.instance-id,Values=$instance_id --query Volumes[].VolumeId --output text)
snapshot_volumes
done < $i
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
done
} >> $logfile
mutt -s "Auto snapshot ec2 instances $(date +%Y-%m-%d)" This email address is being protected from spambots. You need JavaScript enabled to view it. < $logfile
testlab AKd3IAJU7XdIUcO2Q2WQ6A bTCRTILiwc1TOHd4zV3Fxbp8nUOGmvZsMWmLl++rcjie
website
efx
gc
dm2
i-0e64e2521ee0bb4c ap-southeast-1 123
i-080e36x9a2d7e1b4 ap-southeast-1 456
i-0a2cdlb3a2093b47 ap-southeast-1 789