Skip to content

Instantly share code, notes, and snippets.

@tacitochaves
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save tacitochaves/fc6c4f4c145cb10600d8 to your computer and use it in GitHub Desktop.

Select an option

Save tacitochaves/fc6c4f4c145cb10600d8 to your computer and use it in GitHub Desktop.
Backup of firewall FwBuilder 2
#!/usr/bin/env perl
#
#
# bkp_fwbuilder.pl
#
# This program is responsible for performing the backup of the firewall.
#
# Author: Tácito Chaves
# Contact: (98) 8123 - 8153 / (98) 8857 - 9777
# E-mail: chaves@tchaves.com.br
use 5.12.0;
use POSIX qw(strftime);
sub numero_do_backup {
my $arquivo_lido = $_[0];
my $posicao_letra_p = index( $arquivo_lido, "p" );
my $posicao_traco = index( $arquivo_lido, "-" );
my $numero_digitos = $posicao_letra_p - $posicao_traco;
my $numero_backup = substr( $arquivo_lido, ( $posicao_letra_p + 1 ), $numero_digitos ) + 0;
return $numero_backup;
}
sub criar_novo_arquivo_backup_fire {
# lista o diretorio
my $diretorio = $_[0];
my $utimo_numero = 0;
opendir( diretorio, "$diretorio" );
my @lista = readdir(diretorio);
closedir(diretorio);
# obtem a data atual
my $data_hoje = strftime "%Y-%m-%d", localtime;
# separa em um array todos os arquivos de backup com a data atual e captura o ultimo arquivo (da data atual)
my $numero_existentes = 0;
foreach my $arquivo (@lista)
{
my $arq_l = qq~$arquivo~;
my $tam_string = length($arq_l);
my $data_arquivo_cap = substr( $arq_l, ( $tam_string - 10 ), 10 );
if ( $data_arquivo_cap eq $data_hoje ) {
$numero_existentes++;
my $utimo_file = numero_do_backup($arq_l);
if ( $utimo_numero < $utimo_file ) { $utimo_numero = $utimo_file; }
}
}
#Define o nome do novo arquivo
my $novo_arquivo;
if ( $numero_existentes == 0 ) {
$novo_arquivo = "firewall-bkp" . "-" . $data_hoje;
}
else {
my $num_novo = $utimo_numero + 1;
$novo_arquivo = "firewall-bkp" . $num_novo . "-" . $data_hoje;
}
#Cria o novo arquivo
open( ARQUIVO, "> $diretorio/$novo_arquivo" )
|| die "Erro ao criar o arquivo\n";
close(ARQUIVO);
return $novo_arquivo;
}
my $arquivo_criado = criar_novo_arquivo_backup_fire("Diretorio_Backups");
print "Novo arquivo criado: $arquivo_criado\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment