Hi everyone.

I've just wrote a small script to save the data of a backuppc 3.0 server
to a remote *NIX host (offsite backup, uses mainly rsync over ssh, but
also need a small shell, at least for the command cat on the remote
host).
This script is written in perl and depends on the module getopt::std

I'm testing it, so please, do not use on production servers for now

this is how it works:

- first, it stops backuppc
- it copy /etc/BackupPC/* to the data directory (to save also the configuration)
- then, it copy the data directory, exept the big ones (pool, cpool and pc)
- it copy cpool to the remote host
- idem for pool
- it uses BackupPC_tarPCCopy to copy the pc directory on the remote host
- it restart backuppc

The main goal of this script is to reduce the needed RAM to copy a big
pool (an rsync on the data directory can work, but it takes a lot of
RAM, my own example is about 700Mo of ram for a 40Go pool)

How to use it:
you can have help by invoking the script with the -h option
-s specify the source, I mean, the data directory (without the ending /, or 
you'll have an error)
-d specify the destination, rsync formatted: [EMAIL PROTECTED]:/path
-e tells the script to extract the pc archive on the remote host, can be 
usefull if you want the remote host to be ready for running backuppc, if -e is 
not specified, the pc directory on the remote host will just contains an 
archive (as tar or tar.gz)
-z tells the script to compress the pc archive before sending it on the remote 
host (if -e is also specified, the compression will only affect the transfert, 
if -e is not specified, the pc archive will be pc/pc.tar.gz instead of 
pc/pc.tar on the remote host)

here are some example of using it (in these examples, the data directory
of backuppc is /opt/backuppc/files, and I want to copy it
in /home/backup of the host backup.mydomain.com as the user test.). This
script needs ssh key authentication, but if you use backuppc, you
should know about it so I won't explain here:

BackupPC_SME_remoteBackup -s /opt/backuppc/files -d [EMAIL 
PROTECTED]:/home/backup -z

BackupPC_SME_remoteBackup -s /opt/backuppc/files -d [EMAIL 
PROTECTED]:/home/backup -z -e

BackupPC_SME_remoteBackup -s /opt/backuppc/files -d [EMAIL 
PROTECTED]:/home/backup -e

BackupPC_SME_remoteBackup -s /opt/backuppc/files -d [EMAIL 
PROTECTED]:/home/backup


The script should run as user backuppc (or the user backuppc uses). If
so, you'll need to give the user backuppc the permission to start and stop the 
daemon with sudo:

backuppc ALL=(root) NOPASSWD:/etc/rc.d/init.d/backuppc *

Some last details

- you can edit the path of backuppc bin direcoty at the top of the script (my 
$backuppcBinPath = '/usr/local/BackupPC/bin';)

- you can edit the log directory at the end, by default its 
/var/log/BackupPC/export/ (system("$catPath $logFile >> 
/var/log/BackupPC/export/export.$remoteHost.log");)




###############################################################

#!/usr/bin/perl -w

# Ce script permet de copier les sauvegardes de backuppc vers un hôte distant


use Getopt::Std;

sub init;
sub remotePool;
sub perform;
sub genRandName;
sub vetrifTree;

# les paths de qq prog

my $backuppcBinPath = '/usr/local/BackupPC/bin';
my $rsyncPath = '/usr/bin/rsync';
my $tarPath = '/bin/tar';
my $gzipPath = '/bin/gzip';
my $sshPath = '/usr/bin/ssh';
my $mountPath = '/bin/mount';
my $umountPath = '/bin/umount';
my $mkdirPath = '/bin/mkdir';
my $rmPath = '/bin/rm';
my $catPath = '/bin/cat';
my $pgrepPath = '/usr/bin/pgrep';
my $sudoPath = '/usr/bin/sudo';
my $datePath = '/bin/date';
my $lsPath = '/bin/ls';

my %opts;
my $logFile = '/tmp/'.genRandName ();
my $date = `$datePath`;

init();

open (STDOUT, ">$logFile");

print "$date";

remotePool($opts{s},$opts{d},$opts{e},$opts{z},$logFile);



## toutes les sous-fonctions

# On parse les arguments
sub init (){
        getopts ("zeh:s:d:", \%opts) || help();
        foreach ('z','e','h'){
                $opts{$_} = '0' if (! defined $opts{$_});
        }
        help () if $opts{h};
}


sub help (){
        print STDERR <<EOF;
        usage: $0 -s source -d destination [-z] [-h] [-e]
        Required options:
     -s sourceDir               local directory where backuppc stores the data
     SourceDir format is /opt/backuppc/files (without the ending /)
     
     
     -d destinationDir          local or remote directory where the data will 
be copied
     DestinationDir format is [EMAIL PROTECTED]:/home/backup (remote path 
through ssh)
     
     

        Other options:
     -z         use gzip to compress the pc directory archive
     -e         extract the pc archive on the receiver side
     -h         print this help
     
     
     To restore the data, you can reinstall a fresh server, then (example)
     
     rsync -avP --exclude=pc/ [EMAIL PROTECTED]:/home/backup/ 
/opt/backuppc/files/
     mkdir -p /opt/backuppc/files/pc
     ssh [EMAIL PROTECTED] "cat /home/backup/pc/pc.tar.gz" | tar xzfP - -C 
/opt/backuppc/files/pc/
     
EOF
    exit(1);
}

# fonction qui génère un nom aléatoire pour les fichiers temporaires
sub genRandName(){
        my @c=("A".."Z","a".."z",0..9);
        my $randomName = join("",@c[map{rand @c}(1..8)]);
        return $randomName;
}

# Fonction qui vérifie la présences des rep
sub verifTree($$){
        my ($source,$dest) = @_;
        my $ok = 1;
        
        foreach ("$source","$source/pc","$source/pool","$source/cpool","",""){
                if (!-d "$_"){
                        print STDERR "$_ is not a valid directory, aborting\n";
                        $ok = 0;
                }
        }
        return $ok;
}

# La fonction qui stock toutes les commandes les unes à la suites des autres 
dans un tableau
sub remotePool ($$$$$){
        my ($source,$dest,$extract,$compress,$logFile) = @_;
        
        my $archName = 'pc.tar';
        my @cmd = ();
        my @pipe = ();
        my $tarOpts = 'xPf';
        my $main = '';
        
        
        #if ( $dest !~ /^([\w-]:[EMAIL PROTECTED]/.])$/ ){
        #       print STDERR "destination should follow this format: [EMAIL 
PROTECTED]:/path\n";
        #       exit (1);
        #}
        
        my @tmp = split(/@/,$dest);
        my $remoteUser = shift(@tmp);
        @tmp = split(/:/,$tmp[0]);
        my $remoteHost = shift(@tmp);
        my $remoteDir = $tmp[0];
        
        push(@cmd,"$sudoPath /etc/rc.d/init.d/backuppc stop");
                 
        # Si /etc/BackupPC exist, on le sauvegarde dans $source/etc
        if(-d '/etc/BackupPC'){
                push(@cmd,"$mkdirPath -p $source/etc") if (!-d "$source/etc");
                push(@cmd,"$rsyncPath -a --del --stats /etc/BackupPC/ 
$source/etc/");
        }
                
        
        # commande qui sync en non récursif pour créer les rep nécessaires
        push(@cmd,"$rsyncPath -qlptgoDHd --stats --del $source/ $dest/");
        
        # la commande suivante sync tout sauf les rep pc, pool et cpool (pour 
éviter de saturer la ram)
        push(@cmd,"$rsyncPath -a --del --stats --exclude=cpool/ --exclude=pool/ 
 --exclude=pc/ $source/ $dest/");
        
        # Maintenant, on sync le rep pool
        push(@cmd,"$rsyncPath -a --del --stats $source/pool/ $dest/pool/");
        
        # Puis le cpool
        push(@cmd,"$rsyncPath -a --del --stats $source/cpool/ $dest/cpool/");
        
        
        # on vide le répertoire "pc"
        if ($remoteDir ne ''){
                push(@cmd,"$sshPath [EMAIL PROTECTED] \"$rmPath -Rf 
$remoteDir/pc/*\"");
        }

        
        push(@pipe,"$backuppcBinPath/BackupPC_tarPCCopy $source/pc/ |");
        
        if ($compress eq '1'){
                push (@pipe," gzip -c |");
                $archName = "pc.tar.gz";
                $tarOpts = 'xPzf';
        }
        if ($extract eq '1'){
                push (@pipe,"$sshPath [EMAIL PROTECTED] \"(cd $remoteDir/pc/ && 
$tarPath $tarOpts -)\"");
        }
        else{
                push (@pipe,"$sshPath [EMAIL PROTECTED] \"cat > 
$remoteDir/pc/$archName\"");
        }
        
        foreach (@pipe){
                $main = $main.$_;
        }
        
        push (@cmd,$main);
        
        push(@cmd,"$sudoPath /etc/rc.d/init.d/backuppc start");
        #print STDERR "logfile is $logFile\n";
        perform ($logFile,$remoteHost,@cmd);
        
}

# la fonction qui exécute les commandes les unes à la suite des autres
sub perform($$){
        my ($logFile,$remoteHost,@cmd) = @_;
        foreach (@cmd){
                print "\n\nexecuting command\n$_\n";
                #print STDERR "$_\n";
                system("$_");
        }
        print "\n\n----- End of copy -----\n\n\n";
        system("$catPath $logFile >> 
/var/log/BackupPC/export/export.$remoteHost.log");
        system("$rmPath -f $logFile");
}



##################################################################################



-- 
Daniel Berteaud
FIREWALL-SERVICES SARL.
Société de Services en Logiciels Libres
Technopôle Montesquieu
33650 MARTILLAC
Tel : 05 56 64 15 32
Fax : 05 56 64 82 05
Mail: [EMAIL PROTECTED]

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
BackupPC-users mailing list
BackupPC-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/backuppc-users
http://backuppc.sourceforge.net/

Reply via email to