Skip to content

Instantly share code, notes, and snippets.

@ehlertij
Last active August 11, 2017 01:24
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ehlertij/6107597 to your computer and use it in GitHub Desktop.
Windows batch script for creating daily EC2 snapshots for a volume and deleting old ones.
set AWS_HOME=C:\AWS
set AWS_VOLUME=vol-12345678
set AWS_SNAPSHOT_KEEP=10
:: Create snapshot for this volume
CMD /C ec2-create-snapshot %AWS_VOLUME% -d "Daily Snapshot"
:: Find old snapshots for this volume, sort them by date desc
ec2-describe-snapshots -F "volume-id=%AWS_VOLUME%" -F "status=completed"|find /I "Daily Snapshot"|sort /R /+49>%AWS_HOME%\snapshots.txt
:: Loop over old snapshots, skip the first 10, delete the rest
for /f "tokens=2 skip=%AWS_SNAPSHOT_KEEP%" %%s in (%AWS_HOME%\snapshots.txt) do ec2-delete-snapshot %%s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment