Skip to content

Instantly share code, notes, and snippets.

@carcam
Last active September 6, 2018 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carcam/8556018 to your computer and use it in GitHub Desktop.
Save carcam/8556018 to your computer and use it in GitHub Desktop.
#!sh
# Filename: RestoreBackups.sh
# Description: This Shell script helps you restore multiple Joomla! site backups created with Akeeba Backup
# Author: Carlos Camara www.cmcamara.com
# License: GPL v2.0 or later
#################
# Configuration #
#################
#
# Absolute path to unjpa file (included in jpa_utils package that you can download from JoomlaCode:
UNJPA_PATH="$HOME/jpa_utils/"
# Absolute path to the folder where all backup files are stored
BACKUPS_FOLDER=$HOME/BackupsToRestore/
# Absolute path to the folder in your webserver where the backups will be restored
WEB_PATH=$HOME/public_html/RestoredBackups/
# DB USer
MYSQL_USER=""
# DB Password
MYSQL_PASS=""
# Database to store restored sites tables
MYSQL_DB="restored_sites"
##We clean up older restored sites
#rm -r $WEB_PATH
#mysql -u $MYSQL_USER -p$MYSQL_PASS -e "DROP DATABASE $MYSQL_DB"
cd $BACKUPS_FOLDER
OLDIFS=$IFS
IFS=$'\n'
SITE_COUNT=0
for BACKUP in `ls`; do
let "++SITE_COUNT"
if [ ! "$BACKUP" == "restauradas" ]; then
cd $BACKUPS_FOLDER
EXTENSION=${BACKUP##*.}
BACKUPFOLDER=${BACKUP%.$EXTENSION}
BACKUPFOLDER=${BACKUPFOLDER// /}
mkdir -p $WEB_PATH/$BACKUPFOLDER
cp ${BACKUP} $WEB:PATH/$BACKUPFOLDER
cd $WEB_PATH/$BACKUPFOLDER
if [ "$EXTENSION" == "jpa" ]; then
cp $UNJPA_PATH/unjpa.php ./
php unjpa.php "${BACKUP}"
rm unjpa.php
elif [ "$EXTENSION" == "zip" ]; then
unzip "${BACKUP}"
fi
rm "${BACKUP}"
#exit
LOGPATH=$WEB_PATH/$BACKUPFOLDER/logs
TMPPATH=$WEB_PATH/$BACKUPFOLDER/tmp
DBPREFIX=$(sed -n 's/.*prefix *= *\([^ ]*.*\)/\1/p' < installation/sql/databases.ini)
DBPREFIX=${DBPREFIX#\"}
OLDDBPREFIX=${DBPREFIX%\"}
$DBPREFIX="res$SITE_COUNT"
sed -i s/#__/$DBPREFIX/g installation/sql/site.sql
echo "Starting DB restoration"
#read startingdb
mysql -u $MYSQL_USER -p$MYSQL_PASS -e "CREATE DATABASE IF NOT EXISTS $MYSQL_DB"
mysql -u $MYSQL_USER -p$MYSQL_PASS $MYSQL_DB < installation/sql/joomla.sql
SQLFILE_COUNT=1
SQLFILE="joomla.s0$SQLFILE_COUNT"
SQLFILE_PATH="$WEB_PATH/$BACKUPFOLDER/installation/sql/"
while [ -f $SQLFILE_PATH/$SQLFILE ]; do
let "++SQLFILE_COUNT"
echo "Starting DB restoration $SQLFILE"
sed -i s/#__/$DBPREFIX/g $SQLFILE_PATH/$SQLFILE
mysql -u $MYSQL_USER -p$MYSQL_PASS $MYSQL_DB < $SQLFILE_PATH/$SQLFILE
if [ $SQLFILE_COUNT lt 10 ];then
SQLFILE="joomla.s0$SQLFILE_COUNT"
else
SQLFILE="joomla.s$SQLFILE_COUNT"
fi
SQLFILE_PATH="$WEB_PATH/$BACKUPFOLDER/installation/sql/"
done
echo "DB restoration finished\n"
sed -i "s/\(\$db *= *\).*/\1\'${MYSQL_DB}\';/" configuration.php
sed -i "s/\(\$user *= *\).*/\1\'${MYSQL_USER}\';/" configuration.php
sed -i "s/\(\$host *= *\).*/\1\'localhost\';/" configuration.php
sed -i "s/\(\$password *= *\).*/\1\'$MYSQL_PASS\';/" configuration.php
sed -i "s/\(\$prefix *= *\).*/\1\'${DBPREFIX}\';/" configuration.php
sed -i "s/\(\$sef_rewrite *= *\).*/\1\'0\';/" configuration.php
sed -i "s|\(\$log_path *= *\).*|\1\'${LOGPATH}\';|" configuration.php
sed -i "s|\(\$tmp_path *= *\).*|\1\'$TMPPATH\';|" configuration.php
#After db restoration we remove the installation folder
rm -r $WEB_PATH/$BACKUPFOLDER/installation
#We change file permissions to be sure everything works in php as a module configurations
chmod 777 -R $WEB_PATH/$BACKUPFOLDER
#Move backup file after restoration
mkdir -p $BACKUPS_FOLDER/restauradas/
mv "$BACKUPS_FOLDER/$BACKUP" "$BACKUPS_FOLDER/restauradas/$BACKUP"
fi
done
IFS=$OLDIFS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment