-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.php
80 lines (64 loc) · 2.06 KB
/
backup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
date_default_timezone_set("America/Bogota");
class Tiempo {
private $tiempoInicial;
private $tiempoFinal;
function __construct() {
$this->tiempoInicial = time();
}
function setTiempoFinal() {
$this->tiempoFinal = time();
}
function getTiempoSeg() {
return $this->tiempoFinal - $this->tiempoInicial;
}
function getTiempoMin() {
return ($this->getTiempoSeg()/60);
}
}
class Backup {
private $rutaDirectorio;
private $rutaPgDump;
function __construct($rutaDirectorio, $rutaPgDump) {
$this->rutaDirectorio = $rutaDirectorio;
$this->rutaPgDump = $rutaPgDump;
if (!$this->crearDirectorio($rutaDirectorio)) {
throw new Exception("No se puede crear el directorio: {$rutaDirectorio}");
}
}
function crearDirectorio($directorio) {
if (!is_dir($directorio)) {
if (!mkdir($directorio)) {
return false;
}
}
return true;
}
function dump($institucion) {
$rutaArchivo = $this->rutaDirectorio . '/' . $institucion . '_' . date('dmYHis') . '.backup';
$comando = '"' . $this->rutaPgDump . '" --host 192.168.1.1 --port 5432 --username "edalmava" --no-password --format custom --blobs --file "' . $rutaArchivo . '" "' . $institucion . '"';
system($comando, $retVal);
return $retVal;
}
function dumpInstituciones($instituciones) {
foreach($instituciones as $institucion) {
echo "$institucion: ";
echo (!$this->dump($institucion))?"[OK]\n":"[ERROR]\n";
}
}
}
$instituciones = array('colegio1', 'colegio2', 'colegio3');
echo "************************************************************\n";
echo "* Programa para Backup de base de datos PostgreSQL *\n";
echo "************************************************************\n";
echo "\n\n";
try {
$backup = new Backup('D:/BD/' . date('dmY'), 'C:/Program Files/PostgreSQL/9.4/bin\pg_dump.exe');
$tiempo = new Tiempo();
$backup->dumpInstituciones($instituciones);
$tiempo->setTiempoFinal();
echo "\n\n";
echo "Tiempo empleado: ({$tiempo->getTiempoSeg()}seg) ({$tiempo->getTiempoMin()}min)";
} catch(Exception $e) {
echo "ERROR: " . $e->getMessage() . "\n";
}