Script Webseite Duplizieren / Testseite: Unterschied zwischen den Versionen

Aus revampedia
Zeile 25: Zeile 25:
 
=== Script ===
 
=== Script ===
  
'''''WIP / in development'''''
+
'''''WIP / in development / untested'''''
  
 
<code>./website-copy</code>:
 
<code>./website-copy</code>:
Zeile 31: Zeile 31:
 
#!/bin/bash
 
#!/bin/bash
 
#
 
#
# script to copy websites (inkl. database) from production
+
# script to copy websites incl. database (mysql for now) from production
 
# to test server / creating a test-site
 
# to test server / creating a test-site
 
#
 
#
Zeile 54: Zeile 54:
 
#
 
#
 
#
 
#
# per-website settings
+
#
# --> import this from separate file later
+
# $1 config file
 
 
# 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"
+
# check command line argument for config file
DB_LOCAL_USER="joomlauser"
 
DB_LOCAL_PASS="secret1.x"
 
  
# remote settings
+
if [ $# -lt 1 ]; then
WEBSITE_REMOTE_SERVER="rochen.revamp-it.ch"
+
    echo "error: config file missing"
 +
    echo "usage: $0 <config-file>"
 +
    exit 1
 +
fi
  
# (always include a trailing slash here!)
+
if [ ! -f "$1" ]; then
WEBSITE_REMOTE_PATH="/var/www/revamp/jodir_ut/"
+
    echo "error: file not found: $1"
 +
    exit 1
 +
fi
  
DB_SERVER="rochen.revamp-it.ch"
+
# import settings
DB_NAME="joomla_revamp_ut"
+
. "$1"
 
 
# 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)
 
ME=$(whoami)
Zeile 88: Zeile 80:
 
         echo "oops, something went wrong, exiting."
 
         echo "oops, something went wrong, exiting."
 
         exit 1
 
         exit 1
 +
    else
 +
        echo "done."
 
     fi
 
     fi
 +
}
  
 
# copy files from server using rsync
 
# copy files from server using rsync
echo "copying files..."
 
 
# rsync opts
 
# rsync opts
 
# -r recursive
 
# -r recursive
 
# -l copy symlinks as symlinks
 
# -l copy symlinks as symlinks
 
# -p preserve permissions
 
# -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". \
 
sudo -u "$WEBSITE_LOCAL_USER". \
     rsync -rlpv "$ME"@"$WEBSITE_REMOTE_SERVER":"$WEBSITE_REMOTE_PATH" \
+
     rsync -rlpv "$ME"@"$WEBSITE_REMOTE_SERVER":"${WEBSITE_REMOTE_PATH%/}"/ \
 
     "$WEBSITE_LOCAL_PATH"
 
     "$WEBSITE_LOCAL_PATH"
 
check_exit $?
 
check_exit $?
  
# --> create local db first ????
+
# --> must create local db first ????
 
# create new db on local instance
 
# create new db on local instance
 
# mysql > 5.7.6
 
# mysql > 5.7.6
Zeile 107: Zeile 107:
 
# GRANT ALL ON `database`.* TO 'user'@'localhost' IDENTIFIED BY 'password';
 
# GRANT ALL ON `database`.* TO 'user'@'localhost' IDENTIFIED BY 'password';
 
# - http://stackoverflow.com/questions/13357760/mysql-create-user-if-not-exists#
 
# - http://stackoverflow.com/questions/13357760/mysql-create-user-if-not-exists#
#echo "creating local database..."
+
echo "creating local database..."
 
mysql <<EOF
 
mysql <<EOF
 
   CREATE DATABASE IF NOT EXISTS '$DB_LOCAL_NAME';
 
   CREATE DATABASE IF NOT EXISTS '$DB_LOCAL_NAME';
Zeile 116: Zeile 116:
 
EOF
 
EOF
 
check_exit $?
 
check_exit $?
 +
 
# ref: http://tltech.com/info/mysql-via-ssh/
 
# ref: http://tltech.com/info/mysql-via-ssh/
 
# mysql destdb < <(ssh sourceserver.example.com "mysqldump sourcedb")
 
# mysql destdb < <(ssh sourceserver.example.com "mysqldump sourcedb")
Zeile 121: Zeile 122:
 
# or ssh sourceserver.example.com "mysqldump sourcedb" | mysql destdb
 
# or ssh sourceserver.example.com "mysqldump sourcedb" | mysql destdb
 
#
 
#
# tested: ssh rochen "mysqldump joomla_revamp_ut| gzip" > joomla_revamp_ut.dump.g
+
# tested: ssh rochen "mysqldump joomla_revamp_ut| gzip" > joomla_revamp_ut.dump.gz
z
 
 
# works!
 
# works!
#
+
echo "copying database from remote..."
ssh "$WEBSITE_REMOTE_SERVER" "mysqldump $DB_NAME | gzip" > mysql "$DB_LOCAL_NAME"
+
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!"
  
 
</source>
 
</source>

Version vom 1. September 2016, 18:55 Uhr

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!"