Scheduled disk image backup using Linode API
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>