###!/usr/bin/perl use strict; use File::Find; use File::Path qw(mkpath rmtree); use File::Copy; use Time::localtime qw(localtime); use Time::Local; use DBI; my $dbuser = 'root'; my $dbpassword = 'fbg4ips'; my $dbhost = '127.0.0.1'; my $database = $ARGV[0]; unless ($database){ print "Usage: $0 oakdale_1\n";exit; } CronExpired(); Start(); sub Start(){ my $tm = localtime(time); my $datetime = sprintf("%02d-%02d-%04d %02d:%02d:%02d",$tm->mday,($tm->mon+1),($tm->year + 1900), $tm->hour, $tm->min, $tm->sec); mkdir ("/SPACEPOS_BACKUP") unless -d "/SPACEPOS_BACKUP"; mkdir ("/SPACEPOS_BACKUP/backup_".$tm->mday."-".($tm->mon+1)."-".($tm->year + 1900)."_".$tm->hour."h".$tm->min."m".$tm->sec."s") unless -d "/SPACEPOS_BACKUP/backup_".$tm->mday."-".($tm->mon+1)."-".($tm->year + 1900)."_".$tm->hour."h".$tm->min."m".$tm->sec."s"; my $backup = "/SPACEPOS_BACKUP/backup_".$tm->mday."-".($tm->mon+1)."-".($tm->year + 1900)."_".$tm->hour."h".$tm->min."m".$tm->sec."s"; foreach("extranet","extranet/pos","extranet/data","socket","socket/jobs","database","database/$database","database/spaceusers"){ mkdir ("$backup/$_") unless -d "$backup/$_"; } my $dbh = DBI->connect("DBI:mysql:;host=$dbhost",$dbuser,$dbpassword) or die $DBI::errstr; my $results = $dbh->prepare("flush tables;") or die $dbh->errstr(); $results->execute() or die $results->errstr(); my $file; #extranet/data $file = "billtext.txt"; if (! copyfile("/wwwroot/extranet/data/$file","$backup/extranet/data/$file") ) { logit("ERROR WITH FILE: $file"); } $file = "billfooter.txt"; if (! copyfile("/wwwroot/extranet/data/$file","$backup/extranet/data/$file") ) { logit("ERROR WITH FILE: $file"); } #extranet/pos my $destPath; find ( sub { return unless -f; $destPath = $File::Find::dir; $destPath =~ s/\/wwwroot\/extranet\/pos//; mkpath "$backup/extranet/pos/$destPath" unless -d "$backup/extranet/pos/$destPath"; copy "$_", "$backup/extranet/pos/$destPath" || logit("ERROR WITH FILE: $File::Find::name"); }, "/wwwroot/extranet/pos"); #extranet $file = "settings_pm.sto"; if (! copyfile("/wwwroot/extranet/$file","$backup/extranet/$file") ) { logit("ERROR WITH FILE: $file"); } $file = "printsetup_pm.sto"; if (! copyfile("/wwwroot/extranet/$file","$backup/extranet/$file") ) { logit("ERROR WITH FILE: $file"); } $file = "lflag.pm"; if (! copyfile("/wwwroot/extranet/$file","$backup/extranet/$file") ) { logit("ERROR WITH FILE: $file"); } $file = "largelogo.png"; if (! copyfile("/wwwroot/extranet/$file","$backup/extranet/$file") ) { logit("ERROR WITH FILE: $file"); } #JOBS Folder structure opendir(IPS,"/socket/jobs"); foreach my $ip (grep {/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/} readdir(IPS)) { mkdir ("$backup/socket/jobs/$ip") unless -d "$backup/socket/jobs/$ip"; #duplicate structures opendir(QUES,"/socket/jobs/$ip"); foreach my $que (grep {! /^\.|^\.\./} readdir(QUES)) { mkdir ("$backup/socket/jobs/$ip/$que") unless -d "$backup/socket/jobs/$ip/$que"; #duplicate structures } closedir(QUES); } closedir(IPS); #mysql database opendir(DBS,"/mysql/data/$database"); foreach my $file (grep {! /^\.|^\.\./} readdir(DBS)) { if (! copyfile("/mysql/data/$database/$file","$backup/database/$database/$file") ) { lotit("ERROR WITH FILE: $file"); } } closedir(DBS); opendir(DBS,"/mysql/data/spaceusers"); foreach my $file (grep {! /^\.|^\.\./} readdir(DBS)) { if (! copyfile("/mysql/data/spaceusers/$file","$backup/database/spaceusers/$file") ) { logit("ERROR WITH FILE: $file"); } } closedir(DBS); logit("Done"); } sub CronExpired{ #let's keep last 10 copies my $path = '/SPACEPOS_BACKUP'; opendir(DIR, $path); while( defined(my $fn=readdir(DIR)) ) { next if $fn=~/^\.{1,2}$/; my $file = "$path/$fn"; rmtree($file) if -d $file && (time -(lstat($file))[9]) > 86400 * 10; #24 hours x 5 } closedir DIR; print "Done\n"; } sub logit{ my ($year,$month,$day,$hour,$minute,$second) = getTime(); open FILE,">>backup.log.txt"; print FILE "\n==== $year-$month-$day $hour:$minute:$second ===\n";print FILE shift;print "\n"; close FILE; } sub getTime { my ($time) = @_; my @t = localtime(time ); return ( sprintf("%04d",$t[5]+1900), sprintf("%02d",$t[4]+1), sprintf("%02d",$t[3]), sprintf("%02d",$t[2]), sprintf("%02d",$t[1]), sprintf("%02d",$t[0]) ); } sub copyfile() { if (open(IN, "< $_[0]") ) {# or die "can't open $_[0]: $!"; open(OUT, "> $_[1]") ;# or die "can't open $_[1]: $!"; binmode (IN); binmode (OUT); my $blksize = (stat IN)[11] || 16384; # preferred block size? my ($len,$buf,$written); while ($len = sysread IN, $buf, $blksize) { if (!defined $len) { next if $! =~ /^Interrupted/; # ^Z and fg die "System read error: $!\n"; } my $offset = 0; while ($len) { # Handle partial writes. defined($written = syswrite OUT, $buf, $len, $offset) or die "System write error: $!\n"; $len -= $written; $offset += $written; }; } close(IN); close(OUT); return 1; } else { return 0; } }