Wikiraum

Benutzer-Werkzeuge

Webseiten-Werkzeuge


Eine Webseite inklusive zugehöriger MySQL-Datenbank kopieren

Ich nutze das Skript, um eine Parallelinstallation von Joomla! zur Verfügung zu stellen, auf der dann z.B. Plugins vor dem Produktiveinsatz erprobt werden. Mit diesem Skript kann man die zerschossene Kopie dann immer wieder leicht resetten.

website_copy.sh
#!/bin/bash
 
# directories
TARGET_DIR="/path/to/target"
SOURCE_DIR="/path/to/source"
BACKUP_DIR="/path/to/intermediate"
 
# files
CONFIG_FILE="/path/to/config/file/"
DUMP_FILE="/path/to/dump/file"
 
# MySQL Source
MYSQL_SRC_DB="source_db"
MYSQL_SRC_USER="source_db_user"
MYSQL_SRC_PASS="source_db_pass"
 
# MySQL Target
MYSQL_TRG_DB="target_db"
MYSQL_TRG_USER="target_db_user"
MYSQL_TRG_PASS="target_db_pass"
 
# Copy files 
 
if [ ! -d "$BACKUP_DIR" ]; then
 
  mkdir $BACKUP_DIR
 
fi
 
if [ -d "$BACKUP_DIR" ]; then
 
	echo "Ok. Backupdirectory exists."
  	cd $BACKUP_DIR
  	rm -rf *
  	echo "Backupdirectory has been emptied!"
 
  	if [ -f "$CONFIG_FILE" ]; then
 
		cp $CONFIG_FILE $BACKUP_DIR
 
  	else
 
		echo "Fatal. Configuration file not found!"
		exit 0
 
  	fi
 
	if [ -d "$TARGET_DIR" ]; then
 
		cd $TARGET_DIR
		echo "Deleting old files ..."
		rm -rf *
		echo "Old files deleted!"
 
	        if [ -d "$SOURCE_DIR" ]; then
 
			echo "Ok. Sourcedirectory found! - Copying files ..."
			cd $SOURCE_DIR
			cp -rvf * $TARGET_DIR
			echo "Copying configuration ..."
			cd $BACKUP_DIR
			cp -f * $TARGET_DIR
			echo "Finished copying. Now dumping database ..."
			mysqldump -u $MYSQL_SRC_USER -p$MYSQL_SRC_PASS $MYSQL_SRC_DB > $DUMP_FILE
			echo "Ok. Database has been dumped!"
			echo "Building new database ..."
			mysql -u $MYSQL_TRG_USER -p$MYSQL_TRG_PASS $MYSQL_TRG_DB -e "show tables" | grep -v Tables_in | grep -v "+" | gawk '{print "drop table " $1 ";"}' | mysql -u $MYSQL_TRG_USER -p$MYSQL_TRG_PASS $MYSQL_TRG_DB
			echo "Old tables are deleted. Now recreation with new values in progress."
			mysql -u $MYSQL_TRG_USER -p$MYSQL_TRG_PASS $MYSQL_TRG_DB < $DUMP_FILE
			echo "Adjusting permissions ..."
			chown -R www-data:www-data $TARGET_DIR
			echo "All finished!"
 
		else
 
			echo "Fatal. Source directory not found!"
        	        exit 0
 
		fi
	else
 
		echo "Fatal. Target directory not found!"
		exit 0
 
	fi
 
fi