Berikut adalah skrip untuk memastikan bahwa snapshot semua droplet yang ada tanpa batasan jumlah droplet:
#!/bin/bash
# DigitalOcean API token
API_TOKEN="YOUR_API_TOKEN_HERE"
LOG_FILE="/path/to/your/logfile.log"
RETRY_LIMIT=3
# Function to log messages
log() {
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a $LOG_FILE
}
# Set the API token for doctl
export DIGITALOCEAN_ACCESS_TOKEN=$API_TOKEN
# Function to fetch droplet IDs
fetch_droplet_ids() {
log "Fetching droplet IDs..."
DROPLET_IDS=$(doctl compute droplet list --no-header --format ID)
DROPLET_COUNT=$(echo "$DROPLET_IDS" | wc -w)
log "Total droplets found: $DROPLET_COUNT"
}
# Function to delete existing snapshots with names starting with "snapshot-"
delete_existing_snapshots() {
log "Fetching existing snapshots with names starting with 'snapshot-'..."
EXISTING_SNAPSHOT_IDS=$(doctl compute snapshot list --resource droplet --no-header --format ID,Name | grep 'snapshot-' | awk '{print $1}')
for SNAPSHOT_ID in $EXISTING_SNAPSHOT_IDS; do
log "Deleting existing snapshot with ID ${SNAPSHOT_ID}..."
doctl compute snapshot delete $SNAPSHOT_ID -f
if [ $? -eq 0 ]; then
log "Snapshot ${SNAPSHOT_ID} deleted successfully."
else
log "Failed to delete snapshot ${SNAPSHOT_ID}."
fi
done
log "Checking if all specified snapshots have been deleted..."
REMAINING_SNAPSHOT_IDS=$(doctl compute snapshot list --resource droplet --no-header --format ID,Name | grep 'snapshot-' | awk '{print $1}')
if [ -z "$REMAINING_SNAPSHOT_IDS" ]; then
log "All specified snapshots have been successfully deleted."
else
log "Some snapshots were not deleted: $REMAINING_SNAPSHOT_IDS"
fi
}
# Function to create a snapshot for a given droplet ID with retries
create_snapshot() {
local DROPLET_ID=$1
local SNAPSHOT_NAME="snapshot-${DROPLET_ID}-$(date +'%Y-%m-%d')"
local ATTEMPT=1
while [ $ATTEMPT -le $RETRY_LIMIT ]; do
log "Creating snapshot for droplet ID ${DROPLET_ID} (Attempt ${ATTEMPT}) with name ${SNAPSHOT_NAME}..."
doctl compute droplet-action snapshot $DROPLET_ID --snapshot-name $SNAPSHOT_NAME
if [ $? -eq 0 ]; then
log "Snapshot for droplet ID ${DROPLET_ID} created with name ${SNAPSHOT_NAME}."
return 0
else
log "Failed to create snapshot for droplet ID ${DROPLET_ID}."
((ATTEMPT++))
sleep 10 # Wait for 10 seconds before retrying
fi
done
log "Exceeded retry limit. Could not create snapshot for droplet ID ${DROPLET_ID}."
return 1
}
# Main script execution
log "Starting backup process..."
fetch_droplet_ids
delete_existing_snapshots
log "Waiting for 1 minute to ensure snapshot deletion is fully processed..."
sleep 60 # Wait for 60 seconds (1 minute)
SUCCESS_COUNT=0
FAILURE_COUNT=0
for DROPLET_ID in $DROPLET_IDS; do
if create_snapshot $DROPLET_ID; then
((SUCCESS_COUNT++))
log "Waiting for 10 seconds before proceeding to the next droplet..."
sleep 10 # Wait for 10 seconds between snapshots
else
((FAILURE_COUNT++))
fi
done
log "Checking if snapshots have been created for all droplets..."
SNAPSHOT_COUNT=$(doctl compute snapshot list --resource droplet --no-header --format ID | wc -w)
DROPLET_COUNT=$(echo "$DROPLET_IDS" | wc -w)
if [ "$SNAPSHOT_COUNT" -eq "$DROPLET_COUNT" ]; then
log "Snapshot process completed successfully for all droplets."
else
log "Snapshot process did not complete for some droplets. Total snapshots: $SNAPSHOT_COUNT, Total droplets: $DROPLET_COUNT"
fi
log "Backup process completed."
log "Total snapshots created: $SUCCESS_COUNT"
log "Total snapshots failed: $FAILURE_COUNT"
Penjelasan Perubahan:
- Menghapus Batasan Jumlah Droplet: Menghapus logika yang memeriksa jumlah droplet terhadap nilai tetap 30.
- Menggunakan
doctl
: Skrip sekarang menggunakandoctl
untuk semua interaksi dengan API DigitalOcean. - Pengaturan Token: Token API diatur menggunakan
export DIGITALOCEAN_ACCESS_TOKEN=$API_TOKEN
. - Perintah
doctl
: Digunakan untuk mengambil ID droplet, menghapus snapshot, dan membuat snapshot.
Membuat Crontab
Untuk menjalankan skrip ini setiap hari dan menyimpan log hasilnya, ikuti langkah-langkah berikut:
- Simpan skrip di atas ke dalam file, misalnya
backup_droplets.sh
. - Berikan izin eksekusi pada file skrip:
chmod +x /path/to/backup_droplets.sh
- Edit crontab untuk user yang sesuai:
crontab -e
- Tambahkan baris berikut ke dalam file crontab untuk menjalankan skrip setiap hari pada jam 2 pagi dan menyimpan log hasilnya ke file log yang telah ditentukan:
bash 0 2 * * * /path/to/backup_droplets.sh >> /path/to/your/logfile.log 2>&1
Ganti /path/to/backup_droplets.sh
dengan path lengkap ke file skrip Anda dan /path/to/your/logfile.log
dengan path lengkap ke file log yang Anda inginkan.
Sekarang, skrip akan dijalankan setiap hari pada jam 2 pagi dan hasilnya akan dicatat di file log yang ditentukan.