BackupPC-users

[BackupPC-users] Re-read hostname config after DumpPreUserCmd

2011-04-14 06:51:19
Subject: [BackupPC-users] Re-read hostname config after DumpPreUserCmd
From: Marcos Lorenzo de Santiago <mlorenzo AT andago DOT com>
To: backuppc-users AT lists.sourceforge DOT net
Date: Thu, 14 Apr 2011 12:31:08 +0200 (CEST)
Hi everyone,

I've been using BackupPC for several years and I find it very useful. I have a bunch of virtual servers in our platform that I wish to backup. The issue here is that sometimes they're turned on and sometimes they're off. I came out with a solution: modifying the ssh command at login time I can pass another argument with the virtual machine name so I can mount it, chroot to it's disk and make a backup if the machine is turned off.

< .ssh/authorized-keys>
from="backupservers.andago.net",command="/root/.ssh/validate-rsync" ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsR5xaq75Dlh7w6O3RozKo9/sMZJozorPRy2aHoEkkXvPAQiOZTcK9q6OgBMZ/rsOMF4pKg8+9G6pSLjCcjpgaA5p1Dd+QpxU0jzTkX/l0oxoPJYp2P9dfLkLW3XKH6GXCS4cNOba1Sz14tBT60CVuEVlAqfRyPCEQOcFi9WleiKtjdXky2bByOG/gxNTqqQxJGtkxZ+wdDY90TTjObLhpampaVqO7sgvgFP3e9wE8duBNTN4JQm/psBl16ZsSU019c/ZPc0pWP8JcnjijPfJOeeTuB1BKdxIUyr+Yr4jhmSzWuqK41Vz8knYYgfTKtvCk7BsYMfXCeHYgB6vGN03LQ== backuppc@gondor
</ .ssh/authorized-keys>

< .ssh/validate-rsync>
#!/bin/sh

if echo "$SSH_ORIGINAL_COMMAND" | grep -qE "^.*rsync.*vm=*"; then
# if rsync with custom option vm= passed then start chroot environment
        VM=$(echo $SSH_ORIGINAL_COMMAND | sed -e "s/^.*vm=\(.*\)$/\1/ig")
SSH_ORIGINAL_COMMAND=$(echo $SSH_ORIGINAL_COMMAND | sed -e"s/ vm=.*$//ig") MOUNTPOINT=$(mount | grep $VM | sed -e "s/^.* on \(.*\) type.*$/\1/ig")
        if [ -z "$MOUNTPOINT" ]; then
                echo "$VM disk not mounted, DumpPreUserCmd failed!"
                exit 1
        fi
        chroot $MOUNTPOINT $SSH_ORIGINAL_COMMAND
elif [ ! -z "$SSH_ORIGINAL_COMMAND" ]; then
        # args passed, run ssh command
        $SSH_ORIGINAL_COMMAND
else
        # no args passed, interactive login
        bash -l
fi
<.ssh/validate-rsync>

So here it comes the issue:

I have a DumpPreUserCmd that modifies the very backuppc configuration of the host being backed up. This command modifies ClientNameAlias so BackupPC connects to the server that has access to the virtual machines disks. It also mounts the virtual machine disk so when DumpPreUserCmd is done running, the environment is prepared for the BackupPC rsync.

So I needed to re-read the host config after DumpPreUserCmd is ran. This way if I change $host or $hostIP I can tweak BackupPC to connect to a server that hast access to virtual machine disk when the machine is off.

I added the following at the end of the method UserCommandRun within BackupPC_dump and it works like a charm:

<code>
    #
    # Re-read config file, so we can include the PC-specific config
    #
    print "Re-reading $client configuration\n";
    $clientURI = $bpc->uriEsc($client);
    print "[$clientURI] Change host: $host => ";
    if ($cmdType eq 'DumpPreUserCmd' || $cmdType eq 'DumpPostUserCmd') {
        if (defined(my $error = $bpc->ConfigRead($client)) ) {
            print("dump failed: Can't read PC's config file: $error\n");
            exit(1);
        }
        %Conf = $bpc->Conf();
        $host=$Conf{ClientNameAlias};
        $hostIP=$host;
        print "$host\n";
    }
</code>

I just wanted to share this with BackupPC users as some others may want to implement this. I wanted also to include this modification under BackupPC_dump as it increases BackupPC's functionality.


Best regards.


--

Marcos Lorenzo de Santiago
System administrator
marcos.lorenzo AT andago DOT com

ÁNDAGO INGENIERÍA
Tlf: +34 916 011 373          Álcalde Ángel Arroyo, 10, 1º
Mvl: +34 637 741 034          28904, Getafe, Madrid (Spain)
Fax: +34 916 011 372               www.andago.com

-----------------------------------------------------------------------
"We all know Linux is great...it does infinite loops in 5 seconds."
(Linus Torvalds about the superiority of Linux on the Amsterdam
Linux Symposium)
-----------------------------------------------------------------------
------------------------------------------------------------------------------
Benefiting from Server Virtualization: Beyond Initial Workload 
Consolidation -- Increasing the use of server virtualization is a top
priority.Virtualization can reduce costs, simplify management, and improve 
application availability and disaster protection. Learn more about boosting 
the value of server virtualization. http://p.sf.net/sfu/vmware-sfdev2dev
_______________________________________________
BackupPC-users mailing list
BackupPC-users AT lists.sourceforge DOT net
List:    https://lists.sourceforge.net/lists/listinfo/backuppc-users
Wiki:    http://backuppc.wiki.sourceforge.net
Project: http://backuppc.sourceforge.net/