Vultr: Auto backup your servers with free Snapshots Dec 6, 2018

Overview

It is crucial to have an auto backup system for all your servers, and Vultr does offer this, but it is not free. In this article, I will show you how to build a script to do the backup job automatically with 2 backups stored.

Vultr

Requirements

  • Any Vultr Linux base server. If you do not have one, signup and get free $25 here.
  • Basic knowledge of SSH

Step 1: Get Vultr API key

Login to your Vultr's account, go to Account \ API and get your Personal Access Token.

Step 2: Create a script for auto taking snapshots

SSH to your server, add 2 file name snapshot.sh and SNAPSHOTID, put them in /root

#!/bin/bash

# info
API="XXX"
SUBID="YYY"
DESC="ZZZ"

cd /root

# count
COUNT=$(awk 'END {print NR}' SNAPSHOTID)

# list old snapshots
readarray SNAPSHOTS < SNAPSHOTID

# delete
if [ $COUNT -ge 2 ]; then
# get the 1st to delete
DELID=${SNAPSHOTS[0]}

# delete old snapshot
curl -H 'API-Key: '$API https://api.vultr.com/v1/snapshot/destroy --data 'SNAPSHOTID='$DELID

# update SNAPSHOTID file
sed -i '1d' SNAPSHOTID
fi

# create new snapshot
curl -H 'API-Key: '$API https://api.vultr.com/v1/snapshot/create --data 'SUBID='$SUBID --data 'description='$DESC | grep -Po '"SNAPSHOTID": *"\K\w+' >> SNAPSHOTID

Replace API with your token got from step 1, SUBID with your Vultr server ID (you can get your server ID by looking at the URL when you are viewing your server) and DESC with any text would help you easily identify the snapshot for the server.

Step 3: config crontab to run the script daily

Run crontab -e and add the following line (I assume your OS is Ubuntu)

0 2 * * * /root/snapshot.sh > /dev/null 2>&1

Congratulations, now you have an auto backup system for your Vultr's Server(s)!

 

Advertisement

Latest Updates