mandag den 24. juni 2013

Nightly Backups over smb

This is just a quickie.

I have a usb drive attached to my boxee box which I share over boxee's inbuilt smb server. This is mounted on my server machine with an /etc/fstab line like:

//192.168.0.100/maxell_hdd /home/csr/boxee cifs user,rw,username=guest,file_mode=0777,dir_mode=0777,guest,auto,uid=1000,gid=1000 0 0

So if I want to backup files from my server machine to the boxee on a nightly basis, how do I do it? rsync is a great tool, but I ran into one problem - every so often the boxee would be offline for some reason or other and the rsync would just copy my files into the mount directory, filling my disk up in no time. So I added a test to see if the boxee was actually mounted. The backup script ended up looking like

#!/bin/bash
#
#
volume="/home/csr/boxee"
if mount | grep "on ${volume} type" > /dev/null
then
  rsync -av --modify-window=1 --ignore-existing  /home/csr/media/Music/ /home/csr/boxee/Music
else
echo 'Nightly sync failed becaue of lack of mount'
fi

I put this in a file /bin/backup.sh and then added it to my nightly cron job.

Update

I've now replaced the boxee box with a NUC Linux box to which I have direct ssh access. This means I can replace the above rsync with something like this:

rsync -av --modify-window=1 --ignore-existing  /home/csr/media/Music/ -e ssh colin@192.168.0.155:Maxell/Music

onsdag den 19. juni 2013

Getting from A to C via B (with ssh)

So this is a very typical, but stupid, real-life problem. I need to get some files from machine A to machine C. I have ssh access to machine A only from machine B. From my local workstation I can access machine B and machine C, but not A (except via B). And to make matters more fun, there isn't enough diskspace on machine B to just move the files there first temporarily. Also I only have root access to my local machine so I can't assume that nice tools like sshfs are installed on the any of the other machines.

Here's my solution:
  • From localhost, create an ssh tunnel B->A:
  • ssh -f B_user@machineB -L 9999:machineA:22 -N
  • Create a mount point for the remote directory
  • mkdir my_mount
  • Mount the remote directory (this prompts for the password on machine A)
  • sshfs -p 9999 A_user@127.0.0.1:/data my_mount
  • ... and just copy the files to machine C,
  • scp my_mount/afile C_user@machineC:/home/C_user/data
     
The crucial part is the third step which mounts the remote directory from machine A on my local machine.

Hello

This blog is just a place where I can stick little bits of technical nerdery that I come across in solving the daily problems of life as a software-geek and household-head-of-IT.