Vultr: Auto backup your servers with Snapshots

Vultr Backup
Vultr Backup

Introduction

Having an auto backup system for all your servers is crucial, 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 two backups stored.

Prerequisites

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

Step-by-step Guide

  1. Get the Vultr API key Login to your Vultr account, go to Account \ API and get your Personal Access Token.

  2. Create a script for auto-taking snapshots SSH to your server, add two files, snapshot.sh and SNAPSHOTID, and 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 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.

  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
    

Conclusion

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