Skip to content

Instantly share code, notes, and snippets.

@Tracnac
Last active September 23, 2022 16:03
Show Gist options
  • Select an option

  • Save Tracnac/a3d2faf3299d6154468188099bd481e2 to your computer and use it in GitHub Desktop.

Select an option

Save Tracnac/a3d2faf3299d6154468188099bd481e2 to your computer and use it in GitHub Desktop.
AWS Snapshot RDS #aws #shell
#!/bin/sh
# Week/Month snapshot

# Variables :
_debug=0
source .bashrc

# Find the last automatic aws snapshot
export date_of_snapshot_to_save=$(date +"%Y-%m-%d")

# Date
if [ ${_debug} -eq 0 ]; then
    export today_day=$(date +"%a")
    export today_day_number=$(date +"%d")
    export today_week_number=$(date +"%V")
    export today_month=$(date +"%m")
export year=$(date +"%Y")
else
    echo "Debug mode on"
    export today_day="Sun"
    export today_day_number=15
    export today_week_number=$(date +"%V")
    export today_month=$(date +"%m")
export year=$(date +"%Y")
fi

echo
echo "/*"
echo "Snapshot : ${date_of_snapshot_to_save}"
echo "Day : ${today_day}"
echo "Day : ${today_day_number}"
echo "Week number : ${today_week_number}"
echo "Month number : ${today_month}"
echo "Year Number : ${year}"
echo "*/"
echo

# We snap only weekend and month as daily is automatic...
for db_instance_identifier in $(aws rds describe-db-snapshots --query "DBSnapshots[?SnapshotCreateTime>='${date_of_snapshot_to_save}' && SnapshotType=='automated'].["DBInstanceIdentifier"]" --output text); do
    for snapshot_identifier in $(aws rds describe-db-snapshots --query "DBSnapshots[?SnapshotCreateTime>='${date_of_snapshot_to_save}' && DBInstanceIdentifier=='${db_instance_identifier}' && SnapshotType=='automated'].[DBSnapshotIdentifier]" --output text); do
        echo
echo "DBInstanceIdentifier : ${db_instance_identifier}"
        echo "DBSnapshotIdentifier : ${snapshot_identifier}"
        # Weekly Snapshot
        if [ "${today_day}" = "Sun" ]; then
            echo "Week snapshot"
            aws rds copy-db-snapshot --source-db-snapshot-identifier "${snapshot_identifier}" --target-db-snapshot-identifier "${db_instance_identifier}-week-${today_week_number}" --copy-tags
            # Add backup tag
            arn_resource=$(aws rds describe-db-snapshots --query "DBSnapshots[?DBSnapshotIdentifier=='${db_instance_identifier}-week-${today_week_number}'].[DBSnapshotArn]" --output text)
            echo ${arn_resource}
            aws rds add-tags-to-resource --resource-name "${arn_resource}" --tags Key="ADP_Backup_Weekly",Value="${today_week_number}"
            echo
        fi

        # Monthly Snapshot
        if [ \( "${today_day}" = "Sun" \) -a \( ${today_day_number} -ge 1 \) -a \( ${today_day_number} -le 7 \) ]; then
            echo "Month snapshot"
            aws rds copy-db-snapshot --source-db-snapshot-identifier "${snapshot_identifier}" --target-db-snapshot-identifier "${db_instance_identifier}-month-${today_month}" --copy-tags
            # Add backup tag
            arn_resource=$(aws rds describe-db-snapshots --query "DBSnapshots[?DBSnapshotIdentifier=='${db_instance_identifier}-month-${today_week_number}'].[DBSnapshotArn]" --output text)
            echo ${arn_resource}
            aws rds add-tags-to-resource --resource-name "${arn_resource}" --tags Key="ADP_Backup_Monthly",Value="${today_month}"
            echo
        fi
    done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment