BOP Blog: Offsite Backup Script

BytesOfProgress

Offsite Backup Script

26th January 2025 | 08:00 PM

Today I have written a bash script which takes backups of the BytesOfProgress website. It takes a backup every night at midnight (german time). Now I can deploy the script on any Linux VM (in my case i use Debian VMs) at an offsite location. That way, in case something happens to the physical webserver or git server which hold the website files, I always have a copy of it at that offsite location.

The script only keeps the 5 most recent backups, so it does not waste too much disk space at the offsite location. This is the script:

#!/bin/bash

URL="https://bytesofprogress.net"  # Website URL
KEYWORD="Blog"    # Keyword
REPO_URL="gitserverURL" # Git-Repo-URL
BASE_DIR="/home/bop-backup/BytesOfProgress"  # Backups get saved to
MAX_DIRS=5  # Keeping the x most recent backups

# Date in YYYY-MM-DD_HH-MM-SS format
TIMESTAMP=$(date '+%Y-%m-%d_%H-%M-%S')

# Checking for Keyword
check_keyword() {
  curl -s "$URL" | grep -q "$KEYWORD"
  return $?
}

# Removing oldest backups
cleanup_oldest_dir() {
  # Find backups (oldest first)
  dirs=($(ls -dt "$BASE_DIR"/* 2>/dev/null | sort -V))

  if (( ${#dirs[@]} > MAX_DIRS )); then
      oldest_dir="${dirs[0]}"
      echo "$TIMESTAMP: Deleting old backup: $oldest_dir"
      rm -rf "$oldest_dir"
  fi
}

# Main process
main() {
  echo "$TIMESTAMP: Checking for keyword..."

  if check_keyword; then
      echo "$TIMESTAMP: Keyword found, cloning repository."

      # Verzeichnisname mit Datum und Zeit erstellen
      new_dir="$BASE_DIR/BOP-BU-$TIMESTAMP"
      echo "$TIMESTAMP: Creating backup directory: $new_dir"
      git clone "$REPO_URL" "$new_dir"

      # Alte Verzeichnisse bereinigen
      cleanup_oldest_dir
  else
      echo "$TIMESTAMP: Keyword not found. Aborting."
  fi
}

# Running main
main

The script checks if a keyword is present on the website. If the keyword is found, it clones the git repository into a new backup folder. This is how to set it up:


1. Create a sudo user named:
     bop-backup

  2. Install following packages:
     git
     curl

  3. Place "BOP-Backup-Script.sh" in "/home/bop-backup/".

  4. Create following cronjob using "crontab -e":
     0 0 * * * /bin/bash /home/bop-backup/BOP-Backup-Script.sh

  5. Do a test run:
     bash /home/bop-backup/BOP-Backup-Script.sh

  6. The directory "/home/bop-backup/BytesOfProgress" should now exist with
     the first backup in it.





back