About how to clone a Prestashop instance

Lately, I’ve been helping my neighbor with his Prestashop website. I’ve learned a few tricks, like editing the overall appearance of the site, making and restoring backups in a limited environment, but my best invention has been cloning the Prestashop instance to have a test version where I can try things out without fear of breaking anything. Here’s the code for anyone who might find it useful, licensed under GPL 3.

You can see the code here, too: https://gist.github.com/axelei/dafd305213ed1b2655545f06edfbc905

# Programa para clonar prestashop
# Copyright Krusher 2015 - Licenciado bajo GPL3

paramfile="httpdocs/app/config/parameters.php"
extractDatabaseName="s/.*'database_name' => '\(.*\)'.*/\1/p"
extractDatabaseUser="s/.*'database_user' => '\(.*\)'.*/\1/p"
extractDatabasePassword="s/.*'database_password' => '\(.*\)'.*/\1/p"

if [ "$#" -lt 4 ]; then
  echo "Error: Se requieren 3 argumentos."
  echo "Uso: $0 dir_web_original dir_web_destino url_original url_destino"
  exit 1
fi

echo Traspasando de $1 a $2 \(urls: $3 -- $4 \)

databaseName=$(sed -n "$extractDatabaseName" $1/app/config/parameters.php)
databaseUser=$(sed -n "$extractDatabaseUser" $1/app/config/parameters.php)
databasePassword=$(sed -n "$extractDatabasePassword" $1/app/config/parameters.php)

echo Datos de la base de datos de origen: $databaseName / $databaseUser / $databasePassword

targetName=$(sed -n "$extractDatabaseName" $2/app/config/parameters.php)
targetUser=$(sed -n "$extractDatabaseUser" $2/app/config/parameters.php)
targetPassword=$(sed -n "$extractDatabasePassword" $2/app/config/parameters.php)

echo Datos de la base de datos de destino: $targetName / $targetUser / $targetPassword

if [[ -z "$databaseName" ]] || [[ -z "$databaseUser" ]] || [[ -z "$databasePassword" ]] || [[ -z "$targetName" ]] || [[ -z "$targetUser" ]] || [[ -z "$targetPassword" ]]; then
  echo "Error: Algunas de las variables no han sido extraídas, comprueba los errores y los directorios."
  exit 1
fi

echo URL de la instancia origen: $3
echo URL de la instancia destino: $4

read -p "Pulsa enter para continuar o CTRL+C para cancelar"
echo  "Procediendo con el traspaso."

echo Extrayendo base de datos
rm -f temp.sql
mysqldump -u $databaseUser $databaseName -p$databasePassword > temp.sql
echo Borrando base de datos objetivo
mysql -u $targetUser $targetName -p$targetPassword -e "drop database $targetName; create database $targetName"
echo Insertando la base de datos origen en la objetivo
mysql -u $targetUser -D $targetName -p$targetPassword < temp.sql
rm -f temp.sql

echo borra ficheros contenido antiguo
rm -Rf $2/*
echo copia ficheros
cp -r $1/* $2/

echo Configurando base de datos objetivo con los datos originales

sed -i "s/'database_name' => '\(.*\)'/'database_name' => '$targetName'/g" $2/app/config/parameters.php
sed -i "s/'database_user' => '\(.*\)'/'database_user' => '$targetUser'/g" $2/app/config/parameters.php
sed -i "s/'database_password' => '\(.*\)'/'database_password' => '$targetPassword'/g" $2/app/config/parameters.php

echo Configurando la nueva URL en la instancia destino

mysql -u $targetUser $targetName -p$targetPassword -e "update prstshp_configuration set value = '$4' where NAME IN ('PS_SHOP_DOMAIN', 'PS_SHOP_DOMAIN_SSL')"
mysql -u $targetUser $targetName -p$targetPassword -e "UPDATE prstshp_shop_url SET domain = '$4', domain_ssl = '$4' WHERE id_shop_url = 1;"
sed -i s/\\^$3\$/^$4$/g $2/.htaccess

echo Terminado.

The implementation could undoubtedly be improved—using a more suitable language or more advanced tools—but this is the solution I came up with given the constraints of an environment lacking many essential utilities and commands. If you found it helpful, feel free to share your experience in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.