Vultr: Auto backup your servers with Snapshots
![Vultr Backup](/_next/image?url=https%3A%2F%2Fassets.harrytang.xyz%2Fprod%2Fvultr_backup_ef681df202.jpeg&w=3840&q=75)
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
-
Get the Vultr API key Login to your Vultr account, go to Account \ API and get your Personal Access Token.
-
Create a script for auto-taking snapshots SSH to your server, add two files,
snapshot.sh
andSNAPSHOTID
, 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), andDESC
with any text would help you easily identify the snapshot for the server. -
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)!