#!/usr/bin/perl
#
use DBI;
use warnings;
use strict;
use POSIX qw(strftime);

my $base_easy="easy";
my $base_external="external";
my $host_in="in.fr.clara.net";
my $host_sarthe="sarthe.fr.clara.net";
my $user= "db_backup";
my $passwd="xxxxx";
my $eid_code_catA="";
my $eid_code_catB="";
my @eid_code_catA=();
my @eid_code_catB=();
my $no_client="";
my $path="/data/web/";
my $RESULT_NO_CLIENT_CATA=""; # liste des no_clients categorie B
my $RESULT_FINAL_CATA=""; # liste final (no client, domain, domain_id, mysql user, mysql password, mysql server) des clients categorie A
my $RESULT_NO_CLIENT_CATB=""; # liste des no_clients categorie B
my $RESULT_FINAL_CATB=""; # liste final (no client, domain, domain_id, mysql user, mysql password, mysql server) des clients categorie A
my $date = strftime "%y-%m-%d", localtime;
my @temp;



###################################################################################################################################################################################
# PARTIE 1 pour les backups des packs CATEGORIE A (Biz expert / corporate, EBiz expert / corporate (backup hebdomadaire rétention 1 mois + backup journalier rétention 1 semaine) #
###################################################################################################################################################################################


### A.  connexion a la base external de IN ###
my $dsnA = "DBI:mysql:database=$base_external;host=$host_in";
my $dbhA = DBI->connect($dsnA,$user,$passwd) or die "Echec connexion";

# Recuperation de l eid + code pour les packs Biz expert / corporate, EBiz expert / corporate (backup hebdomadaire rétention 1 mois + backup journalier rétention 1 semaine)  = CATA
my $requeteA = "select tracker.eid,tracker.code from easy left join compta on easy.eid=compta.eid left join tracker on tracker.eid=compta.eid where easy.persoinfo='Y' and easy.loginpass='Y' and easy.lettre='Y' and easy.pieces='Y' and easy.zonecheck='Y' and easy.paid='Y' and easy.installation='Y' and easy.status='on' and  compta.pid=23 or compta.pid=24 or compta.pid=34 or compta.pid=35";
my $sthA = $dbhA->prepare($requeteA);
$sthA->execute();

# RESULT_NO_CLIENT_CATA :  numero client = concatenation de  eid + code pour les packs Biz expert / corporate, EBiz expert / corporate (backup hebdomadaire rétention 1 mois + backup journalier rétention 1 semaine)

open (RESULT_NO_CLIENT_CATA,">/home/benj/RESULT_NO_CLIENT_CATA");
#
while (@eid_code_catA = $sthA -> fetchrow_array) {
	print RESULT_NO_CLIENT_CATA $eid_code_catA[0];
	print RESULT_NO_CLIENT_CATA "$eid_code_catA[1]\n";
 }

  close (RESULT_NO_CLIENT_CATA);


$sthA -> finish;
$dbhA -> disconnect;


# B.  connexion a la base easy de sarthe


my $dsnB = "DBI:mysql:database=$base_easy;host=$host_sarthe";
my $dbhB = DBI->connect($dsnB,$user,$passwd) or die "Echec connexion";

# Sur Sarthe, base easy, table compta, on recupere le compta_id ŕ partir du no_client (eid+code). Avec le compta ID, sur sarthe, base easy, table domain on récupere le domain_id. Avec le domain_id , sur sarthe, base easy, table mysql on recupere le nom de la basei, le mot de passe & le serveur mysql.

open (RESULT_NO_CLIENT_CATA, '/home/benj/RESULT_NO_CLIENT_CATA');
open (RESULT_FINAL_CATA,">/home/benj/RESULT_FINAL_CATA");

while ($no_client= <RESULT_NO_CLIENT_CATA>) {
	my $requeteB = "select compta.no_client,domain.domain_id,domain.domain,mysql.user,mysql.passwd,mysql.server from domain,compta,mysql where compta.compta_id=domain.compta_id and domain.domain_id=mysql.domain_id and compta.no_client=$no_client";
	my $sthB = $dbhB->prepare($requeteB);
	$sthB->execute();

	if ($sthB -> rows ne 0) {
		while (@temp = $sthB -> fetchrow_array) {
			print RESULT_FINAL_CATA "$temp[0] "; # n° client
		      	print RESULT_FINAL_CATA "$temp[1] "; # domain id
			print RESULT_FINAL_CATA "$temp[2] "; # nom du domaine
			print RESULT_FINAL_CATA "$temp[3] "; # user / base mysql
			print RESULT_FINAL_CATA "$temp[4] "; # password mysql
			print RESULT_FINAL_CATA "$temp[5]\n"; # server mysqm
		}					                
	}
}	

close (RESULT_FINAL_CATA);



# C. On va maintenant faire le dump de(s) bases mysql de chaque domaine et les placer ŕ la racine de /public

# faire le hash / cd rep client / mysqldump & chown

#open (RESULT_FINAL_CATA, '/home/benj/RESULT_FINAL_CATA');
while 





#system("mysqldump -h  .$repSauvegarde." ".$nomBase." --opt  --result-file ");
print $date;
