jasonwryan.com

Miscellaneous ephemera…

rsync + SSH Backups

image

I posted recently about setting up a microserver and included a rsync script for backing up my files to the server. At the time, I was running it manually, but since have sorted out the excludes list and have now automated it.

My original scheme was to set up a cron job and mount an NFS share before rsync’ing the directories across. In the end, I went with SSH and public key authentication as debugging all of the various permissions issues with the NFS share did not seem worth the effort.

First, on my local desktop, I created the keypair called, inventively, rsync:

1
2
3
ssh-keygen -t rsa -f rsync
mv rsync ~/.ssh && chmod 600 ~/.ssh/rsync
ssh-copy-id -i rsync.pub jason@microserver

This keypair was created without a passphrase so that the script could be successfully called from cron. To slightly increase the security around a key without a passphrase, I used the from option in authorized_keys1, to restrict access to the IP address of the client machine(s), like so:

1
from="192.168.1.10*" ssh-rsa AAAAB3Nza...

It was then just a matter of adding a line to my crontab:

1
2
crontab -e
00 22 * * * /home/jason/Scripts/runbackup

To further simplify matters, I created an alias in $HOME/.ssh/config for the SSH command:

.ssh/config
1
2
3
4
5
Host backups
   Hostname 192.168.1.200
   Port 0000
   User jason
   IdentityFile ~/.ssh/rsync

And I was done. Everynight at 10pm, all of my data will be backed up to the server. The rsync command in the script:

runbackup.sh
1
2
3
4
5
6
#!/bin/sh
# Backup to remote server over SSH

rsync -azP --delete --exclude-from=/home/jason/Scripts/excludes.txt \
    --log-file=/home/jason/Documents/rsync-$(date +%d%m%y).log \
    -e ssh /home/jason /etc backups:Backups/Centurion
Notes
  1. All of the options are detailed in man sshd(8).

Creative Commons image from skreuzer.

Microserver

image

I recently picked up a HP Microserver; a small, silent server to use primarily to back up my other boxes. I went for the 2GB version without any preinstalled operating system.

As I want this machine to just sit quietly in my garage for the next 5 or 6 years, I opted for Debian stable as the base system. After installing two 1TB drives, I set them up as an encrypted RAID 1 array, in a logical volume.

Pro tip when you get to the part of the partitioning where you configure the volume for encryption, make sure you select No for erase data. It took close to 90 hours to scrub both the hard drives. And, as I am not really expecting a visit from the spooks, that seems a little like overkill…

Once the base install was complete, I set up my SSH keys, tmux, configured the firewall and forwarded the necessary ports, set up NFS, and that was it. The box is deathly quite, can be mounted locally for rsync backups and accessed remotely (for a tmux session running irssi), and all for less than NZD700.

I knocked together a quick script1 to manage the backing up; currently I run it manually, but once I have the excludes file tweaked properly, I’ll set up a cron job to automate the task.

runbackup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/sh
# Backup to remote server over NFS

if [ $(id -u) -ne 0 ]; then
     printf '%s\n' "You must run this as root. Terminating."
     exit 1
fi
 
if [ -d /media/Sentinel/Backups ]; then 
     printf '%s\n' "Sentinel mounted."
 else
     printf '%s\n' "Mounting Sentinel."
     mount.nfs 192.168.1.200:/home/jason /media/Sentinel || exit 1
 fi

# sync directories
 printf '%s\n' "Starting sync..."

 rsync -azP --delete --exclude-from=Scripts/excludes.txt \
     --log-file=Documents/rsync.log \
     /home/jason /etc /media/Sentinel/Backups/Centurion

 if [ "$?" -eq 0 ]; then
     printf '%s\n' "Synched successfully; now unmounting."
     umount /media/Sentinel
 else
     printf '%s\n' "Fail!"
 fi
Notes
  1. Any faults aside, it should be reasonably portable…

IRC Notifications with dzen2

image

I have a persistent tmux session running on a server connected to IRC and I wanted to get notifications whenever someone addressed me, irrespective of whether I was connected to the session, or that particular machine at the time.

I use irssi as my IRC client, and there is a script called fnotify that will print any mentions in a channel to a file. I then configured the fnotify script to write to a folder in Dropbox. Now, whenever I am mentioned in freenode, the file is updated across all my machines.

The next challenge was to translate the updating of the fnotify file into a notification on my current machine. As I don’t use libnotify on my Arch machines, I wanted to make it work with dzen21 Enter a simple shell script:

highlights.sh
1
2
3
4
5
6
#!/bin/sh
dir="$HOME/Dropbox/irssi/"

while inotifywait -qqre attrib "$dir" >/dev/null 2>&1; do
    echo "IRC: You have been pinged..." | dzen2 -p 3
done

I have included this in my $HOME/.xinitrc so that it runs in the background after login. Whenever the metadata (the attrib switch) of the file changes, a dzen2 notification is written to my dwm statusbar with the message.

Simple; but does what I need…

Notes
  1. Dropbox’s API won’t work with dzen2—or with other lightweight notifications like statnot, so I filed a bug report (that was closed without any progress)—hence my having to resort to a home-cooked solution…