Script Webseite Duplizieren / Testseite

Aus revampedia

Es soll ein Script erstellt werden um Webseiten inkl. Datenbank von produktiv- zu testserver zu kopieren, also eine testseite zu erstellen.

Geplantes Set-up:

Produktivserver
- webserver
- db-server

Testserver
- web- und db-server

Recherche

Google: "mysqldump over internet"

Script

WIP / in development / untested

./website-copy:

#!/bin/bash
#
# script to copy websites incl. database (mysql for now) from production
# to test server / creating a test-site
#
# using a config file per-website
#
# using rsync
# mysqldump, mysql/mysqladmin (?)
#
# cem, 2016-08-29
#
#
#
# program workflow
#
# 1. read settings from config file
#
# 2. copy files using rsync
#
# 3. copy database
#   3.1 create new db
#   3.2 copy from remote
#
#
#
# $1 config file

# check command line argument for config file

if [ $# -lt 1 ]; then
    echo "error: config file missing"
    echo "usage: $0 <config-file>"
    exit 1
fi

if [ ! -f "$1" ]; then
    echo "error: file not found: $1"
    exit 1
fi

# import settings
. "$1"

ME=$(whoami)

check_exit() {
    # $1: exit code, $?
    if [ $1 -ne 0 ]; then
        echo "oops, something went wrong, exiting."
        exit 1
    else
        echo "done."
    fi
}

# copy files from server using rsync
# rsync opts
# -r recursive
# -l copy symlinks as symlinks
# -p preserve permissions
#
# remote path includes always a trailing slash,
# by `parameter substitution`, e.g.: "${var%/}"/
# stripping if present and readding to avoid doubles
#
echo "copying files..."
sudo -u "$WEBSITE_LOCAL_USER". \
    rsync -rlpv "$ME"@"$WEBSITE_REMOTE_SERVER":"${WEBSITE_REMOTE_PATH%/}"/ \
    "$WEBSITE_LOCAL_PATH"
check_exit $?

# --> must create local db first ????
# create new db on local instance
# mysql > 5.7.6
# for mysql below this use something like:
# GRANT ALL ON `database`.* TO 'user'@'localhost' IDENTIFIED BY 'password';
# - http://stackoverflow.com/questions/13357760/mysql-create-user-if-not-exists#
echo "creating local database..."
mysql <<EOF
  CREATE DATABASE IF NOT EXISTS '$DB_LOCAL_NAME';
  CREATE USER IF NOT EXISTS '$DB_LOCAL_USER'@'localhost'
    IDENTIFIED BY '$DB_LOCAL_PASS';
  GRANT ALL PRIVILEGES ON $DB_LOCAL_NAME.* TO '$DB_LOCAL_USER'@'localhost';
  FLUSH PRIVILEGES;
EOF
check_exit $?

# ref: http://tltech.com/info/mysql-via-ssh/
# mysql destdb < <(ssh sourceserver.example.com "mysqldump sourcedb")
# sudo ??
# or ssh sourceserver.example.com "mysqldump sourcedb" | mysql destdb
#
# tested: ssh rochen "mysqldump joomla_revamp_ut| gzip" > joomla_revamp_ut.dump.gz
# works!
echo "copying database from remote..."
ssh "$WEBSITE_REMOTE_SERVER" "mysqldump $DB_NAME | gzip -c" \
    | gzip -d > mysql "$DB_LOCAL_NAME"
check_exit $?

# copying files from override directory
echo "copying files from override directory..."
sudo -u "$WEBSITE_LOCAL_USER". \
    cp -dpr "${OVERRIDE_PATH%/}"/. "$WEBSITE_LOCAL_PATH"
check_exit $?

echo "finished!"