Scheduled disk image backup using Linode API

Use this script to backup your Linode disk image on a regular basis. It assumes that the disk for backup is labeled as Live – you can either rename the disk image in Linode dashboard or adjust the script. You will also need available storage within your account to replicate the disk image. The script uses wget command so make sure to install it first if you don't have it.

You can put this script on crontab to run every day even from the system that you need to back up. This is only the initial idea and you can also modify this script to make weekly backups, keep several rotating backups, etc.

#!/bin/bash

# fill this in with your args
apikey= <your-linode-api-key>linodeid= <your-linode-id>baseurl="https://api.linode.com/"

# get the id of volume to backup which is named Live

liveid=`wget -qO- "$baseurl?api_key=$apikey&api_action=linode.disk.list&LinodeID=$linodeid" |grep -o '{[^}]*"LABEL":"Live"[^}]*"DISKID":[0-9]*}' | grep -o '"DISKID":[0-9]*' | grep -o '[0-9]*'`

# get the id of current backup named Daily-YYYY-MM-DD

dailyid=`wget -qO- "$baseurl?api_key=$apikey&api_action=linode.disk.list&LinodeID=$linodeid" |grep -o '{[^}]*"LABEL":"Daily[0-9\-]*"[^}]*"DISKID":[0-9]*}' | grep -o '"DISKID":[0-9]*' | grep -o '[0-9]*'`

# delete current backup to free up disk space for new backup

if [ "$dailyid" != "" ]; then
  delete=`wget -qO- "$baseurl?api_key=$apikey&api_action=linode.disk.delete&LinodeID=$linodeid&DiskID=$dailyid"`
  jobid=`echo $delete | grep -i -o '"JobID":[0-9]*' | grep -o '[0-9]*'`
  # wait for up to five minutes for this job to finish
  for (( start = 0; start < 60; start++ )); do
    jobs=`wget -qO- "$baseurl?api_key=$apikey&api_action=linode.job.list&LinodeID=$linodeid&pendingOnly=1" | grep $jobid`
    if [ "$jobs" == "" ]; then
      break
    fi
    sleep 5s
  done
fi

# make a copy of the volume

duplicate=`wget -qO- "$baseurl?api_key=$apikey&api_action=linode.disk.duplicate&LinodeID=$linodeid&DiskID=$liveid"`
dailyid=`echo $duplicate | grep -i -o '"DiskID":[0-9]*' | grep -o '[0-9]*'`
sleep 5s

# rename the volume copy

daystamp=`date +"%Y-%m-%d"`
wget -qO- "$baseurl?api_key=$apikey&api_action=linode.disk.update&LinodeID=$linodeid&DiskID=$dailyid&Label=Daily-$daystamp"</your-linode-id></your-linode-api-key> 

0 Replies

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct