#!/usr/bin/perl

use strict;
use warnings;
use DB_File;
use POSIX qw(strftime);
use Net::SMTP;
use Net::Domain qw(hostname hostfqdn hostdomain);

require '/usr/libexec/webmin/ovhm/libovhm2.pl';


if (-e '/usr/libexec/webmin/ovhm/.backupoldfiles.lock'){
	print "Error : a backup or a restore is in progress\n";
	exit 1;
}
my $logFile = "/usr/libexec/webmin/ovhm/backupoldfilesbis.log";
`date > /usr/libexec/webmin/ovhm/.backupoldfilesbis.lock`;

if (-e "$logFile"){
	unlink $logFile;
}




my @files;
my $annee="homesansalkclinf";

#push @files, "/home/alkclinf/www/files/documents/dossiers/2020/2020-07-54847";

push @files, "/home/alkclinfproftpd.conf";
push @files, "/home/alkirisk";
push @files, "/home/alkrouge";
push @files, "/home/alkspa";
push @files, "/home/aquota.user";
push @files, "/home/cabinetl";
push @files, "/home/chtvigny";
push @files, "/home/clinform16.sql";
push @files, "/home/clsurvey";
push @files, "/home/clsurveycn";
push @files, "/home/controle4";
push @files, "/home/galk";
push @files, "/home/gcerf";
push @files, "/home/gites";
push @files, "/home/hotellic";
push @files, "/home/html";
push @files, "/home/icons";
push @files, "/home/iter";
push @files, "/home/ldbeau";
push @files, "/home/levesque";
push @files, "/home/lldbeau";
push @files, "/home/log";
push @files, "/home/longcham";
push @files, "/home/lost+found";
push @files, "/home/lougegc";
push @files, "/home/mail";
push @files, "/home/mysql";
push @files, "/home/output.txt";
push @files, "/home/ovh";
push @files, "/home/restaura";
push @files, "/home/risquest";
push @files, "/home/seminair";
push @files, "/home/terreum";
push @files, "/home/test";
push @files, "/home/usage";


my $ftp="vpncl.cl-informatique.com";	
my $ftp_user="web01ftp";
my $ftp_password="Kq0B3ZsB!YbR";	
	#WE HAVE ALL THE FILES ==> CREATING TARBALL ON THE FLY :-)

	
       my $cmd= "tar cvzf -";
	foreach my $file (@files){
		$cmd .=" $file";
	}
	&log("backup vhosts for $cmd");
	$cmd .= " 2>/dev/null| curl -u $ftp_user:$ftp_password ftp://$ftp/usbshare1-2/home/$annee.tar.gz -T - 2>/dev/null";
	`$cmd`;	
	if ( ($? >> 8) != 0){
		&error('Unable to backup files $cmd $?');
	}
		
	


unlink "/usr/libexec/webmin/ovhm/.backupoldfiles.lock";

sub error{
	my $msg = shift;
	if (defined ($msg)){
		open FIC, ">>$logFile";
		print FIC $msg."\n";
		close FIC;
		unlink "/usr/libexec/webmin/ovhm/.backupoldfiles.lock";
		exit 1;
	}
}

sub log{
	my $msg=shift;
	if (defined($msg)){
		open FIC, ">>$logFile";
		print FIC "$msg\n";
		close FIC;
	}
}

#
## send backup mail
#
my @MAIL_REPORT = '';

open (LOG_FILE,"<$logFile");
while (<LOG_FILE>){

	if ( $_ =~ m/^backup vhosts(.*)/ ){
		push (@MAIL_REPORT, ('-----     DOMAIN     -----')."\n");
	}
	elsif ( $_ =~ m/^backup database mysql(.*)/ ){
		push (@MAIL_REPORT, ('-----     DATABASE     -----')."\n");
	}
        push (@MAIL_REPORT, $_);
}
close (LOG_FILE);

my $SENDER_DOMAIN=hostfqdn;
my $MAIL_SENDER = "postmaster@"."$SENDER_DOMAIN";
my $MAIL_RECEIVER='root@localhost';
my $VIA_SMTP='localhost';
my $SUBJECT_BACKUP = "Backupoldfiles from $SENDER_DOMAIN";

my $smtp = Net::SMTP->new($VIA_SMTP, Hello => $SENDER_DOMAIN);

$smtp->mail($MAIL_SENDER);
$smtp->to ($MAIL_RECEIVER);
$smtp->data();
$smtp->datasend("To: $MAIL_RECEIVER\n");
$smtp->datasend("Subject: $SUBJECT_BACKUP\n");
foreach (@MAIL_REPORT)
{
	($smtp->datasend ("$_"));
}
$smtp->datasend("\n");
$smtp->dataend();
$smtp->quit;

