Question

Using doctl in bash script

I have this script so far:

#!/bin/bash
# script to create digitalocean droplet snapshot using their CLI, doctl.

# Function to set variable for snapshot name as date/time of creation.
timestamp() {
NAME=$(date)
}

timestamp

doctl compute droplet-action snapshot --snapshot-name "$NAME" 80750079

The script works with the result being a droplet snapshot with the date/time of creation as the name. However, I need to keep only 12 snapshots.

Somehow I need to count the number of existing snapshots and when I have 12, delete the oldest and add a new one.

How can I achieve that?

I run the script every two hours.

Any help would be greatly appreciated!

Thanks!


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

So this is the bashscript. Uses doctl to check how many snapshots exist, and when 24 exist, deletes the oldest one and creates the new one. Sends an log email to me with each new snapshot so I know it’s working. Repeats every hour. Maybe it’ll help someone. :)

#!/bin/bash
exec &> do_snapshot.log

timestamp() {
 NAME=$(date)
}

timestamp

SNAPSHOTS=$(/snap/bin/doctl compute image list-user --format "ID" --no-header | wc -l)

if [ "$SNAPSHOTS" -gt 23 ]; then
mapfile -t IDLIST < <(/snap/bin/doctl compute image list-user --format "ID" --no-header)

OLDEST=${IDLIST[0]}

/snap/bin/doctl compute image delete "$OLDEST"
fi

/snap/bin/doctl compute droplet-action snapshot --snapshot-name "$NAME" 8xxxxxxxxx9

mail -s "Digital Ocean Hourly Snapshot" my email@gmail.com < do_snapshot.log

rm do_snapshot.log

exit 0

Ryan Quinn
DigitalOcean Employee
DigitalOcean Employee badge
April 23, 2018

I think that if you list your snapshots and split that result so that each result is a different item in an array you could do a for loop to count your results and if that number is over 12, delete the oldest.

Personally I would choose a more robust option than bash+doctl for this since it would be easier to deal with the snapshot results if you were using a library/class rather than a command line utility.

BTW, I’ll post the script when completed.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.