amazon-web-servicesaws-ebs

AWS EBS Snapshot once a month and daily for 7 days


I want make a snapshot every day a week and delete the snapshots older than 7 days but also, at the same time, I want to save one snapshot of the month.

Does somebody knows how to edit this lines to make this?

I use corn every day executing the ebs-snapshot.sh and I need to save, for example, the snapshot maked at first day of month.

 # How many days do you wish to retain backups for? Default: 7 days
retention_days="7"
retention_date_in_seconds=`date +%s --date "$retention_days days ago"`

[...] 

# Purge all instance volume snapshots created by this script that are older than 7 days
for snapshot_id in $(cat /tmp/snapshot_info.txt)
do
    echo "Checking $snapshot_id..."
    snapshot_date=$(aws ec2 describe-snapshots --output=text --snapshot-ids $snapshot_id --query Snapshots[].StartTime | awk -F "T" '{printf "%s\n", $1}')
    snapshot_date_in_seconds=`date "--date=$snapshot_date" +%s`

    if (( $snapshot_date_in_seconds <= $retention_date_in_seconds )); then
        echo "Deleting snapshot $snapshot_id ..." >> $logfile
        aws ec2 delete-snapshot --snapshot-id $snapshot_id
    else
        echo "Not deleting snapshot $snapshot_id ..." >> $logfile
    fi
done

Solution

  • It would probably be easier to Automate the Amazon EBS Snapshot Lifecycle - Amazon Elastic Compute Cloud.

    This feature can automatically create snapshots on a regular schedule, with a defined retention policy.

    Simply configure it to create a daily snapshot and retain it for 7 days.

    As to the monthly snapshot, since you apparently want to keep them forever, just trigger a cron job that creates a snapshot once a month. No retention logic is required, so it would just be a simple aws ec2 create-snapshot command.