In this guide, we will show you how to use the TransIP REST API via a PHP script and a cronjob to automatically create snapshots of a VPS at a fixed time.
For the steps in this guide, it is important that the API is enabled in your control panel.
Step 1
Open/create a PHP script in which you set up the automatic snapshots, for example:
nano ~/snapshot.php
Step 2
Give the script the content below. Click on 'Explanation' below the code for further explanation of this setup.
Note: The script only checks if there are snapshots with the name you specify behind 'SNAPSHOT_DESCRIPTION'. This prevents other snapshots with different descriptions from being deleted if you use two or more snapshots. If you use one snapshot before running this script and it has a different description than the value of 'SNAPSHOT_DESCRIPTION', delete it first before running this script.
<?php
require_once(__DIR__ . '/../vendor/autoload.php');
use Transip\Api\Library\TransipAPI;
use Transip\Api\Library\Entity\Vps;
use Transip\Api\Library\Entity\Vps\Snapshot;
class vpsAutoSnapshot
{
private const TRANSIP_LOGIN = 'youraccount';
private const TRANSIP_PRIVATE_KEY = '';
private const VPS_NAME = 'youraccount-vps';
private const SNAPSHOT_DESCRIPTION = 'auto_snapshot';
/**
* @var TransipAPI
*/
private $transipApi;
public function __construct()
{
$this->transipApi = new TransipAPI(
self::TRANSIP_LOGIN,
self::TRANSIP_PRIVATE_KEY,
false
);
}
public function run(): void
{
if ($this->vpsIsLocked(self::VPS_NAME)) {
return; // vps is locked, don't continue
}
$snapshot = $this->getSnapshotByDescription(self::VPS_NAME, self::SNAPSHOT_DESCRIPTION);
if ($snapshot !== null) {
$this->transipApi->vpsSnapshots()->deleteSnapshot(self::VPS_NAME, $snapshot->getName());
}
$this->waitForVpsToUnlock();
$this->transipApi->vpsSnapshots()->createSnapshot(self::VPS_NAME, self::SNAPSHOT_DESCRIPTION);
}
private function vpsIsLocked(string $vpsName): bool
{
$vps = $this->transipApi->vps()->getByName($vpsName);
return ($vps->isCustomerLocked() || $vps->isLocked());
}
private function getSnapshotByDescription(string $vpsName, string $snapshotDescription): ?Snapshot
{
$snapshots = $this->transipApi->vpsSnapshots()->getByVpsName($vpsName);
foreach ($snapshots as $snapshot) {
if ($snapshot->getDescription() == $snapshotDescription) {
return $snapshot;
}
}
return null;
}
private function waitForVpsToUnlock(): void
{
$sleepCount = 0;
while ($this->vpsIsLocked(self::VPS_NAME)) {
if ($sleepCount > 10) {
throw new \RuntimeException('Try out threshold reached, VPS has been locked for more than 2 minutes');
}
sleep(10);
$sleepCount++;
}
}
}
(new vpsAutoSnapshot)->run();
TL;DR
- Replace 'youraccount' with the name of your TransIP account.
- Enter your private key behind TRANSIP_PRIVATE_KEY in the brackets.
- Replace youraccount-vps with the name (not your own description) of the VPS for which you want to create a snapshot.
Explanation
- Our API examples on Gitlab are quite simple in design. In this example, we use object-oriented programming. The ins and outs of this fall outside the scope of this guide.
- The class vpsAutoSnapshot handles the automatic creation of snapshots. It is structured from top to bottom in the following parts:
- various constants where your TransIP account name, API key, the desired VPS, and the name of the snapshot are specified
- __construct creates a connection to the Rest-API. The available options you can provide can be found here.
- The 'run' function creates the snapshot. For this, several functions are called which are defined under the 'run' function:
- vpsIsLocked: checks if you have set a lock on your VPS, or if a process is running in the background that prevents a snapshot from being created now. For example, you could extend this by waiting a maximum of 2 minutes first to see if, in case of a temporary process, it can continue after 2 minutes, see waitForVpsToUnlock below.
- getSnapshotByDescription: retrieves all snapshots of your VPS. Then, any existing snapshot is deleted via deleteSnapshot, waited via waitForVpsToUnlock until the existing snapshot is deleted, and finally a new snapshot is created with CreateSnapshot.
- In the sections starting with 'private function,' the functions described above are defined.
Step 3
Open crontab with the command:
crontab -e
Step 4
Add the line below at the bottom of the crontab. Adjust the time to the desired moment when you want to create the snapshot (in this example, 1 AM every Sunday). For more information on setting up a cronjob, see this guide.
* 1 * * 0 ~/snapshot.phpSave the changes and close crontab (ctrl + x > y > enter, or :wq! depending on the editor you use).
That brings us to the end of this guide for automatically creating snapshots for VPSes.