Cronjob Snapshot Script for openATTIC

This is the continuation of my last blog post Snapshot Python script with authtoken. I expanded the script a little bit, because I don't want to create the snapshots manually every day and I also want to store the last 10 snapshots.

You can change the maxsnaps variable to adjust the number of stored snapshots.

Snapshot Script:

#!/usr/bin/env python
import requests
import json
import datetime
token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

headers = {'content-type': 'application/json',
           "Authorization": "Token %s" %token}

currentime = datetime.datetime.strftime(, '%Y-%m-%d %H:%M:%S')
maxsnaps = 10

### recorded command 1
        "volumeId": 615,
        "name": currentime,
        "megs": 4577
                })"", data=data, headers=headers)

snaps = requests.get("", headers=headers)
snaps = snaps.json()

if len(snaps["results"]) > maxsnaps:
    delete_amount = snaps["count"] - maxsnaps

if delete_amount > 0:
    for i in range(0, delete_amount):
        oldest = snaps["results"][i]
        print "DELETE oldest %s" % oldest["id"]
        requests.delete("" % oldest["id"], headers=headers)

Now you can create a normal cronjob.

A special thanks goes to Tatjana Dehler.


Comments powered by Disqus