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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
#!/usr/bin/perl -w
# --
# scripts/backup.pl - the backup script
# Copyright (C) 2001-2005 Martin Edenhofer <martin+code@otrs.org>
# --
# $Id: backup.pl,v 1.2 2005/10/25 18:50:38 martin Exp $
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# --
# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin)."/Kernel/cpan-lib";
use strict;
use vars qw($VERSION);
$VERSION = '$Revision: 1.2 $';
$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;
use Getopt::Std;
use Kernel::Config;
use Kernel::System::Time;
use Kernel::System::DB;
use Kernel::System::Log;
use Date::Pcalc qw(Today Today_and_Now Add_Delta_Days);
# --
# get options
# --
my %Opts = ();
my $Compress = '';
my $CompressCMD = '';
my $FullBackup = 0;
my $DB = '';
my $DBDump = '';
getopt('hcrtd', \%Opts);
if ($Opts{'h'}) {
print "backup.pl <Revision $VERSION> - backup script\n";
print "Copyright (c) 2001-2005 Martin Edenhofer <martin\@otrs.org>\n";
print "usage: backup.pl -d /data_backup_dir/ [-c gzip|bzip2] [-r 30] [-t fullbackup|nofullbackup]\n";
exit 1;
}
# check backup dir
if (!$Opts{'d'}) {
print STDERR "ERROR: Need -d for backup directory\n";
exit (1);
}
elsif (! -d $Opts{'d'}) {
print STDERR "ERROR: No such directory: $Opts{'d'}\n";
exit (1);
}
# check compress mode
if ($Opts{'c'} && $Opts{'c'} =~ /bzip2/i) {
$Compress = 'j';
$CompressCMD = 'bzip';
}
else {
$Compress = 'z';
$CompressCMD = 'gzip';
}
# check backup type
if ($Opts{'t'} && $Opts{'t'} =~ /no/i) {
$FullBackup = 0;
}
else {
$FullBackup = 1;
}
# create common objects
my %CommonObject = ();
$CommonObject{ConfigObject} = Kernel::Config->new();
$CommonObject{TimeObject} = Kernel::System::Time->new(
%CommonObject,
);
$CommonObject{LogObject} = Kernel::System::Log->new(
LogPrefix => 'OTRS-Backup',
%CommonObject,
);
my $DatabaseHost = $CommonObject{ConfigObject}->Get('DatabaseHost');
my $Database = $CommonObject{ConfigObject}->Get('Database');
my $DatabaseUser = $CommonObject{ConfigObject}->Get('DatabaseUser');
my $DatabasePw = $CommonObject{ConfigObject}->Get('DatabasePw');
my $DatabaseDSN = $CommonObject{ConfigObject}->Get('DatabaseDSN');
my $ArticleDir = $CommonObject{ConfigObject}->Get('ArticleDir');
# check db backup support
if ($DatabaseDSN =~ /:mysql/i) {
$DB = 'MySQL';
$DBDump = 'mysqldump';
}
elsif ($DatabaseDSN =~ /:pg/i) {
$DB = 'PostgreSQL';
$DBDump = 'pg_dump';
}
else {
print STDERR "ERROR: Can't backup, no database dump support!\n";
exit (1);
}
# check needed programs
foreach my $CMD ('cp', 'tar', $DBDump, $CompressCMD) {
my $Installed = 0;
open (IN, "which $CMD | ");
while (<IN>) {
$Installed = 1;
}
close (IN);
if (!$Installed) {
print STDERR "ERROR: Can't locate $CMD!\n";
exit (1);
}
}
# remove old backups
#use File::Remove;
if ($Opts{'r'}) {
# my($Year, $Month, $Day) = Today_and_Now();
# my($DYear,$DMonth,$DDay) = Add_Delta_Days($Year, $Month, $Day, -$Opts{'r'});
# my $OLDBACKUP = sprintf("%04d-%02d-%02d*",$DYear,$DMonth,$DDay);
# print "deleting old backups in $Opts{'b'}/$OLDBACKUP...";
# File::Remove::remove \1, "$Opts{'b'}/$OLDBACKUP";
# print "done\n";
}
# create new backup directory
my $Home = $CommonObject{ConfigObject}->Get('Home');
my ($s,$m,$h, $D,$M,$Y) = $CommonObject{TimeObject}->SystemTime2Date(
SystemTime => $CommonObject{TimeObject}->SystemTime(),
);
my $Directory = "$Opts{'d'}/$Y-$M-$D"."_"."$h-$m";
if (!mkdir($Directory)) {
print STDERR "ERROR: Can't create directory: $Directory: $!\n";
exit (1);
}
# backup Kernel/Config.pm
print "Backup $Directory/Config.tar.gz ... ";
if (!system("cd $Home && tar -czf $Directory/Config.tar.gz Kernel/Config.pm Kernel/Config/Files/ZZZA*.pm Kernel/Config/GenericAgen*.pm")) {
print "done\n";
}
else {
print "failed\n";
}
# backup application
if ($FullBackup) {
print "Backup $Directory/Application.tar.gz ... ";
if (!system("cd $Home && tar -czf $Directory/Application.tar.gz .")) {
print "done\n";
}
else {
print "failed\n";
}
}
# backup vardir
else {
print "Backup $Directory/VarDir.tar.gz ... ";
if (!system("cd $Home && tar -czf $Directory/VarDir.tar.gz var/")) {
print "done\n";
}
else {
print "failed\n";
}
}
# backup datadir
if ($ArticleDir !~ /\Q$Home\E/) {
print "Backup $Directory/DataDir.tar.gz ... ";
if (!system("cd $ArticleDir && tar -czf $Directory/DataDir.tar.gz .")) {
print "done\n";
}
else {
print "failed\n";
}
}
# backup database
if ($DB =~ /mysql/i) {
print "Dump $DB rdbms ... ";
if ($DatabasePw) {
$DatabasePw = "-p$DatabasePw";
}
if (!system("$DBDump -u $DatabaseUser $DatabasePw -h $DatabaseHost $Database > $Directory/DatabaseBackup.sql")) {
print "done\n";
}
else {
print "failed\n";
}
}
else {
print "Dump $DB rdbms ... ";
if (!system("$DBDump -f $Directory/DatabaseBackup.sql -h $DatabaseHost -U $DatabaseUser $Database")) {
print "done\n";
}
else {
print "failed\n";
}
}
# compressing database
print "Compress SQL-file... ";
if (!system("$CompressCMD $Directory/DatabaseBackup.sql")) {
print "done\n";
}
else {
print "failed\n";
}
|