From cb9fe1cb9114a4ec96beb5acb7527f2b8865c3aa Mon Sep 17 00:00:00 2001 From: Andrew Gillard Date: Mon, 20 Feb 2017 13:44:21 +0000 Subject: [PATCH] Rewrote script in PHP to fix various errors from redis. Also runs much faster. --- php_redis_migrate_sessions.php | 91 ++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 php_redis_migrate_sessions.php diff --git a/php_redis_migrate_sessions.php b/php_redis_migrate_sessions.php new file mode 100644 index 0000000..4d520df --- /dev/null +++ b/php_redis_migrate_sessions.php @@ -0,0 +1,91 @@ +#!/usr/bin/php +; + * heavily inspired by Bash script 2013 by renasboy + * @licence GPL v3 + * + * Reads sessions stored in $sessionDir and transfers the contents to redis + * database using redis-cli pipe mode and redis mass insertion protocol. + * Sessions are stored with $sessionPrefix as a prefix + */ + +// Where are the existing PHP sessions stored? +// Essentially your session.save_path php.ini value +$sessionDir = "/var/lib/php5/sessions"; + +// The PHP session prefix inside redis. +// The default is "PHPHREDIS_SESSION" +$sessionPrefix = "PHPREDIS_SESSION"; + +// Your redis password, if set. Leave blank if not needed. +$redisAuth = ""; + +// Host where redis is being run. Leave blank for localhost. +$redisHost = ""; + +// Port on which redis is running. Leave blank for the default. +$redisPort = ""; + +// ------ End of Config ------ // + +// Create a temporary file for storing the redis protocol messages +$tmpFile = tempnam(sys_get_temp_dir(), "prms"); +$fp = fopen($tmpFile, 'wb'); +echo "Opened temporary file '$tmpFile'...\r\n"; + +// And delete the temporary file when we're done +register_shutdown_function(function () use ($tmpFile) { + unlink($tmpFile); +}); + +// Process every file in the sessions directory... +echo "Processing each file...\r\n"; +foreach (new DirectoryIterator($sessionDir) as $file) { + // Skip the "dot" "files" and any empty sessions + if ($file->isDot()) { + continue; + } + if ($file->getSize() === 0) { + continue; + } + + // Prepare the session data for appending to the protocol file + $sessionId = $sessionPrefix . ":" . preg_replace('/^sess_/i', '', $file->getFilename()); + $sessionData = file_get_contents($file->getPathname()); + + // Incorporate the session data into the redis protocol + $protocol = sprintf( + "*3\r\n$3\r\nSET\r\n$%d\r\n%s\r\n$%d\r\n%s\r\n", + strlen($sessionId), + $sessionId, + strlen($sessionData), + $sessionData + ); + + // And write the protocol data out to our temp file + fwrite($fp, $protocol); +} + +// We've finished writing to the temporary file, so close it... +echo "Finished preparing protocol data...\r\n"; +fclose($fp); + +// ...and pass it on to redis-cli for transfer to the server +echo "Passing data to redis-cli...\r\n"; +$redisCmd = "cat $tmpFile | redis-cli --pipe"; +// Redis command line tool options +if ($redisHost != '') { + $redisCmd .= " -h " . escapeshellarg($redisHost); +} +if ($redisPort != '') { + $redisCmd .= " -p " . (int) $redisPort; +} +if ($redisAuth != '') { + $redisCmd .= " -a " . escapeshellarg($redisAuth); +} +echo "Executing `$redisCmd`...\r\n"; +passthru($redisCmd);