2

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
nfont
  • 99
  • 1
  • 10

1 Answers1

1

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.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • No, monthly snapshot I want to keep them 6 month. Then, I think that I need to edit this code to make de difference between the monthly snapshot and daily snapshot, is that true? Or is it possible to copy this code and make another call in cron every month to the copy edited? example of copy code: retention_days="180 In corn: 0 3 * * * /opt/aws/ebs-snapshot.sh >> /var/log/ebs-snapshotDaily.log 2>&1 0 3 1 * * /opt/aws/ebs-snapshot.sh >> /var/log/ebs-snapshotMonthly.log 2>&1 – nfont Sep 03 '18 at 09:26
  • You can always write your own snapshot code, eg: [AWS EBS snapshotter](https://gist.github.com/nguyendangminh/6669d8ddf3b09427a949a7869f652cc0) – John Rotenstein Sep 03 '18 at 09:43