Skip to content

Instantly share code, notes, and snippets.

@flaviolopes
Created September 29, 2010 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save flaviolopes/603556 to your computer and use it in GitHub Desktop.
Save flaviolopes/603556 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Archive::Tar;
use File::Find;
use Date::Simple (':all');
use GnuPG qw( :algo );
# Define quais diretorios entrarao no backup
my @diretorios = qw{
/home/flavio/Diversos
/home/flavio/Documentos
};
# Define a data de geracao do backup e sua formatacao
my $date = Date::Simple->new(today);
my $data_backup = $date->format("%d-%m-%Y");
# Define qual o destino do backup
my $destino = "/home/flavio/destino/";
# Define o nome do arquivo de backup que sera gerado
my $prefixo = "Backup_";
my $backup_nome = "$destino" . "$prefixo" . "$data_backup" . ".tar.bz2";
find( \&busca, @diretorios );
my @arquivos;
sub busca # Rotina que armazena em "@arquivos" todos os arquivo que serao aramazenados no backup
{
if ( -f $File::Find::name ) {
push( @arquivos, $File::Find::name );
}
}
# Gera o Backup
my $tar = Archive::Tar->new;
$tar->add_files(@arquivos);
$tar->write( "$backup_nome", COMPRESS_BZIP ); # bzip2 compressed
# Criptografa o arquivo de Backup
my $secret = "123";
my @diretorios_encriptacao = $destino;
my $gpg = new GnuPG();
find( \&criptografa, @diretorios_encriptacao );
sub criptografa { # Criptografa o arquivo
if ( $File::Find::name eq "$backup_nome" ) {
$gpg->encrypt(
plaintext => "$_",
output => "$_.gpg",
armor => 1,
sign => 0,
recipient => 0,
symmetric => 1,
passphrase => $secret
);
if ( !/\.gpg$/ )
{ # Remove os arquivos originais e deixa somente os criptografados
unlink $_ or warn "não foi possível remover arquivo \"$_\" => $!";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment