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

./website-copy:

#!/bin/bash
#
# script to copy websites (inkl. database) 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
#
#
# per-website settings
# --> import this from separate file later

# local settings
WEBSITE_LOCAL_PATH="/home/caydin/Scripte/website-copy/revamp_test"
WEBSITE_LOCAL_USER="www-data"

OVERRIDE_PATH="/home/caydin/Scripte/website-copy/revamp_test-ovrd"

DB_LOCAL_NAME="joomla_revamp_test"
DB_LOCAL_USER="joomlauser"
DB_LOCAL_PASS="secret1.x"

# remote settings
WEBSITE_REMOTE_SERVER="rochen.revamp-it.ch"

# (always include a trailing slash here!)
WEBSITE_REMOTE_PATH="/var/www/revamp/jodir_ut/"

DB_SERVER="rochen.revamp-it.ch"
DB_NAME="joomla_revamp_ut"

# DB LOGIN ??
# --> it is best to have the database login information
# in an sql config file, so that they don't appear on the
# process list !!

ME=$(whoami)

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

# copy files from server using rsync
echo "copying files..."
# rsync opts
# -r recursive
# -l copy symlinks as symlinks
# -p preserve permissions
sudo -u "$WEBSITE_LOCAL_USER". \
    rsync -rlpv "$ME"@"$WEBSITE_REMOTE_SERVER":"$WEBSITE_REMOTE_PATH" \
    "$WEBSITE_LOCAL_PATH"
check_exit $?

# --> 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.g
z
# works!
#
ssh "$WEBSITE_REMOTE_SERVER" "mysqldump $DB_NAME | gzip" > mysql "$DB_LOCAL_NAME"