The Hidden Blog

As it turns out, I do have a hostname.

Backup ZNC Logs automatically

Published on August 19, 2014

This is a short script to automatically backup the ZNC logs from multiple remote servers to a local directory.

If you want to run it on a system which isn’t Mac OS you’ll probably have to tweak a few things. Especially the lines containing terminal-notifier because they make use of the Mac OS notification center. Just replace them with echo statements or a similar notification framework for your operating system.

The script

Dependencies

To get the notifications to the notification center install you’ll have to install terminal-notifier. You can grab it from Github or install it via Homebrew.

Get the script

Download the script and make it executable.

git clone https://gist.github.com/d2f364a60e4384b8d44e.git backup-znc-logs
chmod +x backup-znc-logs/backup-znc-logs.sh

Open the script and edit the line which defines the SCRIPT_HOME. That’s the directory where you’ll create subdirectories for the various servers. In our case we’ll create a directory called “notmyhostna.me” and “example.com” because these are the two servers we want to back up.

cd ~/Documents/Textual\ Logs/ZNC/
mkdir notmyhostna.me
mkdir example.com

Create config files

Now it’s time to create a config file to tell the script where it should grab the files from and where to store them. We do this by creating an invisible file called .config-backup in the server directory.

vim ~/Documents/Textual\ Logs/ZNC/notmyhostna.me/.config-backup

Add these lines and costomize them to your needs. (You don’t have to copy the comments) Make sure not to excape the spaces in the path names because rsync doesn’t like this.

# The name of the server which is used in notifications.
SERVERNAME="notmyhostna.me"

# The name of the file where we store the timestamp of the last backup. Just in case you feel like changing it. You probably won't have to change this.
TIMESTAMP_FILE=".last-backup"

# The path and username of the remote server where the logs are stored
PATH_REMOTE="dewey@notmyhostna.me:/home/dewey/.znc/users/dewey/moddata/log/"

# The path the logs should be synced to on the machine the script is running from. `${HOME}` is used instead of `~`.
PATH_LOCAL="${HOME}/Documents/Textual Logs/ZNC/notmyhostna.me/"

You have to create a file like this for every server you want to backup. For this to work it’s required that you are using ssh keys and not passwords.

Run the script

Note: Per default the script will only create a backup every 24h. If you want to change that search for the value 86400 and change that to something you prefer.

If you run the script and the last backup isn’t older than 24h the output you’ll see looks like this:

Scripts|⇒ ./backup-znc-logs.sh
example.com : Last backup < 24h old. Do nothing.
notmyhostna.me : Last backup < 24h old. Do nothing.

If you want to automatically run the script you could use something like this: http://stackoverflow.com/questions/9522324/running-python-in-background-on-os-x/9523030#9523030