Skip to content

Instantly share code, notes, and snippets.

@Lixivial

Lixivial/backup.pl

Created Apr 5, 2010
Embed
What would you like to do?
a quick perl script to do some backup handling of common data types
#!/usr/bin/perl
# set -x
# Jesse Pearson
# Quick perl script to tar up given set of targets, and push them to a given destination
#
# Changelog:
# 04.05.2010 - jp - added cp and svn support.
# 04.05.2010 - jp - added local and remote backup holding directories.
# 04.01.2010 - jp - Initial creation date.
#
# Pre-requisites:
# mount
# tar
# (mysqldump optional)
# (svnadmin optional)
# (ssh optional)
#
# Vars:
# backupMethod - the method in which the backup holding space is used (remote or local; remote uses samba)
# rmtBackupPath - if method is remote, the backup //server/directory
# rmtBackupUser - if method is remote, the smb username
# rmtBackupPass - if method is remote, the smb password
# basePath - the path in which all actions are performed relatively
# logPath - the path to which log files are written.
# log - a boolean dictating whether or not logging should be enabled
# if disabled, the output will instead be written to the console.
#
# targets - a hash map containing the name of the backup file to be generated, along with
# an array of targets. These targets are prefaced with the following types:
# tar - a directory of files to be archived into a single file
# example: "tar:/tmp/:alias"
# mysql - a database to be dumped. This takes the following extra parameters:
# username - the db owner's username
# password - the db owner's password
# dbname - the db to export
# example: "mysql:username:password:database:alias"
# target - wrap around another defined target (for, say, aliasing a series of targets under a cohesive name)
# example: "target:confluence:alias"
# cp - simply copy the files. This is useful if they're already tarballed.
# svn - perform an svn hotcopy backup to the specified target.
#
# The target also needs an alias associated with it, which proceeds the path.
#
# The order of these variables is critical for now.
#
# rmtDirectory - The remote server and directory for use in the sftp or mount.
# rmtUsername - The remote username.
# rmtPassword - The remote password.
# rmtMethod - The methodology for placing these on the server:
# mount - mounts the server as a directory for copying.
# scp - pushes the file across using ssh.
# rmtfs - The local fs used with the mount command. (smbfs, cifs are the only two tested filesystems).
use strict;
use File::Copy;
use File::Path;
# static vars to emulate boolean behaviour.
use constant false => 0;
use constant true => 1;
# configuration globals
my $backupMethod = "local";
my $rmtBackupPath = "//hercules/tmp";
my $rmtBackupUser = "uname";
my $rmtBackupPass = "pwd";
my $basePath = "/tmp/backups/";
my $logPath = $basePath . "/logs/";
my %targets = ("confluence", ["tar:/var/data/confluence:confluence_data",
"tar:/usr/local/confluence-2.5.1/:confluence_install",
"mysql:uname:pwd:confdb:confluence_db"],
"jira", ["tar:/var/data/jira:jira_data",
"tar:/usr/local/apache-tomcat-5.5.23/webapps/jira:jira_install",
"mysql:uname:pwd:jiradb:jira_db"],
"fisheye", ["tar:/var/data/fisheye/:fisheye_data",
"tar:/usr/local/fisheye-2.1.2/:fisheye_install"],
"actitime", ["tar:/usr/local/apache-tomcat-5.5.23/webapps/actitime/:actitime_install",
"mysql:uname:pwd:actitime:actitime_db"],
"mysql", ["mysql:uname:pwd:mysql:mysql_db"],
"tomcat", ["target:confluence:confluence_backup",
"target:jira:jira_backup",
"target:fisheye:fisheye_backup"],
"users", ["tar:/etc/shadow:shadow_definitions",
"tar:/etc/passwd:passwd_definitions",
"tar:/etc/group:group_definitions",
"tar:/etc/sudoers:sudoers_definitions"]);
my $log = true;
my $rmtDirectory = "//stpeterdc1/backups";
my $rmtUsername = "uname";
my $rmtPassword = "pwd";
my $rmtMethod = "mount";
my $rmtfs = "cifs";
# application globals
my @validTypes = ("mysql", "tar", "cp", "target", "svn");
my ($seconds,$minutes,$hour,$dayOfMonth,$month,$year,$weekday,$dayOfYear,$isDST) = localtime(time);
$year += 1900;
$month += 1;
my $logDate = $month . "." . $dayOfMonth . "." . $year . " " . $hour . ":" . $minutes;
my $logFile = $logPath . $month . $dayOfMonth . $year . "_" . $hour . "_" . $minutes . "_" . $seconds . ".txt";
my $hostname = `echo -n \`hostname\``;
my $mntPath = $basePath . "mnt/";
main();
# Main program execution method.
sub main {
my ($type, $target, $alias, $username, $password);
setupBackupDirectory();
foreach my $key ( keys %targets ) {
my $outputPath = $basePath . $key . "/";
my $targetFile = $basePath . $key . ".tar";
mkdir "$outputPath", 0777 unless -d "$outputPath";
for my $value ( @{$targets{$key}}) {
($type, $target, $alias, $username, $password) = parseAttributes($value);
if ($type eq "mysql") {
my $outputFile = $alias . ".sql";
my $result = `mysqldump -u $username -p$password $target > $outputPath$outputFile `;
logging("DEBUG", "MySQL backup: $result");
} elsif ($type eq "tar") {
my $outputFile = $outputPath . $alias . ".tbz2";
my $result = `tar -cjf $outputFile $target`;
logging("DEBUG", "tar backup: $result");
} elsif ($type eq "cp") {
my $outputFile = $outputPath . $alias;
my $result = `cp -Rdf $target $outputFile`;
logging("DEBUG", "cp backup: $result");
} elsif ($type eq "svn") {
my $outputFile = $outputPath . $alias . ".svn";
mkdir "$outputFile", 0777 unless -d "$outputFile";
my $result = `svnadmin hotcopy $target $outputFile`;
logging("DEBUG", "svn backup: $result");
}
}
my $targetResult = `tar -cf $targetFile $outputPath`;
logging("DEBUG", "target backup: $targetResult");
rmtree($outputPath);
}
pushFiles();
exit(0);
}
sub parseAttributes {
my $targetAttributes = shift;
my ($type, $target, $alias, $username, $password, $i);
my @attributes = split(/:/, $targetAttributes);
$i = 0;
for my $attribute ( @attributes ) {
my $result = grep $_ eq $attribute, @validTypes;
if ($result == true && $i == 0) {
$type = $attribute;
} elsif ($type eq "mysql" && $i <= 2) {
if ($i == 1) {
$username = $attribute;
} elsif ($i == 2) {
$password = $attribute;
}
} elsif ($i == 2 || $i == 4) {
$alias = $attribute;
} else {
$target = $attribute;
}
$i++;
}
return ($type, $target, $alias, $username, $password);
}
sub logging {
my ($level, $message) = @_;
my $outputMessage = $logDate . " [" . $level . "] " . $message. "\n";
if ($log == true) {
mkdir "$logPath", 0777 unless -d "$logPath";
open LOG, ">>$logFile" or die "Unable to open the logfile specified as $logFile";
print LOG $logDate . " [" . $level . "] " . $message. "\n";
close LOG;
} else {
print $outputMessage;
}
}
sub setupBackupDirectory {
mkdir "$basePath", 0777 unless -d "$basePath";
if ($backupMethod eq "remote") {
`mount -t $rmtfs -o username=$rmtBackupUser,password=$rmtBackupPass $rmtBackupPath $basePath`;
}
}
sub pushFiles {
my $pushPath = $mntPath . $hostname . "/";
my $fileWildcard = "*.tar";
mkdir "$mntPath", 0777 unless -d "$mntPath";
`mount -t $rmtfs -o username=$rmtUsername,password=$rmtPassword $rmtDirectory $mntPath`;
mkdir "$pushPath", 0777 unless -d "$pushPath";
for( <$basePath/$fileWildcard> ){
move ($_, $pushPath) || die "Can't copy $_ $!\n";
}
`umount $mntPath`;
rmtree($mntPath);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment