Friday, October 07, 2005

BU JShop via Script

Drop the following code in a PHP file, upload it to the top level of a JShop running on Linux, and execute it via the browser. It will create a tarball including the JShop files and MySQL data for downloading:

//Code to BU a JShop in place - all shop files and MySQL data
//Drop this file into the top level of a JShop installation on Linux and call
//it via the web
//This file itself will be deleted at the end as a security measure
//The names of the backup file to download will be output to your browser
echo "

Script to BU a JShop in place


//Let the script run as long as necessary

//Get the info about the DB, file system, etc.
include ("static/config.php");
global $jssShopFileSystem, $jssStoreWebDirHTTP, $databaseUsername,
$databasePassword, $databaseName, $databaseHost;

//Some variables
$date = date ("Ymd");
$name = str_replace("\\", "", str_replace("/", "", str_replace("http://",
"", $jssStoreWebDirHTTP)));
$siteTarball = "{$jssShopFileSystem}{$date}{$name}.tar.gz";
$sqlDumpName = "{$jssShopFileSystem}{$date}{$name}.sql";
$thisScriptFile = $jssShopFileSystem.substr($_SERVER['PHP_SELF'], 1);

//Do a MySQL dump
// "Q" option quotes the column names - very important in case you have a column name that
// is also a keyword
// "n" Do NOT add a "create DB" command - since a lot of our clients have the same name
// for the JSS DB, we don't want to import this on our local setup and overwrite existing
// data
// "no-create-info" do not create the tables. This is done with the first mysqldump. We do
// the creation separate from the dumping so that the tables that we don't want the data
// for (big, unneeded tables for local development) still have their tables created.
// "--add-locks" Surround each table dump with LOCK TABLES and UNLOCK TABLES statements.
// This results in faster inserts when the dump file is reloaded.

// "--create-options" - Include all MySQL-specific table options in the CREATE TABLE statements.
// Cristóbal - In some sites not works!!!

// "K" - For each table, surround the INSERT statements with
// /*!40000 ALTER TABLE tbl_name DISABLE KEYS */; and
// /*!40000 ALTER TABLE tbl_name ENABLE KEYS */; statements.
// This makes loading the dump file faster because the indexes are created after all rows
// are inserted. This option is effective for MyISAM tables only.
// "e" Extended Insert: Use multiple-row INSERT syntax that include several VALUES lists. This
// results in a smaller dump file and speeds up inserts when the file is reloaded.
// "l" Lock all tables before starting the dump. The tables are locked with READ LOCAL to allow
// concurrent inserts in the case of MyISAM tables. For transactional tables such as InnoDB and
// BDB, --single-transaction is a much better option, because it does not need to lock the tables
// at all.
// "q" Quick: This option is useful for dumping large tables. It forces mysqldump to retrieve
// rows for a table from the server a row at a time rather than retrieving the entire row set
// and buffering it in memory before writing it out.

// "--set-charset" Add SET NAMES default_character_set to the output. This option is enabled by
// default. To suppress the SET NAMES statement, use --skip-set-charset.
// Cristóbal - In some sites not works!!!

// "u" is user name, -p is password (no space between the command and the password - see docs), and
// "h" is host.

//runCmd ("mysqldump -Q -n --add-locks --create-options -K -e -l -q --set-charset -u $databaseUsername -p$databasePassword -h "."$databaseHost $databaseName > $sqlDumpName");
runCmd ("mysqldump -d -Q -n --add-locks -K -e -l -q -u $databaseUsername -p$databasePassword -h "."$databaseHost $databaseName > $sqlDumpName");
runCmd ("mysqldump -Q -n --no-create-info --add-locks -K -e -l -q -u $databaseUsername -p$databasePassword -h "."$databaseHost $databaseName --ignore-table=$databaseName.jss_logs --ignore-table=$databaseName.jss_carts --ignore-table=$databaseName.jss_carts_contents --ignore-table=$databaseName.jss_logs --ignore-table=$databaseName.amex_clickthroughs >> $sqlDumpName");
//runCmd ("mysqldump -Q -n --add-locks -K -e -l -q -u $databaseUsername -p$databasePassword -h "."$databaseHost $databaseName > $sqlDumpName");
//Make a tarball of the site files, including the sql dump, excluding the tarball itself
runCmd ("tar czvpf $siteTarball $sqlDumpName");
//Remove the MySQL dump
runCmd ("rm $sqlDumpName");
//Remove this script for security reasons
runCmd ("rm $thisScriptFile");

echo "
echo "

You can now download '$siteTarball', which includes the MySQL dump:

echo "

To save space, be sure to delete the file ".
"from the server after downloading them!!


function runCmd ($cmd) {
echo "------------
echo "About to exec: $cmd
//info on how to get the output of a command comes from the comments on
exec("$cmd 2>&1", $output);
echo "Output is: ";
//Use print_r in case there are multiple lines in the output. Can happen
//if you e.g. add the "v" (verbose) argument to tar
echo "