-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsamazon.pl
144 lines (94 loc) · 4.53 KB
/
wsamazon.pl
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/perl
#
# Script para realizacao de backups com destino ao Amazon S3
use Switch;
use List::Util qw(shuffle);
# Recebendo o tipo de funcao a ser executado - FULL | DIFF
my $BackupType = @ARGV['0'];
# Todos os servidores que devem ser sincronizados com a amazon devem constar no array abaixo
my @servidores = ("CHAT","CORDILHEIRA","DASHBOARDS","DNS01","DNS21","FINANCEIRO","FIREWALL","GW056","GW059","GW111","GW114","GW212","GW214","GW215","GW216","GWE-APP-MAIN","HSBC","OCSINVENTORY","OPENSIPS01","PRODUCAO","PROJETOS","SAO-HUGO","SFTP","SIPPROXY01","SISTEMAS","SUITECRM","SUTECRM","VHOST01-DMZ","VOICEPR","VOICERPR","ZABBIX","ZIMBRA");
# Declarando variaveis referente aos caminhos dos diretorios
my $BackupDiffDir = '/backup/RSYNC_DIFFS';
my $BackupFullDir = '/backup/RSYNC';
my $logDir = '/var/log/wsamazon';
my $programLogFile = 'wsamazon.log';
my $hostname = 'backup2';
if(!-d $logDir){
die("Diretorio de log [ $logDir ] nao encontrado");
}
switch ($BackupType) {
case 'FULL' { execFull(@servidores); }
case 'DIFF' { execDiff(@servidores); }
else { print "Deve ser utilizado FULL ou DIFF como argumento\n"; }
}
# Funcoes
# Funcao para pegar datas anteriores para uso pelo modulo de DIFF
sub getYeasterday{
my $numDays = ($_[0] * 24);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime( ( time() - ( $numDays * 60 * 60 ) ) );
$mon = $mon + 1;
if($mon < 10){ $mon = "0" . $mon; }
if($mday < 10){ $mday = "0" . $mday; }
return ( ($year +1900), $mon, $mday );
}
# Funcao para execucao de Backup FULL
sub execFull {
# Iniciando loop sobre variavel de servidores ( servidores randomizados )
@shuffleServers = shuffle(@_);
foreach $server (@shuffleServers) {
# Gerando informacao de data para utilizacao em log
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
if($mon < 10){ $mon = "0" . $mon; }
if($mday < 10){ $mday = "0" . $mday; }
my $ModuleLogFile = $logDir . '/' . $server . "-" . ($year + 1900) . $mon . $mday;
print "Servidor: $server \n";
print "Log: $ModuleLogFile\n";
print "Iniciando em: $hour:$min\n";
# Redirecionando sainda por handle
open (my $STDOLD, '>&', STDOUT);
open (my $STDOLDERR, '>&', STDERR);
open(STDOUT, ">>$ModuleLogFile");
open(STDERR, ">&STDOUT");
# Iniciando sincronia
print "Comando Executado: s3cmd -v sync /backup/RSYNC/$server/ s3://backup-softmarketing/$hostname/RSYNC/$server/\n";
system("s3cmd -v sync /backup/RSYNC/$server/ s3://backup-softmarketing/$hostname/RSYNC/$server/");
# Fechando redirecioando do handle
open(STDOUT, '>&', $STDOLD);
open(STDERR, '>&', $STDOLDERR);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
print "Finalizdo em: $hour:$min\n";
}
}
sub execDiff {
# Iniciando loop sobre variavel de servidores ( servidores randomizados )
@shuffleServers = shuffle(@_);
foreach $server (@shuffleServers) {
# Pegando a data de DIFF de 3 dias atras para copiar
my ($o_year, $o_month, $o_day) = getYeasterday(3);
if( -e "$BackupDiffDir/$o_year/$o_month/$o_day/$server" ){
# Gerando informacao de data para utilizacao em log
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
if($mon < 10){ $mon = "0" . $mon; }
if($mday < 10){ $mday = "0" . $mday; }
my $ModuleLogFile = $logDir . '/' . $server . "-DIFF-" . ($year + 1900) . $mon . $mday;
print "Servidor: $server \n";
print "Log: $ModuleLogFile\n";
print "Iniciando em: $hour:$min\n";
# iniciando a sincronia
print "Comando Executado: s3cmd -v sync $BackupDiffDir/$o_year/$o_month/$o_day/$server/ s3://backup-softmarketing/$hostname/RSYNC_DIFFS/$o_year/$o_month/$o_day/$server/\n";
# Redirecionando sainda por handle
open (my $STDOLD, '>&', STDOUT);
open (my $STDOLDERR, '>&', STDERR);
open(STDOUT, ">>$ModuleLogFile");
open(STDERR, ">&STDOUT");
system("s3cmd -v sync $BackupDiffDir/$o_year/$o_month/$o_day/$server/ s3://backup-softmarketing/$hostname/RSYNC_DIFFS/$o_year/$o_month/$o_day/$server/");
# Fechando redirecioando do handle
open(STDOUT, '>&', $STDOLD);
open(STDERR, '>&', $STDOLDERR);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
print "Finalizdo em: $hour:$min\n";
}else{
print "Nao existe pasta do servidor em $BackupDiffDir/$o_year/$o_month/$o_day/$server\n";
}
}
}