5
Quote:
wcrwcr wrote:
Hi bungoman
Quote:(using a nice little perl script I made that copies XOOPS and the mySQL database for backup and allows me to revert to any previous version I want).
Any chance to share that very interesting script?
#!/usr/local/bin/perl -w
# ******************************************************************************
# File: xoopsbackup.pl
#
# Author: Sean Eshbaugh
#
# Created: 06-01-2005
#
# Description:
# Creates a backup of the current XOOPS mySQL database and puts it in the
# current XOOPS directory. Then a copy of the XOOPS directory is made and the
# version number associated with it is incrimented by one. All necessary
# changes to file permissions and associations are then made. The symbolic
# link from the Apache htdocs directory is changed to the new XOOPS directory.
# Finally the old XOOPS database is removed from the new XOOPS folder to
# prevent unecessary clutter.
#
# Arguments:
# <arg1> - <password>
#
# Returns:
# 0 on success, 1 on failure
#
# ******************************************************************************
# $Id$
# ******************** Globals *************************************************
use strict;
use Cwd;
use File::Basename;
my $usage = "nUsage: xoopsbackup.pl <mysql_password>nn";
my $password = "";
my $basepath = "/usr/local/apache2";
# ******************** Main ****************************************************
# Parse the command line arguments and get the mySQL database password
&parse_cmd_line;
# Set umask
umask 002;
# cd to the Apache directory
chdir($basepath) || die "Could not cd to $basepathn";
# Get the name of the current XOOPS directory from the htdocs link
print STDERR "Getting current XOOPS path...n";
my $current_xoops_dir = &get_current_xoops_dir;
# Get the version number from the XOOPS directory name
print STDERR "Getting current XOOPS backup version...n";
my $current_version = &get_current_version;
# Incriment the current version by one to get the next version
print STDERR "Getting new XOOPS backup version...n";
my $next_version = $current_version + 1;
# Create the new XOOPS directory name
print STDERR "Getting new XOOPS path...n";
my $new_xoops_dir = sprintf("htdocs.xoops.v%03d", $next_version);
# Backup the mySQL database to a file
print STDERR "Backing up mySQL dbase...n";
&backup_dbase;
# Copy existing XOOPS installation to a new directory
&seed_new_dir;
print STDERR "Done backing up Xoops.n";
# ******************** Subs ****************************************************
# Parse cmd line args
sub parse_cmd_line
{
unless (@ARGV == 1)
{
die $usage;
}
$password = $ARGV[0];
}
# Get the current XOOPS directory
sub get_current_xoops_dir
{
unless (-l 'htdocs')
{
die "Could not find htdocs link in current dir: $basepathn";
}
return readlink('htdocs');
}
sub get_current_version
{
if ($current_xoops_dir =~ /.v(ddd)$/)
{
return $1;
}
else
{
die "Could not extract version from current XOOPS path: $current_xoops_dirn";
}
}
sub backup_dbase
{
my $outfile = "$basepath/htdocs/xoopsdb.v" . sprintf("%03d", $current_version) . ".sql";
if (-f $outfile)
{
unlink($outfile) || die "Failed to delete $outfilen";
}
my $mysql_cmd = "/usr/local/mysql-4.1.7/bin/mysqldump -u root -p$password XOOPS ";
unless (system("$mysql_cmd > $outfile") == 0)
{
die "System call failed: $mysql_cmdn";
}
}
sub seed_new_dir
{
# Copy the current XOOPS directory to a new one
print STDERR "Copying old XOOPS directory to new XOOPS directory...n";
my $cmd = "/bin/cp -pr $current_xoops_dir $new_xoops_dir";
unless (system($cmd) == 0)
{
die "System call failed: $cmdn";
}
# Change group ownership of the new XOOPS directory to techweb
print STDERR "Changing group ownership of new XOOPS directory to techweb...n";
$cmd = "/bin/chgrp -R techweb $new_xoops_dir";
unless (system($cmd) == 0)
{
die "System call failed: $cmdn";
}
# Add group write privleges to the new directory
print STDERR "Adding group write privleges to new XOOPS directory...n";
$cmd = "/bin/chmod -R g+w $new_xoops_dir";
unless (system($cmd) == 0)
{
die "System call failed: $cmdn";
}
# Update XOOPS root path in mainfile.php
print STDERR "Updating XOOPS root path...n";
$cmd = "/bin/chmod u+w $new_xoops_dir/mainfile.php";
unless (system($cmd) == 0)
{
die "System call failed: $cmdn";
}
&update_xoops_root_path;
# Set privleges to read only for the new mainfile.php
print STDERR "Setting privleges to read only for the new mainfile.php...n";
$cmd = "/bin/chmod 444 $new_xoops_dir/mainfile.php";
unless (system($cmd) == 0)
{
die "System call failed: $cmdn";
}
# Change symbolic link to point to the newly created XOOPS directory
print STDERR "Changing htdocs symbolic link to point to new XOOPS directory...n";
$cmd = "/bin/rm -f htdocs; /bin/ln -s $new_xoops_dir htdocs";
unless (system($cmd) == 0)
{
die "System call failed: $cmdn";
}
# Remove mySQL database backup from new directory
print STDERR "Removing old mySQL database backup from new XOOPS directory...n";
chdir ('htdocs') || die "Could not cd into newly created 'htdocs' directoryn";
my $oldfile = "$basepath/htdocs/xoopsdb.v" . sprintf("%03d", $current_version) . ".sql";
$cmd = "/bin/rm -f $oldfile";
unless (system($cmd) == 0)
{
die "System call failed: $cmdn";
}
}
sub update_xoops_root_path
{
my $file = "$new_xoops_dir/mainfile.php";
my @array = &load_file_to_array($file);
open(OUT, ">$file") || die "Could not open $file for writtingn";
foreach my $line (@array)
{
$line =~ s/$current_xoops_dir/$new_xoops_dir/;
print OUT "$linen";
}
close(OUT);
}
# Read the contents of a file into an array
sub load_file_to_array
{
my $file = shift;
my @array = ();
open(LFTA, $file) || die "Could not open $file for readingn";
while (<LFTA>)
{
$_ =~ s/r//;
chomp $_;
push(@array, $_);
}
close(LFTA);
return @array;
}
and...
#!/usr/local/bin/perl -w
# ******************************************************************************
# File: xoopsrevert.pl
#
# Author: Sean Eshbaugh
#
# Created: 06-03-2005
#
# Description:
#
# Arguments:
# <arg1> - <password>, <arg2> - <version to revert to>
#
# Returns:
# 0 on success, 1 on failure
#
# ******************************************************************************
# $Id$
# ******************** Globals *************************************************
use strict;
use Cwd;
use File::Basename;
my $usage = "nUsage: xoopsrevert.pl <password> <requested_version>nn";
my $basepath = "/usr/local/apache2/";
my $password = "";
my $requested_version = 0;
# ******************** Main ****************************************************
# Parse the command line arguments and get the mySQL database password
&parse_cmd_line;
# Set umask
umask 002;
# cd to the Apache directory
chdir($basepath) || die "Could not cd to $basepathn";
# Get the name of the current XOOPS directory from the htdocs link
print STDERR "Getting current XOOPS path...n";
my $current_xoops_dir = &get_current_xoops_dir;
# Check to see if the requested version exists
my $requested_version_dir = "htdocs.xoops.v" . sprintf("%03d", $requested_version);
unless (-d $requested_version_dir)
{
die "Could not find requested backup versionn";
}
if ($current_xoops_dir eq $requested_version_dir)
{
die "Current XOOPS backup requestedn";
}
print STDERR "$current_xoops_dirn";
print STDERR "$requested_version_dirn";
# Change symbolic link to point to the old XOOPS directory
print STDERR "Changing htdocs symbolic link to point to old XOOPS directory...n";
my $cmd = "/bin/rm -f htdocs; /bin/ln -s $requested_version_dir htdocs";
unless (system($cmd) == 0)
{
die "System call failed: $cmdn";
}
# Edit the mySQL backup file so that mySQL can use it
print STDERR "Editing the mySQL database file...n";
&edit_mysql_file;
# ******************** Subs ****************************************************
# Parse cmd line args
sub parse_cmd_line
{
unless (@ARGV == 2)
{
die $usage;
}
$password = $ARGV[0];
$requested_version = $ARGV[1];
}
# Get the current XOOPS directory
sub get_current_xoops_dir
{
unless (-l 'htdocs')
{
die "Could not find htdocs link in current dir: $basepathn";
}
return readlink('htdocs');
}
sub edit_mysql_file
{
my $file = "$requested_version_dir/xoopsdb.v" . sprintf("%03d", $requested_version) . ".sql";
my @array = &load_file_to_array($file);
open(OUT, ">$file") || die "Could not open $file for writtingn";
print OUT "use xoopsn";
foreach my $line (@array)
{
print OUT "$linen";
}
close(OUT);
my $mysql_cmd = "/usr/local/mysql-4.1.7/bin/mysql -u root -p$password < $file";
unless (system($mysql_cmd) == 0)
{
die "Could not load the mySQL database filen";
}
}
# Read the contents of a file into an array
sub load_file_to_array
{
my $file = shift;
my @array = ();
open(LFTA, $file) || die "Could not open $file for readingn";
while (<LFTA>)
{
$_ =~ s/r//;
chomp $_;
push(@array, $_);
}
close(LFTA);
return @array;
}