Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Thu May 05, 2011 4:02 am 
Offline

Joined: Tue Jun 26, 2007 2:55 am
Posts: 1
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.

Code:
#!/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"


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group