Skip to content

Instantly share code, notes, and snippets.

@Rhilip
Created December 8, 2018 13:45
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Rhilip/8c7b8579ac115f84a5ede70f83ad69d2 to your computer and use it in GitHub Desktop.
Save Rhilip/8c7b8579ac115f84a5ede70f83ad69d2 to your computer and use it in GitHub Desktop.
Automatic snapshots using Vultr API (Python3)

Thanks to Automated Snapshots / Backups via Vultr API And this is Python3 version script used to Automatic snapshots using Vultr API


A more customizable alternative to Vultr's backup feature using Vultr API v1. Tested on Ubuntu 16.04. Run this python3 script on Anywhere to create a snapshot of VPS (which you know IP or it's subid) and rotate out the oldest snapshot(s). Use the BACKUP_TAG_PREFIX prefix field to uniquely id a set of snapshots.

Notice: You can only create 11 snapshots by default.


  1. Set API_KEY to your Vultr API key.
  2. Set MAX_NUM_OF_BACKUPS to specify how many snapshots to keep.
  3. Test it and then add vultr_auto_snapshot.py to your cron tab.
  4. Epic winning at life.

For example, add the crob jobs below in order to create 3 daily, weekly, and monthly snapshots. The oldest snapshots will be automatically rotated out.

0 0 * * * /usr/bin/python3 /home/user/scripts/vultr_auto_snapshot.py
import re
import time
import datetime
import requests
# ------------- User setting START -------------- #
# your vultr api_key
API_KEY = ""
# The server's ip or subid which
MAIN_IP = ""
SUBID = None
BACKUP_TAG_PREFIX = "auto_backup"
MAX_NUM_OF_BACKUPS = 5
# ------------- User setting END -------------- #
# Get base info
api_endpoint = "https://api.vultr.com/v1/"
day = time.strftime("%Y-%m-%d", time.localtime())
# simple wrapper to access vultr api
def vultr(method = "GET",action = "" , data = None):
return requests.request(method,"{}{}".format(api_endpoint,action),headers = {"API-Key" : API_KEY}, data=data)
server_list = vultr("GET","server/list").json()
# Find subid if not set.
if SUBID == None:
for server_subid,server_info in server_list.items():
if server_info.get("main_ip", None) == MAIN_IP:
SUBID = server_subid
break
if SUBID == None:
raise Exception("Fail to find subid for IP: {}".format(MAIN_IP))
snapshot_list_raw = vultr("GET","snapshot/list").json()
# Resort the raw snapshot list dict to list obj
snapshot_list = [v for k,v in snapshot_list_raw.items()]
# Get auto-backup snapshot list
backup_snapshot_list = list(filter(lambda x:re.search("{}-{}".format(BACKUP_TAG_PREFIX,SUBID),x["description"]),snapshot_list))
# Remove old auto-backup-snapshot
if len(backup_snapshot_list) >= MAX_NUM_OF_BACKUPS:
to_remove_snapshot_list = sorted(backup_snapshot_list,
key = lambda k:datetime.datetime.strptime(k["date_created"],"%Y-%m-%d %H:%M:%S")
)[:-MAX_NUM_OF_BACKUPS]
for s in to_remove_snapshot_list:
vultr("POST","snapshot/destroy",{"SNAPSHOTID": s["SNAPSHOTID"]})
# create New auto-backup-snapshot
vultr("POST","snapshot/create",{"SUBID": SUBID,"description": "{}-{}-{}".format(BACKUP_TAG_PREFIX,SUBID,day)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment