/usr/share/otrs/scripts/restore.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 | #!/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 File::Spec qw(catfile);
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 $DB = '';
my $DBDump = '';
my $DecompressCMD = '';
my $Installed = 0;
getopt( 'hbd', \%Opts );
if ( exists $Opts{h} ) {
print <<EOF;
Restore an OTRS system from backup.
Usage:
restore.pl -b /data_backup/<TIME>/ -d /opt/otrs/
Options:
-b - Directory of the backup files.
-d - Target OTRS home directory.
[-h] - Display help for this command.
EOF
exit 1;
}
if ( !$Opts{b} ) {
print STDERR "ERROR: Need -b for backup directory\n";
exit 1;
}
elsif ( !-d $Opts{b} ) {
print STDERR "ERROR: No such directory: $Opts{b}\n";
exit 1;
}
if ( !$Opts{d} ) {
print STDERR "ERROR: Need -d for destination directory\n";
exit 1;
}
elsif ( !-d $Opts{d} ) {
print STDERR "ERROR: No such directory: $Opts{d}\n";
exit 1;
}
if ( -e File::Spec->catfile( $Opts{b}, 'DatabaseBackup.sql.bz2' ) ) {
$DecompressCMD = 'bunzip2';
}
else {
$DecompressCMD = 'gunzip';
}
# check needed programs
for my $CMD ( 'cp', 'tar', $DecompressCMD ) {
$Installed = 0;
open( my $Input, '-|', "which $CMD" ); ## no critic
while (<$Input>) {
$Installed = 1;
}
close $Input;
if ( !$Installed ) {
print STDERR "ERROR: Can't locate $CMD!\n";
exit 1;
}
}
# restore config
chdir( $Opts{d} );
my $ConfigBackupGz = File::Spec->catfile( $Opts{b}, 'Config.tar.gz' );
my $ConfigBackupBz2 = File::Spec->catfile( $Opts{b}, 'Config.tar.bz2' );
if ( -e $ConfigBackupGz ) {
print "Restore $ConfigBackupGz ...\n";
system("tar -xzf $ConfigBackupGz");
}
elsif ( -e $ConfigBackupBz2 ) {
print "Restore $ConfigBackupBz2 ...\n";
system("tar -xjf $ConfigBackupBz2");
}
# create common objects
local $Kernel::OM = Kernel::System::ObjectManager->new(
'Kernel::System::Log' => {
LogPrefix => 'OTRS-restore.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 = 'mysql';
$Installed = 0;
open( my $Input, '-|', "which $DBDump" ); ## no critic
while (<$Input>) {
$Installed = 1;
}
close $Input;
if ( !$Installed ) {
print STDERR "ERROR: Can't locate $DBDump!\n";
exit 1;
}
}
elsif ( $DatabaseDSN =~ m/:pg/i ) {
$DB = 'PostgreSQL';
$DBDump = 'psql';
if ( $DatabaseDSN !~ m/host=/i ) {
$DatabaseHost = ''
}
$Installed = 0;
open( my $Input, '-|', "which $DBDump" ); ## no critic
while (<$Input>) {
$Installed = 1;
}
close $Input;
if ( !$Installed ) {
print STDERR "ERROR: Can't locate $DBDump!\n";
exit 1;
}
}
else {
print STDERR "ERROR: Can't backup, no database dump support!\n";
exit 1;
}
# check database env
if ( $DB =~ m/mysql/i ) {
$Kernel::OM->Get('Kernel::System::DB')->Prepare( SQL => "SHOW TABLES" );
my $Check = 0;
while ( my @RowTmp = $Kernel::OM->Get('Kernel::System::DB')->FetchrowArray() ) {
$Check++;
}
if ($Check) {
print STDERR
"ERROR: Already existing tables in this database. A empty database is required for restore!\n";
exit 1;
}
}
else {
$Kernel::OM->Get('Kernel::System::DB')->Prepare(
SQL =>
"SELECT table_name FROM information_schema.tables WHERE table_catalog = 'otrs' AND table_schema = 'public'",
);
my $Check = 0;
while ( my @RowTmp = $Kernel::OM->Get('Kernel::System::DB')->FetchrowArray() ) {
$Check++;
}
if ($Check) {
print STDERR
"ERROR: Already existing tables in this database. A empty database is required for restore!\n";
exit 1;
}
}
# restore
my $Home = $Kernel::OM->Get('Kernel::Config')->Get('Home');
chdir($Home);
# extract application
my $ApplicationBackupGz = File::Spec->catfile( $Opts{b}, 'Application.tar.gz' );
my $ApplicationBackupBz2 = File::Spec->catfile( $Opts{b}, 'Application.tar.bz2' );
if ( -e $ApplicationBackupGz ) {
print "Restore $ApplicationBackupGz ...\n";
system("tar -xzf $ApplicationBackupGz");
}
elsif ( -e $ApplicationBackupBz2 ) {
print "Restore $ApplicationBackupBz2 ...\n";
system("tar -xjf $ApplicationBackupBz2");
}
# extract vardir
my $VarDirBackupGz = File::Spec->catfile( $Opts{b}, 'VarDir.tar.gz' );
my $VarDirBackupBz2 = File::Spec->catfile( $Opts{b}, 'VarDir.tar.bz2' );
if ( -e $VarDirBackupGz ) {
print "Restore $VarDirBackupGz ...\n";
system("tar -xzf $VarDirBackupGz");
}
elsif ( -e $VarDirBackupBz2 ) {
print "Restore $VarDirBackupBz2 ...\n";
system("tar -xjf $VarDirBackupBz2");
}
# extract datadir
my $DataDirBackupGz = File::Spec->catfile( $Opts{b}, 'DataDir.tar.gz' );
my $DataDirBackupBz2 = File::Spec->catfile( $Opts{b}, 'DataDir.tar.bz2' );
if ( -e $DataDirBackupGz ) {
print "Restore $DataDirBackupGz ...\n";
system("tar -xzf $DataDirBackupGz");
}
if ( -e $DataDirBackupBz2 ) {
print "Restore $DataDirBackupBz2 ...\n";
system("tar -xjf $DataDirBackupBz2");
}
# restore symlinked structure
if (-e "$Opts{'b'}/SymLinks.tar.gz") {
print "Restore $Opts{'b'}/SymLinks.tar.gz ...\n";
mkdir $Opts{'d'}."/tmpdir";
# deflate Symlinks into temporary directory
system("cd $Opts{'d'}.\"/tmpdir/\" && tar -xzf $Opts{'b'}/SymLinks.tar.gz");
# check if the destination files exist or abort
system("cd $Opts{'d'} && ".'find tmpdir/ -type l -print0 | xargs -0 perl -e \'foreach(@ARGV) { die "Destination: ".readlink($_)." exists! Abort." if (not -x readlink) }\'');
# otherwise restore them to the location from the symlink
system("cd $Opts{'d'} && ".'find tmpdir/ -type l -print0 | xargs -0 perl -e \'foreach (@ARGV) { $link = readlink; $_ =~ s|[^/]*/||; system("mv $_ $link"); symlink $link, $_ }\'');
# delete temporary directory
system("cd $Opts{'d'} && rm -rf tmpdir");
}
# import database
my $DatabaseBackupGz = File::Spec->catfile( $Opts{b}, 'DatabaseBackup.sql.gz' );
my $DatabaseBackupBz2 = File::Spec->catfile( $Opts{b}, 'DatabaseBackup.sql.bz2' );
if ( $DB =~ m/mysql/i ) {
print "create $DB\n";
if ($DatabasePw) {
$DatabasePw = "-p'$DatabasePw'";
}
if ( -e $DatabaseBackupGz ) {
print "Restore database into $DB ...\n";
system(
"gunzip -c $DatabaseBackupGz | mysql -f -u$DatabaseUser $DatabasePw -h$DatabaseHost $Database"
);
}
elsif ( -e $DatabaseBackupBz2 ) {
print "Restore database into $DB ...\n";
system(
"bunzip2 -c $DatabaseBackupBz2 | mysql -f -u$DatabaseUser $DatabasePw -h$DatabaseHost $Database"
);
}
}
else {
if ($DatabaseHost) {
$DatabaseHost = "-h $DatabaseHost"
}
if ( -e $DatabaseBackupGz ) {
# set password via environment variable if there is one
if ($DatabasePw) {
$ENV{'PGPASSWORD'} = $DatabasePw; ## no critic
}
print "Restore database into $DB ...\n";
system(
"gunzip -c $DatabaseBackupGz | psql -U$DatabaseUser $DatabaseHost $Database"
);
}
elsif ( -e $DatabaseBackupBz2 ) {
# set password via environment variable if there is one
if ($DatabasePw) {
$ENV{'PGPASSWORD'} = $DatabasePw; ## no critic
}
print "Restore database into $DB ...\n";
system(
"bunzip2 -c $DatabaseBackupBz2 | psql -U$DatabaseUser $DatabaseHost $Database"
);
}
}
|