anderstornvig.dk

Where’s your head at?

Archive for January 2008

Automatic USB backup in Linux using udev

without comments

Lately I’ve been experimenting with reliability and security on various levels in Linux. Reliability involves backing up important files and I’ve managed to backup my desktop computer to my server using rsync. But since I really can’t count on my server that much as the hard drives are getting old I needed a second backup. For this I got an idea with my USB stick. Here’s the the plan:

Plug in the stick, do nothing while the computer backs everything important up to the stick and unplug it when it’s done.

Test distro is Slackware 12.0 but it will work on any other Linux distribution as Ubuntu.

I found out that the udev deamon was responsible for detecting devices such as USB storage. I had to read a bit in order to make this guide. Something about the /dev directory, the manual for udev “# man udev”, writing udev rules, a bit shell scripting and the manual for cp “# man cp”.

Preparing USB stick

Create a mount point for your USB stick

# mkdir /home/atc/mount

Plug it in and run the following to find out what node it has been given

# fdisk -l

Locate the stick and remember the path. Mine is /dev/sda1.

Format it as described here from 4 through 8. The mkfs-command may take a while so I recommend that you do it on another computer or in a seperate shell while continuing with the rest.

The backup script

Create a script called usbbackup in /usr/bin.

# nano /usr/bin/usbbackup

Copy paste this and correct it so i fits your needs. Don’t worry about the if-condition. We’ll take care of that later.

#!/bin/bash
mount -v -t ext3 /dev/sda1 /home/atc/mount > /var/log/usbbackup.log
if [ -f /home/atc/mount/usbbackup/backupstick ]
then
	# make it backup anything you want here
	cp -r -v /path/to/source/directory /path/to/dest/directory/in/mountpoint --archive --update >> /var/log/usbbackup.log
	cp -v /etc/*.conf /home/atc/mount/usbbackup/. >> /var/log/usbbackup.log
	mysqldump --all-databases -v -h localhost -ppassword -u username > /home/atc/mount/usbbackup/atcmysqldb.sql
fi
umount -v /home/atc/mount >> /var/log/usbbackup.log

The “-v”’s make the commands run in verbose mode. It basically just give you more debugging from the processes. Save, close and make it executable with the right permissions.

# chmod +x /usr/bin/usbbackup
# chmod 755 /usr/bin/usbbackup

Create an empty log file

# touch /var/log/usbbackup.log

This log will in the future give you information about your latest backup.

Getting dirty with udev

Time to make an udev rule. The rule works as a trigger to your script run when you plug in the stick. Remember device node your usb stick was given when you plugged it in? Too bad if you don’t cause we’re gonna use it now.

Create the udev rule opening a new file in this specific directory.

# nano /etc/udev/rules.d/91-usbbackup.rules

Make it contain this line

ACTION=="add", KERNEL=="device_name", RUN+="/usr/bin/usbbackup"

ACTION “add” makes the script run once when you connect the stick. Device_name was in my case still sda1. RUN is obviously the command that actually runs the script. Udev is in fact capable of recognizing your exact stick matching model, vendor and so on. If you want this google is as always your friend. For me it’s getting really late and since I don’t need it let’s just leave it.

Save and close.

The final touch

Finally, assuming the formatting of your stick is done, mount it.

# mount /dev/sda1 /home/atc/mount

Create a backup folder containing a file that identifies your backup stick and thus takes care of the if-condition.

# mkdir /home/atc/mount/usbbackup
# touch /home/atc/mount/usbbackup/backupstick

Unmount the stick

# umount /home/atc/mount/usbbackup

Unplug it.

That should be it. You’re now ready to plug in your USB stick, lean back while the computer is taking care of it all and unplug it again. The first backup will take while as it’s copying all of your files. To make sure that everything works I recommend that you survey your first backup with

# tail -f /var/log/usbbackup.log

The “-f” makes the file update automatically.

My stick has a blue light that blinks when it’s being used. That way I know when it’s done backing up. Freaking nice..!

Please feel free to post a comment about your experience with this guide. If you improved it I’d also like to know.

EDIT: My m8 Andri took this idea a step further in Ubuntu. Check it out on http://andri.dk/en/tech/linux/usb-backup.

EDIT: Some guy on the Arch Linux forum tried something similar. I think you’ll find it useful.

Written by Anders Tornvig

January 19, 2008 at 21:20

Posted in Linux

Tagged with , , , ,

Mount and create an ISO file from a CD

without comments

Today I wanted to image a DVD. I found the following command that also works with a CD.

$ dd if=device_path of=outputfile.iso

Device_path is /dev/sr0 in my case. For more information about the dd command:

$ man dd

I also had to mount an image. This can in most cases be done by

$ mount -o loop -t iso9660 imagefile.iso /path/to/mountpoint/

Written by Anders Tornvig

January 13, 2008 at 23:51

Posted in Linux

Tagged with , , ,

Encoding and transfering video for iPod in Linux

without comments

What can take hours on Windows often takes minutes on Linux. Ok maybe that’s not always true but in this case it certainly is. Before I really got into Linux I literally spent hours trying to figure out how to get a bloody video on my iPod video (30gb). This should judging from the name be the easiest task to do – it isn’t though. Don’t expect Apples expensive and crappy hardware called iPod VIDEO to actually play your videos. iTunes doesn’t do the job unless you live in the US and A and have access to the american iTunes Store. The only option you have is to google something like “video for ipod”. At some point you’ll get so tired of downloading either shareware or freeware software that according to the description should do the job but just doesn’t. And of course you don’t want to spend another fortune on software for freaking video converting. When this however seems to be the only solution you probably just give up thinking “why did they implement video playback without giving me any software to put my own videos on the iPod? Apple is even trying to sell me cables to connect it to my TV”. It doesn’t seem fair and it isn’t.

Luckily Linux was around and once again came in handy for this task. I’ll in the following describe what I did on my Ubuntu computer to easily encode and transfer my video files to my iPod. I used http://ubuntuforums.org/showthread.php?t=114946 and http://www.hachemuda.com/ficheros/ipodvidenc.

First install the following:

$ sudo apt-get install liblame-dev libxvidcore4-dev libx264-dev libfaac-dev libfaad2-dev liba52-dev libdc1394-dev libgsm1-dev libtheora-dev libvorbis-dev
$ sudo apt-get build-dep ffmpeg

This is the libraries and program that actually converts you file. Next, get the ipodvidenc script. Do this by typing:

$ nano ipodvidenc

and copy paste the following. I’ve made a few changes to it, so here goes:

#!/bin/bash
## ipodvidenc - The iPod Video Encoder for Linux.
## Created by Eric Hewitt, January 9, 2006.
## Released under the GPL.  Go nuts.
input_file=\$1
echo "What would you like to name the output file (without extension)? "
read output_file_name
echo "\$output_file_name.mov will be located in \$PWD. Is this acceptable? [y/n]"
read output_file_loc_permis
if [ \$output_file_loc_permis = 'n' ] || [ \$output_file_loc_permis = 'N' ]
then
	echo "Where would you like to store \$output_file_name.mov?"
	read output_dir
else
	output_dir=\$PWD
fi
ffmpeg -i "\${input_file}" -f mp4 -vcodec mpeg4 -maxrate 1000 -b 700 -qmin 3 -qmax 5 -bufsize 4096 -g 300 -acodec aac -ab 192 -s 320x240 -aspect 4:3 "\${output_dir}/\${output_file_name}.mov"

The last line is the crucial one here. Save the file and do

$ chmod 755 ipodvidenc
$ sudo mv ipodvidenc /usr/bin

That’s it. You can now encode your file with this command:

$ ipodvidenc video.avi

You’re probably also interested in getting the file on your iPod. Just install gtkpod with

$ sudo apt-get install gtkpod-aac

and it will guide you trough the process.

Well, that was all I needed to do. And then I got to know a bit of shell scripting too which was nice. Some day I’m gonna learn it. If you have problems with this procedure you should look it up on google or in the links I just gave you.

Written by Anders Tornvig

January 4, 2008 at 15:46

Posted in Linux

Tagged with , , , , ,