smeserver-dirty-tools/root/sbin/e-smith/dt-data-sync

114 lines
2.7 KiB
Perl
Executable File

#!/usr/bin/perl
# (C) 2007 Michael Weinberger
# See http://wiki.contribs.org/Dirty_Tools for full documentation
use Getopt::Long;
my %opts;
my $getRes = GetOptions(
"dry-run"=>\$opts{'dry-run'},
"ibay"=>\$opts{'ibay'},
"compress"=>\$opts{'compress'},
"copy"=>\$opts{'copy'},
);
( my $myself = $0) =~ s/.*\///;
$opts{'copy'} = ($myself eq 'dt-data-copy' or $opts{'copy'}) ? 1 : 0;
die "Usage: dt-data-sync [--dry-run] [--compress] [--ibay] source_server account|ibay\n dt-data-copy [--dry-run] [--compress] [--ibay] source_server account|ibay\n" if( not $ARGV[1] ) ;
$server=$ARGV[0];
$source=$ARGV[1];
$dryrun= $opts{'dry-run'} ? 1 : 0;
$exit=0;
if( $opts{'ibay'} )
{
$datapath="/home/e-smith/files/ibays/$source";
$b=`/sbin/e-smith/db accounts show|grep '=ibay\$'|/bin/sed 's/=.*//'`;
chomp($b);
@ibays=split( /\n/, $b );
$b=`/usr/bin/ssh $server /sbin/e-smith/db accounts show|/bin/grep '=ibay\$'|/bin/sed 's/=.*//'`;
chomp($b);
@remote_ibays=split( /\n/, $b );
printf "Ibay %s %s\n", $source, $dryrun ? "(dry run)" : "";
@b= grep( /^$source$/, @ibays);
if( not $b[0] )
{
print "Error: Ibay does not exist local\n";
$exit=1;
}
@b= grep( /^$source$/, @remote_ibays);
if( not $b[0] )
{
print "Error: Ibay does not exist remote\n";
$exit=1;
}
if( not -d $datapath )
{
print "Error: local ibay dir does not exist\n";
$exit=1;
}
if( system( "/usr/bin/ssh $server test -d $datapath" ) )
{
print "Error: remote ibay dir does not exist\n";
$exit=1;
}
}
else
{
$datapath="/home/e-smith/files/users/$source";
$u=`/sbin/e-smith/db accounts show|/bin/grep '=user\$'|/bin/sed 's/=.*//'`;
chomp($u);
@users=split( /\n/, $u );
$u=`/usr/bin/ssh $server /sbin/e-smith/db accounts show|/bin/grep '=user\$'|/bin/sed 's/=.*//'`;
chomp($u);
@remote_users=split( /\n/, $u );
printf "Account %s %s\n", $source, $dryrun ? "(dry run)" : "";
@u= grep( /^$source$/, @users);
if( not $u[0] )
{
print "Error: Account does not exist local\n";
$exit=1;
}
@u= grep( /^$source$/, @remote_users);
if( not $u[0] )
{
print "Error: Account does not exist remote\n";
$exit=1;
}
if( not -d $datapath )
{
print "Error: local home dir does not exist\n";
$exit=1;
}
if( system( "/usr/bin/ssh $server test -d $datapath" ) )
{
print "Error: remote home dir does not exist\n";
$exit=1;
}
}
if( not $exit )
{
$rync_copy=$opts{'copy'}?"--update":"--delete-during";
$rsync_dry=$dryrun ? "--dry-run" : "";
$rsync_compress=$opts{'compress'} ? "--compress" : "";
system( "/usr/bin/rsync $rsync_dry -av $rync_copy $rsync_compress --rsh='/usr/bin/ssh -o StrictHostKeyChecking=yes' $server:$datapath/ $datapath/" );
print "Ok.\n";
}