Last active
July 30, 2021 07:22
-
-
Save hcoohb/75849c38fe675cb01dfb8e48119e8c60 to your computer and use it in GitHub Desktop.
Wordpress - Softaculous backup to local docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.8" | |
services: | |
db: | |
image: mariadb | |
container_name: ${SUBFOLDER}_db | |
restart: always | |
volumes: | |
- ./database:/var/lib/mysql | |
- ./database/initdb.d:/docker-entrypoint-initdb.d | |
environment: | |
MYSQL_RANDOM_ROOT_PASSWORD: "1" | |
MYSQL_DATABASE: $MYSQL_DB | |
MYSQL_USER: $MYSQL_USER | |
MYSQL_PASSWORD: $MYSQL_PASSWORD | |
networks: | |
- wp | |
adminer: | |
image: adminer | |
container_name: ${SUBFOLDER}_adminer | |
restart: always | |
depends_on: | |
- db | |
networks: | |
- traefik_proxy | |
- wp | |
labels: | |
- traefik.enable=true | |
- traefik.docker.network=traefik_proxy | |
## Routers | |
- traefik.http.routers.${SUBFOLDER}_adminer_rtr_web.rule=PathPrefix(`/${SUBFOLDER}_db`) | |
- traefik.http.routers.${SUBFOLDER}_adminer_rtr_web.entrypoints=web | |
- traefik.http.routers.${SUBFOLDER}_adminer_rtr.rule=PathPrefix(`/${SUBFOLDER}_db`) | |
- traefik.http.routers.${SUBFOLDER}_adminer_rtr.entrypoints=websecure | |
- traefik.http.routers.${SUBFOLDER}_adminer_rtr.tls=true | |
## Middlewares | |
- traefik.http.routers.${SUBFOLDER}_adminer_rtr.middlewares=compress_mid | |
## Services | |
- traefik.http.services.${SUBFOLDER}_adminer_svc.loadbalancer.server.port=8080 | |
whoami: | |
# A container that exposes an API to show its IP address | |
image: containous/whoami | |
container_name: ${SUBFOLDER}_whoami | |
networks: | |
- traefik_proxy | |
labels: | |
- traefik.enable=true | |
## Routers | |
- traefik.http.routers.${SUBFOLDER}_whoami_rtr_web.rule=PathPrefix(`/${SUBFOLDER}_whoami`) | |
- traefik.http.routers.${SUBFOLDER}_whoami_rtr_web.entrypoints=web | |
- traefik.http.routers.${SUBFOLDER}_whoami_rtr.rule=PathPrefix(`/${SUBFOLDER}_whoami`) | |
- traefik.http.routers.${SUBFOLDER}_whoami_rtr.entrypoints=websecure | |
- traefik.http.routers.${SUBFOLDER}_whoami_rtr.tls=true | |
wordpress: | |
image: wordpress:5.7.2-php7.4 | |
container_name: ${SUBFOLDER}_wp | |
restart: always | |
depends_on: | |
- db | |
environment: | |
WORDPRESS_DB_HOST: db | |
WORDPRESS_DB_USER: $MYSQL_USER | |
WORDPRESS_DB_NAME: $MYSQL_USER | |
WORDPRESS_DB_PASSWORD: $MYSQL_PASSWORD | |
WORDPRESS_TABLE_PREFIX: $WP_TABLE_PREFIX | |
volumes: | |
- ./wordpress:/var/www/html | |
- ./wp-content:/var/www/html/${SUBFOLDER}/wp/wp-content | |
working_dir: /var/www/html/${SUBFOLDER}/wp # allows us to have the same structure as live site | |
networks: | |
- traefik_proxy | |
- wp | |
labels: | |
- traefik.enable=true | |
- traefik.docker.network=traefik_proxy | |
## Routers | |
- traefik.http.routers.${SUBFOLDER}_wp_rtr_web.rule= PathPrefix(`/${SUBFOLDER}`) | |
- traefik.http.routers.${SUBFOLDER}_wp_rtr_web.entrypoints=web | |
- traefik.http.routers.${SUBFOLDER}_wp_rtr.rule=PathPrefix(`/${SUBFOLDER}`) | |
- traefik.http.routers.${SUBFOLDER}_wp_rtr.entrypoints=websecure | |
- traefik.http.routers.${SUBFOLDER}_wp_rtr.tls=true | |
wp-cli: | |
image: wordpress:cli-php7.4 | |
container_name: ${SUBFOLDER}_wp_cli | |
restart: on-failure # exec only on init | |
depends_on: | |
- db | |
- wordpress | |
networks: | |
- wp | |
volumes: | |
- ./wordpress:/var/www/html | |
- ./wp-content:/var/www/html/${SUBFOLDER}/wp/wp-content | |
working_dir: /var/www/html/${SUBFOLDER}/wp # allows uss to have the same structure as live site | |
command: wp search-replace 'https://${LIVE_DOMAIN}' 'https://${HOST_LAN_IP}/${SUBFOLDER}' | |
environment: | |
WORDPRESS_DB_HOST: db | |
WORDPRESS_DB_USER: $MYSQL_USER | |
WORDPRESS_DB_NAME: $MYSQL_USER | |
WORDPRESS_DB_PASSWORD: $MYSQL_PASSWORD | |
WORDPRESS_TABLE_PREFIX: $WP_TABLE_PREFIX | |
networks: | |
wp: | |
traefik_proxy: | |
external: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -z "$1" ] | |
then | |
echo "No argument supplied" | |
echo "Execute this script as root to extract all infos from softaculous backup." | |
echo "Usage: \`$0 <BACKUP_FNAME>\`" | |
echo "You can then use \`docker-compose up -d\`" | |
exit 1 | |
fi | |
if [ $(id -u) != 0 ]; then | |
echo "You're not root. Restarting as root" | |
# elevate script privileges | |
exec sudo "$0" "$@" | |
fi | |
fname=$(basename -- "$1") | |
fname_no_ext="${fname%.*.*}" #remove .tar.gz | |
echo "Extracting website from backup '$1'" | |
# echo $fname_no_ext | |
tar -zxf $1 $fname_no_ext | |
dbprefix=$(sed -n 's/.*dbprefix\";s:[[:digit:]]:\"\([^\"]*\)\".*/\1/p' $fname_no_ext) | |
db=$(sed -n 's/.*softdb\";s:[[:digit:]]*:\"\([^\"]*\)\".*/\1/p' $fname_no_ext) | |
dbuser=$(sed -n 's/.*softdbuser\";s:[[:digit:]]*:\"\([^\"]*\)\".*/\1/p' $fname_no_ext) | |
dbpass=$(sed -n 's/.*softdbpass\";s:[[:digit:]]*:\"\([^\"]*\)\".*/\1/p' $fname_no_ext) | |
wp_domain=$(sed -n 's/.*softdomain\";s:[[:digit:]]*:\"\([^\"]*\)\".*/\1/p' $fname_no_ext) | |
wp_ver=$(sed -n 's/.*\"ver\";s:[[:digit:]]*:\"\([^\"]*\)\".*/\1/p' $fname_no_ext) | |
rm $fname_no_ext | |
# create the env file | |
env_fname='.env' | |
if [[ ! -e $env_fname ]]; then | |
echo "" > $env_fname | |
fi | |
# replace the value read from backup | |
folder_name=${PWD##*/} | |
lan_ip=$(ip -o route get "8.8.8.8" 2>/dev/null | sed -e 's/^.* src \([^ ]*\) .*$/\1/' ) | |
cat $env_fname | | |
sed -n -e '/^MYSQL_DB=/!p' -e '$a MYSQL_DB='$db | | |
sed -n -e '/^MYSQL_USER=/!p' -e '$a MYSQL_USER='$dbuser | | |
sed -n -e '/^MYSQL_PASSWORD=/!p' -e '$a MYSQL_PASSWORD='$dbpass | | |
sed -n -e '/^WP_TABLE_PREFIX=/!p' -e '$a WP_TABLE_PREFIX='$dbprefix | | |
sed -n -e '/^LIVE_DOMAIN=/!p' -e '$a LIVE_DOMAIN='$wp_domain | | |
sed -n -e '/^SUBFOLDER=/!p' -e '$a SUBFOLDER='$folder_name | | |
sed -n -e '/^HOST_LAN_IP=/!p' -e '$a HOST_LAN_IP='$lan_ip \ | |
> $env_fname'-tmp' | |
mv -f $env_fname'-tmp' $env_fname | |
echo "Env file '$env_fname' is:" | |
cat $env_fname | |
chown user:user $env_fname | |
# delete current database and replace | |
echo "Extracting database: " | |
rm -rf ./database | |
mkdir -p ./database/initdb.d | |
tar -zxvf $1 softsql.sql | |
mv softsql.sql ./database/initdb.d/ | |
chown user:user ./database/initdb.d/softsql.sql | |
# need to delete current wp-content | |
rm -rf ./wp-content | |
echo "Extracting wp-content folder..." | |
tar -zxf $1 wp-content | |
chown -R http:http ./wp-content/ | |
# Recreate .htaccess and index.php in case it got pwned | |
rm -rf ./wordpress | |
mkdir -p ./wordpress/$folder_name/wp | |
cat > ./wordpress/$folder_name/.htaccess <<- "EOF" | |
# BEGIN WordPress | |
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are | |
# dynamically generated, and should only be modified via WordPress filters. | |
# Any changes to the directives between these markers will be overwritten. | |
<IfModule mod_rewrite.c> | |
RewriteEngine On | |
RewriteBase /SUBFOLDER/wp/ | |
RewriteRule ^index\.php$ - [L] | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule . /SUBFOLDER/wp/index.php [L] | |
</IfModule> | |
# END WordPress | |
php_value upload_max_filesize 24M | |
php_value post_max_size 24M | |
php_value max_execution_time 300 | |
php_value max_input_time 300 | |
EOF | |
# replace the actual subfolder name: | |
sed -i "s|SUBFOLDER|$folder_name|g" ./wordpress/$folder_name/.htaccess | |
cat > ./wordpress/$folder_name/index.php <<- "EOF" | |
<?php | |
/** | |
* Front to the WordPress application. This file doesn't do anything, but loads | |
* wp-blog-header.php which does and tells WordPress to load the theme. | |
* | |
* @package WordPress | |
*/ | |
/** | |
* Tells WordPress to load the WordPress theme and output it. | |
* | |
* @var bool | |
*/ | |
define( 'WP_USE_THEMES', true ); | |
/** Loads the WordPress Environment and Template */ | |
require __DIR__ . '/wp/wp-blog-header.php'; | |
EOF | |
chown -R http:http ./wordpress/ | |
# read and check wp-version! | |
wp_ver_docker=$(sed -n 's/.*wordpress:\([^-cli]*\)-php.*/\1/p' docker-compose.yaml) | |
if [ $wp_ver_docker != $wp_ver ]; then | |
echo "" | |
echo "WARNING!!!! Wordpress backup version is different from the docker-compose!" | |
echo "$wp_ver: backup version" | |
echo "$wp_ver_docker: docker version" | |
fi | |
echo "" | |
echo "Ready to execute \`docker-compose up -d\`" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment