|
| 1 | +# Backup a server and re-create it using the backup |
| 2 | + |
| 3 | +This example demonstrates how to backup a server with `upctl` and use the created backup to re-create the server. |
| 4 | + |
| 5 | +To keep track of resources created during this example, we will use common prefix in all resource names. |
| 6 | + |
| 7 | +```env |
| 8 | +prefix=example-upctl-backup- |
| 9 | +``` |
| 10 | + |
| 11 | +We will first create a ssh-key into the current working directory for configuring an nginx server via SSH connection. |
| 12 | + |
| 13 | +```sh |
| 14 | +ssh-keygen -t ed25519 -q -f "./id_ed25519" -N "" -C "upctl example" |
| 15 | +``` |
| 16 | + |
| 17 | +We will then create a server with a single network interface and default template settings. |
| 18 | + |
| 19 | +```sh |
| 20 | +upctl server create \ |
| 21 | + --hostname ${prefix}source-server \ |
| 22 | + --zone pl-waw1 \ |
| 23 | + --ssh-keys ./id_ed25519.pub \ |
| 24 | + --network type=public \ |
| 25 | + --wait |
| 26 | +``` |
| 27 | + |
| 28 | +To have something to backup, we will install a nginx server and configure a non-default HTML content to serve. |
| 29 | + |
| 30 | +```sh filename=configure-nginx.sh |
| 31 | +#!/bin/sh -xe |
| 32 | + |
| 33 | +apt install nginx -y |
| 34 | +echo "Hello from $(hostname)"'!' > /var/www/html/index.html |
| 35 | +``` |
| 36 | + |
| 37 | +To configure the server, we will parse the public IP of the server and run the above script using SSH connection. We can then use `curl` to ensure that the HTTP server serves the content we defined. |
| 38 | + |
| 39 | +```sh |
| 40 | +# Parse public IP of the server with jq |
| 41 | +ip=$(upctl server show ${prefix}source-server -o json | jq -r '.networking.interfaces[] | select(.type == "public") | .ip_addresses[0].address') |
| 42 | + |
| 43 | +# Wait for a moment for the ssh server to become available |
| 44 | +sleep 30 |
| 45 | + |
| 46 | +# Run the script defined above |
| 47 | +ssh -i id_ed25519 -o StrictHostKeyChecking=accept-new root@$ip "sh" < configure-nginx.sh |
| 48 | + |
| 49 | +# Validate HTTP server response |
| 50 | +test "$(curl -s $ip)" = 'Hello from example-upctl-backup-source-server!' |
| 51 | +``` |
| 52 | + |
| 53 | +We will then backup the OS disk of the created server. |
| 54 | + |
| 55 | +```sh |
| 56 | +upctl storage backup create ${prefix}source-server-OS --title ${prefix}source-server-backup |
| 57 | +``` |
| 58 | + |
| 59 | +After creating the backup, we can delete the source server and its storages. |
| 60 | + |
| 61 | +```sh |
| 62 | +upctl server stop --type hard --wait ${prefix}source-server |
| 63 | +upctl server delete ${prefix}source-server --delete-storages |
| 64 | +``` |
| 65 | + |
| 66 | +We can then create a new server based on the backup of the source servers disk. |
| 67 | + |
| 68 | +```sh |
| 69 | +upctl server create \ |
| 70 | + --hostname ${prefix}restored-server \ |
| 71 | + --zone pl-waw1 \ |
| 72 | + --ssh-keys ./id_ed25519.pub \ |
| 73 | + --network type=public \ |
| 74 | + --storage action=clone,storage=${prefix}source-server-backup \ |
| 75 | + --wait |
| 76 | +``` |
| 77 | + |
| 78 | +To validate that the server was re-created successfully, we will parse the public IP of the server and use curl to see that the HTTP server is running. |
| 79 | + |
| 80 | +```sh |
| 81 | +# Parse public IP of the server with jq |
| 82 | +ip=$(upctl server show ${prefix}restored-server -o json | jq -r '.networking.interfaces[] | select(.type == "public") | .ip_addresses[0].address') |
| 83 | + |
| 84 | +# Wait until server returns expected response |
| 85 | +for i in $(seq 1 9); do |
| 86 | + test "$(curl -s $ip)" = 'Hello from example-upctl-backup-source-server!' && break || true; |
| 87 | + sleep 15; |
| 88 | +done; |
| 89 | +``` |
| 90 | + |
| 91 | +Finally, we can cleanup the created resources. |
| 92 | + |
| 93 | +```sh |
| 94 | +# Delete the restored server and its storages |
| 95 | +upctl server stop --type hard --wait ${prefix}restored-server |
| 96 | +upctl server delete ${prefix}restored-server --delete-storages |
| 97 | + |
| 98 | +# Delete the backup |
| 99 | +upctl storage delete ${prefix}source-server-backup |
| 100 | +``` |
0 commit comments