Automated Snapshots on Digital Ocean Instead of Backups

What is doctl

Doctl is a command-line interface (CLI) tool for managing and interacting with DigitalOcean resources. There are several benefits to using Doctl, such as its ability to automate complex tasks and streamline workflows. With Doctl, users can easily create, configure, and delete resources such as droplets, volumes, and load balancers. Additionally, Doctl offers a simple and intuitive interface for managing clusters, deploying applications, and monitoring resources. Other benefits of using Doctl include its ability to integrate with popular DevOps tools like Terraform and Ansible, as well as its support for multiple programming languages and platforms. Overall, Doctl provides developers and system administrators with a powerful and flexible tool for managing their DigitalOcean infrastructure.


resourceID='4851361'

function DO-create-snapshot() {
    doctl compute droplet-action snapshot $resourceID --snapshot-name "subdomain.domain.com$(date +%Y-%m-%d-%H-%M-%S)"
}

function DO-delete-last-snapshot() {
    snapshotID=$(doctl compute snapshot list -v --region nyc3 | awk 'NR>1' | sort -k4 | awk 'NR==1{print $1}')
    doctl compute snapshot --interactive=false delete $snapshotID  --force      # non interactive variant suitable for automation
}

# first schedule the creation of the new snapshot and IF successful - delete oldest one
alias DO-rotate-NYC-snapshot="DO-create-snapshot-NYC_DO_COMITIC_COM && DO-delete-last-NYC3-snapshot"

of course, you can do that for many resources and name the methdos accordingly

Hope it helps!