Forked from ehlertij/windows_ec2_snapshot.bat
Last active
September 13, 2016 21:09
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to newest AWS-CLI syntax. Added a couple user variables to statements.