Skip to content

Instantly share code, notes, and snippets.

@bmatthewshea
Forked from ehlertij/windows_ec2_snapshot.bat
Last active September 13, 2016 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmatthewshea/944abb2a441b7f006345335ba76d0c31 to your computer and use it in GitHub Desktop.
Save bmatthewshea/944abb2a441b7f006345335ba76d0c31 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_EC2_COMMAND=aws ec2 --dryrun
set AWS_HOME=.
set AWS_VOLUME=vol-12345678
set AWS_SNAPSHOT_KEEP=10
set VOL_DESC=Daily Snapshot
:: Create snapshot for this volume
CMD /C %AWS_EC2_COMMAND% create-snapshot %AWS_VOLUME% -d "%VOL_DESC%"
:: Find old snapshots for this volume, sort them by date desc
%AWS_EC2_COMMAND% describe-snapshots --filters Name="volume-id",Values="%AWS_VOLUME%" Name="status",Values="completed"|find /I "%VOL_DESC%"|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 %AWS_EC2_COMMAND% delete-snapshot %%s
@bmatthewshea
Copy link
Author

Updated to newest AWS-CLI syntax. Added a couple user variables to statements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment