/usr/share/otrs/scripts/backup.pl is in otrs2 6.0.5-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | #!/usr/bin/perl
# --
# Copyright (C) 2001-2018 OTRS AG, http://otrs.com/
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# 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 Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --
use strict;
use warnings;
# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . "/Kernel/cpan-lib";
use Getopt::Std;
use Kernel::System::ObjectManager;
# get options
my %Opts;
my $Compress = '';
my $CompressCMD = '';
my $CompressEXT = '';
my $DB = '';
my $DBDump = '';
getopt( 'hcrtd', \%Opts );
if ( exists $Opts{h} ) {
print <<EOF;
Backup an OTRS system.
Usage:
backup.pl -d /data_backup_dir [-c gzip|bzip2] [-r DAYS] [-t fullbackup|nofullbackup|dbonly]
Options:
-d - Directory where the backup files should place to.
[-c] - Select the compression method (gzip|bzip2). Default: gzip.
[-r DAYS] - Remove backups which are more than DAYS days old.
[-t] - Specify which data will be saved (fullbackup|nofullbackup|dbonly). Default: fullbackup.
[-h] - Display help for this command.
Help:
Using -t fullbackup saves the database and the whole OTRS home directory (except /var/tmp and cache directories).
Using -t nofullbackup saves only the database, /Kernel/Config* and /var directories.
With -t dbonly only the database will be saved.
Output:
Config.tar.gz - Backup of /Kernel/Config* configuration files.
Application.tar.gz - Backup of application file system (in case of full backup).
VarDir.tar.gz - Backup of /var directory (in case of no full backup).
DataDir.tar.gz - Backup of article files.
DatabaseBackup.sql.gz - Database dump.
EOF
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} =~ m/bzip2/i ) {
$Compress = 'j';
$CompressCMD = 'bzip2';
$CompressEXT = 'bz2';
}
else {
$Compress = 'z';
$CompressCMD = 'gzip';
$CompressEXT = 'gz';
}
# check backup type
my $DBOnlyBackup = 0;
my $FullBackup = 0;
if ( $Opts{t} && $Opts{t} eq 'dbonly' ) {
$DBOnlyBackup = 1;
}
elsif ( $Opts{t} && $Opts{t} eq 'nofullbackup' ) {
$FullBackup = 0;
}
else {
$FullBackup = 1;
}
# create common objects
local $Kernel::OM = Kernel::System::ObjectManager->new(
'Kernel::System::Log' => {
LogPrefix => 'OTRS-backup.pl',
},
);
my $DatabaseHost = $Kernel::OM->Get('Kernel::Config')->Get('DatabaseHost');
my $Database = $Kernel::OM->Get('Kernel::Config')->Get('Database');
my $DatabaseUser = $Kernel::OM->Get('Kernel::Config')->Get('DatabaseUser');
my $DatabasePw = $Kernel::OM->Get('Kernel::Config')->Get('DatabasePw');
my $DatabaseDSN = $Kernel::OM->Get('Kernel::Config')->Get('DatabaseDSN');
my $ArticleDir = $Kernel::OM->Get('Kernel::Config')->Get('Ticket::Article::Backend::MIMEBase::ArticleDataDir');
# decrypt pw (if needed)
if ( $DatabasePw =~ m/^\{(.*)\}$/ ) {
$DatabasePw = $Kernel::OM->Get('Kernel::System::DB')->_Decrypt($1);
}
# check db backup support
if ( $DatabaseDSN =~ m/:mysql/i ) {
$DB = 'MySQL';
$DBDump = 'mysqldump';
}
elsif ( $DatabaseDSN =~ m/:pg/i ) {
$DB = 'PostgreSQL';
$DBDump = 'pg_dump';
if ( $DatabaseDSN !~ m/host=/i ) {
$DatabaseHost = '';
}
}
else {
print STDERR "ERROR: Can't backup, no database dump support!\n";
exit(1);
}
# check needed programs
for my $CMD ( 'cp', 'tar', $DBDump, $CompressCMD ) {
my $Installed = 0;
open my $In, '-|', "which $CMD"; ## no critic
while (<$In>) {
$Installed = 1;
}
close $In;
if ( !$Installed ) {
print STDERR "ERROR: Can't locate $CMD!\n";
exit 1;
}
}
# create new backup directory
my $Home = $Kernel::OM->Get('Kernel::Config')->Get('Home');
# append trailing slash to home directory, if it's missing
if ( $Home !~ m{\/\z} ) {
$Home .= '/';
}
chdir($Home);
# create directory name - this looks like 2013-09-09_22-19'
my $Directory = $Kernel::OM->Create('Kernel::System::DateTime')->Format(
Format => $Opts{d} . '/%Y-%m-%d_%H-%M',
);
if ( !mkdir($Directory) ) {
die "ERROR: Can't create directory: $Directory: $!\n";
}
# backup application
if ($DBOnlyBackup) {
print "Backup of filesystem data disabled by parameter dbonly ... \n";
}
else {
# backup Kernel/Config.pm
print "Backup $Directory/Config.tar.$CompressEXT ... ";
if ( !system("tar -ch -$Compress -f $Directory/Config.tar.$CompressEXT Kernel/Config*") ) {
print "done\n";
}
else {
print "failed\n";
RemoveIncompleteBackup($Directory);
die "Backup failed\n";
}
if ($FullBackup) {
print "Backup $Directory/Application.tar.$CompressEXT ... ";
my $Excludes = "--exclude=var/tmp --exclude=js-cache --exclude=css-cache --exclude=.git";
if ( !system("tar $Excludes -ch -$Compress -f $Directory/Application.tar.$CompressEXT .") ) {
print "done\n";
}
else {
print "failed\n";
RemoveIncompleteBackup($Directory);
die "Backup failed\n";
}
}
# backup vardir
else {
print "Backup $Directory/VarDir.tar.$CompressEXT ... ";
if ( !system("tar -ch -$Compress -f $Directory/VarDir.tar.$CompressEXT var/") ) {
print "done\n";
}
else {
print "failed\n";
RemoveIncompleteBackup($Directory);
die "Backup failed\n";
}
}
# backup datadir
if ( $ArticleDir !~ m/\A\Q$Home\E/ ) {
print "Backup $Directory/DataDir.tar.$CompressEXT ... ";
if ( !system("tar -ch -$Compress -f $Directory/DataDir.tar.$CompressEXT $ArticleDir") ) {
print "done\n";
}
else {
print "failed\n";
RemoveIncompleteBackup($Directory);
die "Backup failed\n";
}
}
}
# backup database
if ( $DB =~ m/mysql/i ) {
print "Dump $DB data to $Directory/DatabaseBackup.sql.$CompressEXT ... ";
if ($DatabasePw) {
$DatabasePw = "-p'$DatabasePw'";
}
if (
!system(
"$DBDump -u $DatabaseUser $DatabasePw -h $DatabaseHost $Database | $CompressCMD > $Directory/DatabaseBackup.sql.$CompressEXT"
)
)
{
print "done\n";
}
else {
print "failed\n";
RemoveIncompleteBackup($Directory);
die "Backup failed\n";
}
}
else {
print "Dump $DB data to $Directory/DatabaseBackup.sql ... ";
# set password via environment variable if there is one
if ($DatabasePw) {
$ENV{'PGPASSWORD'} = $DatabasePw; ## no critic
}
if ($DatabaseHost) {
$DatabaseHost = "-h $DatabaseHost"
}
if (
!system(
"$DBDump $DatabaseHost -U $DatabaseUser $Database | $CompressCMD > $Directory/DatabaseBackup.sql.$CompressEXT"
)
)
{
print "done\n";
}
else {
print "failed\n";
RemoveIncompleteBackup($Directory);
die "Backup failed\n";
}
}
# remove old backups only after everything worked well
if ( defined $Opts{r} ) {
my %LeaveBackups;
my $SystemDTObject = $Kernel::OM->Create('Kernel::System::DateTime');
# we'll be substracting days to the current time
# we don't want DST changes to affect our dates
# if it is < 2:00 AM, add two hours so we're sure DST will not change our timestamp
# to another day
if ( $SystemDTObject->Get()->{Hour} < 2 ) {
$SystemDTObject->Add( Hours => 2 );
}
for ( 0 .. $Opts{r} ) {
# legacy, old directories could be in the format 2013-4-8
my @LegacyDirFormats = (
'%04d-%01d-%01d',
'%04d-%02d-%01d',
'%04d-%01d-%02d',
'%04d-%02d-%02d',
);
my $SystemDTDetails = $SystemDTObject->Get();
for my $LegacyFirFormat (@LegacyDirFormats) {
my $Dir = sprintf(
$LegacyFirFormat,
$SystemDTDetails->{Year},
$SystemDTDetails->{Month},
$SystemDTDetails->{Day},
);
$LeaveBackups{$Dir} = 1;
}
# substract one day
$SystemDTObject->Subtract( Days => 1 );
}
my @Directories = $Kernel::OM->Get('Kernel::System::Main')->DirectoryRead(
Directory => $Opts{d},
Filter => '*',
);
DIRECTORY:
for my $Directory (@Directories) {
next DIRECTORY if !-d $Directory;
my $Leave = 0;
for my $Data ( sort keys %LeaveBackups ) {
if ( $Directory =~ m/$Data/ ) {
$Leave = 1;
}
}
if ( !$Leave ) {
# remove files and directory
print "deleting old backup in $Directory ... ";
my @Files = $Kernel::OM->Get('Kernel::System::Main')->DirectoryRead(
Directory => $Directory,
Filter => '*',
);
for my $File (@Files) {
if ( -e $File ) {
# print "Notice: remove $File\n";
unlink $File;
}
}
if ( rmdir($Directory) ) {
print "done\n";
}
else {
die "failed\n";
}
}
}
}
# If error occurs this functions remove incomlete backup folder to avoid the impression
# that the backup was ok (see http://bugs.otrs.org/show_bug.cgi?id=10665).
sub RemoveIncompleteBackup {
# get parameters
my $Directory = shift;
# remove files and directory
print STDERR "Deleting incomplete backup $Directory ... ";
my @Files = $Kernel::OM->Get('Kernel::System::Main')->DirectoryRead(
Directory => $Directory,
Filter => '*',
);
for my $File (@Files) {
if ( -e $File ) {
unlink $File;
}
}
if ( rmdir($Directory) ) {
print STDERR "done\n";
}
else {
print STDERR "failed\n";
}
return;
}
|