Skip to content

Instantly share code, notes, and snippets.

@dewey
Created August 19, 2014 19:55
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 dewey/d2f364a60e4384b8d44e to your computer and use it in GitHub Desktop.
Save dewey/d2f364a60e4384b8d44e to your computer and use it in GitHub Desktop.
This is a simple script to automatically backup your IRC logs from multiple remote servers to a local directory.
#!/bin/bash
#title : backup-znc-logs.sh
#description : Create a local backup of all important ZNC accounts
#date : 19.08.2014
# ------------------------- START CONFIG -------------------------
# Script home directory
# This is the directory where you want to store the directories containing your log files.
SCRIPT_HOME="${HOME}/Documents/Textual Logs/ZNC/"
# -------------------------- END CONFIG --------------------------
# Timestamp used to determine last backup.
today=$(date +%s)
# Get a list of all directories in the SCRIPT_HOME
# "ls -l $MYDIR" = get a directory listing
# "| egrep '^d'" = pipe to egrep and select only the directories
# "awk '{print $9}'" = pipe the result from egrep to awk and print only the 8th field
DIRS=`ls -l "$SCRIPT_HOME" | egrep '^d' | awk '{print $9}'`
# Iterate through all directories in script home directory
for DIR in $DIRS
do
# Check if .config-backup file exists
# If it does, source the config file.
if [ -f "$SCRIPT_HOME/${DIR}/.config-backup" ];
then
source "$SCRIPT_HOME/${DIR}/.config-backup"
last_backup="$SCRIPT_HOME/${DIR}/$TIMESTAMP_FILE"
# Function to backup logs and notify the user, all parameters need to be enclosed in tick marks (")
# Important: Do not excape spaces in file or directory names.
backup_logs () {
servername=$1
remotepath=$2
localpath=$3
# Notification center: Backup started
/usr/local/bin/terminal-notifier -message "Backup $servername started." -title "ZNC Backup"
# Start the transfer
rsync -rhPt -e ssh "$remotepath" "$localpath"
# Notification center: Backup done
/usr/local/bin/terminal-notifier -message "Backup $servername done." -title "ZNC Backup"
}
if [ -f "$last_backup" ];
then
timestamp=$(tail -1 "$last_backup")
# Calculate if last backup is older than 24h (86400 seconds)
# Sync new logs if it's older than the specified timeframe.
if [ $(($today - $timestamp)) -gt 86400 ]
then
backup_logs "$SERVERNAME" "$PATH_REMOTE" "$PATH_LOCAL"
else
echo "$SERVERNAME : Last backup < 24h old. Do nothing."
fi
else
echo "$SERVERNAME : Timestamp for last backup not found. Creating new one."
echo "$today" > "$last_backup"
backup_logs "$SERVERNAME" "$PATH_REMOTE" "$PATH_LOCAL"
fi
else
echo "$SERVERNAME : .config-backup not found. Please create one."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment