Skip to content

Instantly share code, notes, and snippets.

@gilangvperdana
Last active March 20, 2023 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilangvperdana/2ab234420c473c6eb3aefc48715578ba to your computer and use it in GitHub Desktop.
Save gilangvperdana/2ab234420c473c6eb3aefc48715578ba to your computer and use it in GitHub Desktop.
Auto Backup File / Directory to S3 with S3CMD

General

  • I have a case that is to backup Grafana where grafana.ini is located in /etc/grafana/grafana.ini in the form of a single file and /var/lib/grafana/ is in the form of a directory to be backed up to S3 (here I use minio).
  • In this script the file will be kept for 3 days and will be replaced every 12-hours every day with crontab 0 */12 * * *.
  • And of course when the backup is successful it will send an alert to the telegram bot that is if the file number is 2 correctly (you can adjust it in the -eq 2 section).

Script

#!/bin/bash

# set variable
gobackupdate=$(date +"%Y%m%d %H:%M")
today=$(date +"%d_%m_%Y")
backup_path="s3://grafana-backup"
keep_backup_days=3
telegram_bot_token="YOUR_TELEGRAM_BOT_TOKEN"
telegram_chat_id="YOUR_TELEGRAM_CHAT_ID"

echo "========== BACKUP STARTING $gobackupdate =========="

#upload /var/lib/grafana
echo "upload /var/lib/grafana to S3 Minio"
s3cmd sync /var/lib/grafana $backup_path/$today/

#upload /etc/grafana/grafana.ini
echo "upload /etc/grafana/grafana.ini to S3 Minio"
s3cmd sync /etc/grafana/grafana.ini $backup_path/$today/

# check if backup is uploaded successfully
backup_count=$(s3cmd ls $backup_path/$today/ | wc -l)
if [ "$backup_count" -eq 2 ]; then
  backup_date=$(date +"%d %B %Y %H:%M")
  echo "backup berhasil"
  # send report to telegram bot
  message="Backup Grafana ke S3 Minio pada $backup_date Berhasil"
  curl -s -X POST https://api.telegram.org/bot$telegram_bot_token/sendMessage -d chat_id=$telegram_chat_id -d text="$message"
else
  echo "backup gagal"
  # send report to telegram bot
  message="Backup Grafana ke S3 Minio pada $backup_date Gagal"
  curl -s -X POST https://api.telegram.org/bot$telegram_bot_token/sendMessage -d chat_id=$telegram_chat_id -d text="$message"
fi

# delete backup older than $keep_backup_days
s3cmd ls $backup_path/ | while read -r line;  do
    create_date=$(echo $line|awk {'print $1" "$2'})
    backup_date=$(date -d "$create_date" +%s)
    current_date=$(date +%s)
    diff=$(( (current_date - backup_date) / (24*60*60) ))
    if [ $diff -gt $keep_backup_days ]; then
        echo "deleting $line"
        s3cmd del "$line"
    fi
done

echo "========== BACKUP FINISHED =========="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment