Linux, programming, computers and life

June 30, 2007

Do not say “It won’t happen to me” and always make backups

Filed under: programming, linux

I’m the millionth person to say this - you have to, you need, you must backup. Backup is not only protection against hardware failures, it’s also protection against user error. Backup is very easy to setup nowdays. It’s possible to have some online storage and backup there, backup from one physical hard drive to another, to flash memory stick or even to email some files to gmail.
The last reason i had to restore was the following command: gpg -d passwords.txt.gpg > passwords.txt.gpg - redirection first truncates the destination file - so i was left without my passwords collection :) .
My backup policy is to backup my home folder - all flies smaller than 20MB and the /etc directory to another physical hard disk twice a week using sbackup. Recently i added a small script which sends email with several attached (small) files to my gmail account. It’s being run daily by cron daemon.
Here’s the script i use for emails:

#! /bin/bash

# tar gz files:
DATE=`date +%F_%T`
OUT_FILE=”/tmp/${DATE}.tar.bz2″
BACKUP_FILES=”/home/some_path/*.txt”
BACKUP_EMAIL=”my_gmail_email@gmail.com”

tar cvfj “$OUT_FILE” $BACKUP_FILES
trap “{ rm -f “$OUT_FILE” ; exit 255; }” EXIT

FILE_SIZE=`\ls -lh “$OUT_FILE” | cut -d ‘ ‘ -f 5`
SUBJECT=”backup of `echo $BACKUP_FILES | wc -w` files ($FILE_SIZE) at $DATE”
echo “`echo ${BACKUP_FILES} | tr ‘ ‘ ‘\n’`” | mutt -a “$OUT_FILE” “$BACKUP_EMAIL” -s “$SUBJECT”

rm “$OUT_FILE”


It uses mutt for sending email with attachments.

Technorati Tags: , , ,

June 16, 2007

some bash scripting, named pipes and traps

Filed under: CLI, programming, linux

Recently, i needed to convert a lot of mp4 files to mp3 format. Well, time for some scripting i thought to myself, time to improve my bash skills :) . And, actually, i learned several things from this simple task.
First, and pretty interesting one is named pipes.  In my script i used mplayer to convert mp4 to wav format. And then, lame in order to convert the wav to mp3.  So, generally speaking, there’s a need in temporary file which takes disk space and time to write and to delete. A lot of linux applications have the possibility to write their output to standard output, allowing to use shell pipes to pass it to other applications (which know to get their input from standard input). I did not found such an option in mplayer, but, in the back of my head i remembered it’s possible to create named pipe on linux. So, instead of creating the temporary file, i create named pipe (using mkfifo command) and use it as a channel from mplayer to lame. This allows me to parallelize them and hopefully reduces disk writes - which should give substantial speed improvement.
Second, named pipe (or any other temporary file) needs to be deleted in the end of the script… even if the script was stopped using Ctrl-c or kill/pkill commands. There’s nice thing named trap in bash which allows to execute certain code when signals arrive - which is exactly what happens on Ctrl-c or other ways of stopping an application in the middle of it’s work.
I think it’s enough lessons for one small script. Here it comes:

#! /bin/bash

if [[ “$#” != “2″ && “$#” != “1″ ]]
then
echo “Usage: $0 file .mp4 [file .mp3]”
exit 1
fi

IN_FILE=$1

if [[ “$#” == “1″ ]]
then
OUT_FILE=`echo $IN_FILE | sed -s ’s/mp4$/mp3/’`
else
OUT_FILE=”$2″
fi

# if output file already exists - exit
if [ -f “$OUT_FILE” ]
then
echo “\”$OUT_FILE\” already exists!”
exit 1
fi

# create named pipe:
PIPE_NAME=”`mktemp -u`.$$.tmp_named_pipe”

# make sure it’s deleted anyway:
trap “{ rm -f “$PIPE_NAME” ; exit 255; }” EXIT

mkfifo “$PIPE_NAME”

# mp4 -> pipe(wav):
mplayer -vc null -vo null -ao pcm:fast -ao pcm:file=”$PIPE_NAME” “$IN_FILE” &> /dev/null &

# pipe(wav) -> mp3:
lame -b 32 “$PIPE_NAME” “$OUT_FILE”

rm -f “$PIPE_NAME”
exit 0

Technorati Tags: , , , , , ,

June 10, 2007

installing rpm for the poor or what to do if you are not root

Filed under: linux

Sometimes, you want to install some application on a linux computer where you have no access as root user. Of course, the best way would be to download the source code files and compile them all using the usual configure --prefix=...;make;make install commands. However, it’s not as easy as it seems. Usually any non trivial application you want to install needs a lot of libraries, and in order to compile it, you’ll need the development files of the libraries installed. And if those are not present (that what usually happens), you’ll need to do the same build cycle to all the involved libraries (which may require others…).
Generally speaking, there’s no other way to do it, unless somebody did the compilations for you. But, if you are lucky and the application you are planning to install is well written, you might get away with less work.
The solution is simple:

  1. find (here or here for example) a binary rpm file for your distribution. Let’s call the file 123.rpm
  2. unpack the rpm using rpm2cpio (i have it available google for it if you don’t). The command would be: rpm2cpio 123.rpm > 123.rpm.cpio
  3. in some temporary directory unpack the cpio file you have: cpio --extract --make-directories < 123.rpm.cpio Note you’ll have some directories and files created
  4. Hope for the best :)
Ok, now you have the rpm unpacked and if you’re lucky you’ll be able to run the application as is. Just make sure the newly created *bin* directory is in your path and try running it.
If the app will not run, you’ll have to troubleshoot it. If the application cannot find a library, make sure the libraries which got unpacked from rpm are accessible. In order to do so, you’ll have to add path of those libs to LD_LIBRARY_PATH environment variable. Just add export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/path/to/new/libs to your ~/.bashrc file.
Now if that’s not enough, the application is probably looking for some file which is supposed to be somewhere in /usr/… (but you can’t write there, remember?). If so - you’ll need to investigate what file is it (usually you’ll get it’s name in the error message) and where it’s possible to put it instead. The post is getting too long, so the only thing i’ll say here - use strace and common sense :) .

In order to install gaim 2.0.0beta5 (recently renamed to pidgin) i needed to add a softlink to plugins directory in the ~/.gaim/ dir:

> ll ~/.gaim/ | grep plugins
lrwxrwxrwx   1 ——- —–    31 Jan  7 11:47 plugins -> /home/—-/public/usr/lib/gaim/

Hopefully, it’ll inspire somebody

Technorati Tags: , , ,

June 7, 2007

jabber translator bots

Filed under: internet

jabber is really cool IM protocol. Here come several bots for you, jabber user, which know to translate among several languages.
In order to use the bot, just add <direction>@translate.jrudevels.org to your roster, where <direction> is one of the following:

  • er English-Russian
  • re Russian-English
  • gr German-Russian
  • rg Russian-German
  • fr French-Russian
  • rf Russian-French
  • sr Spanish-Russian
  • rs Russian-Spanish
  • ir Italian-Russian
  • eg English-German
  • ge German-English
  • es English-Spanish
  • se Spanish-English
  • ef English-French
  • fe French-English
  • ep English-Portuguese
BTW, it will work with google talk and LiveJournal IM :)

Based on russian post http://osoznanie.blogspot.com/2007/06/jabber.html

Technorati Tags: , , ,

June 5, 2007

if you are planning to purge free accounts - do not expect any appreciation or loyalty

Filed under: internet

I once used zoto (i do not give the link intentionally because i do not want to recommend this service) as a platform for photo sharing. It worked decently (not perfectly, but decently) and gave a lot of space for a free account.
One sunny day, they decided they cannot handle free accounts any more. They also said, that the photos can be downloaded and a way to do it will be published. Guess what, it never was. I even sent an email asking how to do it, and no answer arrived. Furthermore, my photos are not on the site any more.
The bottom line is - why will i ever pay them? Why? My bottom line is: do not use zoto.
Update: a comment was left by zoto employee which says they provide a way to download the photos. I’ll contact them and see what will happen.

Technorati Tags: , , ,

Get free blog up and running in minutes with Blogsome
Theme designed by Gary Rogers