Automating snapshot creation and cleanup is an essential task for server management, ensuring data safety and efficient resource usage. The following Bash script accomplishes this by creating snapshots of a server hosted on Hetzner Cloud and deleting snapshots older than two days. This guide explains the script step by step.
Prerequisites
Before running the script, ensure you have:
- A Hetzner Cloud account with API access.
- jq installed on your Debian 12 server (
sudo apt install jq
). - curl installed on your Debian 12 server (
sudo apt install curl
).
The Script
Save the following script as hetzner_backup.sh
:
#!/bin/bash # Replace with your actual server ID and API token SERVER_ID="YOURSERVERID" API_TOKEN="YOUR API TOKEN" # Log file location LOG_FILE="/var/log/hetzner_backup.log" # Function to create a snapshot create_snapshot() { DATE=$(date '+%B-%d-%Y') DESCRIPTION="$DATE" echo "$(date '+%Y-%m-%d %H:%M:%S') - Creating snapshot for server ID $SERVER_ID with description: $DESCRIPTION" >> $LOG_FILE RESPONSE=$(curl -s -X POST \ -H "Authorization: Bearer $API_TOKEN" \ -H "Content-Type: application/json" \ -d "{\"description\":\"$DESCRIPTION\",\"type\":\"snapshot\"}" \ "https://api.hetzner.cloud/v1/servers/$SERVER_ID/actions/create_image") if echo "$RESPONSE" | grep -q '"error":'; then echo "$(date '+%Y-%m-%d %H:%M:%S') - Error creating snapshot: $RESPONSE" >> $LOG_FILE else echo "$(date '+%Y-%m-%d %H:%M:%S') - Snapshot created successfully" >> $LOG_FILE fi } # Function to delete snapshots older than 2 days delete_old_snapshots() { echo "$(date '+%Y-%m-%d %H:%M:%S') - Deleting snapshots older than 2 days for server ID $SERVER_ID" >> $LOG_FILE IMAGES=$(curl -s -H "Authorization: Bearer $API_TOKEN" \ "https://api.hetzner.cloud/v1/images" | jq -r '.images[] | select(.created_from.id=='"$SERVER_ID"') | select(.created < "'$(date -d '2 days ago' --utc +%Y-%m-%dT%H:%M:%SZ)'") | .id') for IMAGE_ID in $IMAGES; do RESPONSE=$(curl -s -X DELETE \ -H "Authorization: Bearer $API_TOKEN" \ "https://api.hetzner.cloud/v1/images/$IMAGE_ID") if echo "$RESPONSE" | grep -q '"error":'; then echo "$(date '+%Y-%m-%d %H:%M:%S') - Error deleting snapshot ID $IMAGE_ID: $RESPONSE" >> $LOG_FILE else echo "$(date '+%Y-%m-%d %H:%M:%S') - Snapshot ID $IMAGE_ID deleted successfully" >> $LOG_FILE fi done } # Create a snapshot create_snapshot # Delete old snapshots delete_old_snapshots
How the Script Works
- Configuration: Replace
YOURSERVERID
andYOUR API TOKEN
with your Hetzner Cloud server ID and API token respectively. These credentials allow the script to interact with the Hetzner Cloud API. - Logging: The script logs its actions to
/var/log/hetzner_backup.log
. Ensure this file is writable by the user running the script. - Snapshot Creation:
- The
create_snapshot
function creates a snapshot with the current date as the description. - It uses the
curl
command to send a POST request to the Hetzner API, creating a snapshot of the specified server. - Responses are checked for errors, and the outcome is logged.
- Old Snapshot Deletion:
- The
delete_old_snapshots
function retrieves a list of snapshots older than two days usingcurl
andjq
for JSON processing. - For each outdated snapshot, a DELETE request is sent to the Hetzner API to remove it.
- The script logs the success or failure of each deletion attempt.
Running the Script
- Make the Script Executable: Set execute permissions for the script:
chmod +x hetzner_backup.sh
- Schedule the Script: Use
cron
to schedule the script for periodic execution. For example, to run the script daily at midnight:
crontab -e
Add the following line:
0 0 * * * /path/to/hetzner_backup.sh
Conclusion
This script automates the process of creating and cleaning up snapshots on a Hetzner Cloud server. By scheduling it with cron
, you can ensure regular backups and manage disk space efficiently by removing old snapshots. This approach enhances your server management and data safety practices, If you need to see what’s your server ID, click here.