Posts Tagged ‘hacking’

My Backup Solution

Thursday, July 16th, 2009

For a long time I have made backups of my home partition by hand, starting from time to time rdiff-backup. But as you can imagine, this approach doesn’t generate regular and reliable backups.

I couldn’t put this task into a simple cronjob because of two reasons. First I use encrypted hard disks and my backup disk is connected via USB and not always on. So before a backup starts I have to turn on my backup disk and make sure, that my home partition and my backup disk is decrypted and mounted. Second I don’t want the backup happen during my regular work. In my experience such processes often starts in the most annoying moments.

So I decided that I need an semi-automatic backup, which runs during shutdown. The result is this small script which I put in /etc/rc0.d/K05backup.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
 
currentTime=`date +%s`
timeUntilNextBackup=604800                 # 604800sec = 1week
startBackup=false
 
# check if it's time for the next backup
if [ -f /var/log/nextBackup.log ]; then
    nextBackupTime=`cat /var/log/nextBackup.log`
    if [ $(($currentTime - $nextBackupTime)) -gt 0 ]; then
        startBackup=true                       #time for the next backup
    fi
else
    startBackup=true
fi
 
if [ $startBackup == true ]; then
    echo "It's time for another Backup!"
    echo "Don't forget to switch on your backup hard disk before you start!"
    repeat=true
    while $repeat; do
        echo -n "Start backup procedure now? (y)es or (n)o? "
        read char
        case $char in
            [y,Y] ) 
                if [ ! -d /home/schiesbn ]; then
                    echo "encrypted HOME partition has to be mounted..."
                    cryptsetup luksOpen /dev/sda6 secureHome
                    mount /dev/mapper/secureHome /home
                fi
                echo "encrypted BACKUP partition has to be mounted..."
                cryptsetup luksOpen /dev/sdd1 secureBackup
                mount /dev/mapper/secureBackup /mnt/backup
                echo "Starting Backup...";
                rdiff-backup --print-statistics /home/schiesbn /mnt/backup
                echo "umount backup disk..."
                umount /mnt/backup
                cryptsetup luksClose secureBackup
                # calculate the time for the next backup and write it to the log
                nextBackup=$(($currentTime + $timeUntilNextBackup))
                echo $nextBackup > /var/log/nextBackup.log
                echo "DONE."
                sleep 10   #give me some time to look at the backup statistics
                repeat=false;;
            [n,N] )
                repeat=false;;
        esac
    done
fi

If the last backup is older than 1 week the script asks me, if I want to do another backup. Than I can decide to postpone it or to start it now. If I decide to start the backup procedure I get the opportunity to decrypt my backup and home partition before rdiff-backup starts. After that I can leave the room and be sure that the computer will shutdown after the backup is finished.

Until now this is the best and most reliable, least annoying and most automated solution I could found.

Jabber Mail Notification

Friday, February 20th, 2009

I always struggled to find the right mail notification applet for my desktop. Furthermore I always stumble over the question: Why do I have to ask the mail server in a defined time interval “Do I have a new e-mail?”. Wouldn’t it be better if the mail server notifies me if a new e-mail arrives?
This is probably somehow a new form of the good old question “mailing list vs bulletin board” or in general: Do i have to fetch the information or does the information come to me? Personally i always preferred to get the information and not to hunt around for them.

Thinking about this question i realized that notification through Jabber would be perfect and the open XMPP protocol virtually invites one to do such things.

The idea was born. Now the first step was to find a easy to use XMPP implementation for a scripting language like Python, Ruby or PHP. At the end I found a quite nice and easy to use PHP library. While searching such a library I also found this guidance (German only), borrowed some code from it and my solution was born:

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
29
30
31
32
33
34
35
36
37
38
<?php
// The script gets the input either as an argument, from a REQUEST-variable or from stdin
// If you use it within procmail you will get the input through stdin 
if ($argv[1]) {
    $msg = $argv[1];
} elseif ($_REQUEST['msg']) {
    $msg = urldecode($_REQUEST['msg']);
} else {
    // open stdin. Only read the first 4096 character, this should be enough to match
    // the FROM- and  SUBJECT-header
    $stdin = fopen('php://stdin', 'r');
    $msg   = fread($stdin, 4096);
 
    if (empty($msg)) {
        $msg = "empty";
    } else {
        // Get FROM und SUBJECT
        preg_match('@From:(.*)@i', $msg, $from);
	preg_match('@Subject:(.*)@i', $msg, $subject);
        $msg = "\n" . $from[0] . "\n" . $subject[0] . "\n";
    }
}
 
// now init xmpp and get the notification out
include 'XMPPHP/XMPP.php';
 
$conn = new XMPPHP_XMPP('schiessle.org', 5222, 'user', 'password', 'xmpphp', 'schiessle.org', $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);
 
try {
    $conn->connect();
    $conn->processUntil('session_start');
    $conn->presence();
    $conn->message('me@jabber.server.org', $msg);
    $conn->disconnect();
} catch(XMPPHP_Exception $e) {
    die($e->getMessage());
}
?>

Now I just had to tell procmail to pipe the mails through the PHP script. If you want to get notified about all mails you can simply put this line at the top of your procmail rules (Or maybe at least behind the spam filter rules ;-) ):

1
2
:0c
|php /home/schiessle/bin/mailnotification.php

I want to get notified only about some specific mails so I extended my procmail rules in this way:

1
2
3
4
5
6
7
8
9
:0
* ^(To:|Cc:).*foo@bar-mailinglist.org
{
 	:0c
        |php /home/schiessle/bin/mailnotification.php
 
        :0
        .bar-list/
}

That’s it! All in all it was quite easy to get e-mail notification through Jabber. Now I don’t have to search for the right applet, configure it etc.. All I have to do is to start my Jabber client and I will get notified about new mails whatever desktop or computer i’m using.