From 08f15f3b0feb45a428300a877ab08ea76eef1cf0 Mon Sep 17 00:00:00 2001 From: Otheus Date: Mon, 9 Dec 2019 22:43:14 +0100 Subject: [PATCH 1/3] Complete refactor 1. Provide a file or directory as a command argument to convert to redis. 2. Does not make a separate exec call for each file to convert. Everything is done directly. 3. However, it does make an exec call to redis-cli for each session, as a way of more easily catching and identifying problem sessions. 4. DRYRUN and VERBOSE environment variables can be set accordingly --- php_redis_migrate_sessions | 70 ++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/php_redis_migrate_sessions b/php_redis_migrate_sessions index 478df86..a4ffc09 100755 --- a/php_redis_migrate_sessions +++ b/php_redis_migrate_sessions @@ -13,46 +13,42 @@ ## #--- -# declaring the php session directory location -# basically this is your session.save_path -php_session_dir=/var/lib/php5 - # this is the php session prefix inside redis -php_session_prefix=PHPREDIS_SESSION +: ${php_session_prefix:=PHPREDIS_SESSION} +: ${VERBOSE:=} +: ${DRYRUN:=} -# this is what happens during the find loop underneath -# when the script is called with php session file as parameter -# it outputs the redis protocol for mass insertion -if test -n "$1" -a -f "$1"; then +# function to generate the data file with redis protocol for +# for mass insertion http://redis.io/topics/mass-insert +session_2_redis_pipe() { # generate the session id cause we need to get the length of it session_id=$php_session_prefix:$(basename ${1/sess_/}) - echo -n -e "*3\r\n\$3\r\nSET\r\n\$${#session_id}\r\n$session_id\r\n\$$(stat -c"%s" $1)\r\n$(sed 's/\\/\\\\/g' $1)\r\n" - exit 0 -fi - -# trap method to cleanup on exit -trap cleanexit EXIT - -cleanexit () { - if test -d "$tmp_dir"; then - rm -rf "$tmp_dir" - fi + echo -n -e "*3\r\n\$3\r\nSET\r\n\$${#session_id}\r\n$session_id\r\n\$$(stat -c"%s" $1)\r\n" + #sed 's/\\/\\\\/g' $1 + cat "$1" + echo -n -e "\r\n" } -# create temp dir where data is stored -tmp_dir=`mktemp -d` - -# first cleanup all zero byte sessions -find $php_session_dir -size 0 -exec rm -f {} \; - -# then generate the data file with redis protocol for -# for mass insertion http://redis.io/topics/mass-insert -# during the find loop we call ourselves with a file as first param -# this is basically to have everything in a single script -find $php_session_dir -type f -exec $0 {} \; > $tmp_dir/data.txt - -# load data into redis using the redis-cli pipe mode -cat $tmp_dir/data.txt | redis-cli --pipe - -# exit gracefully -exit 0 +# Directory found, assume it's where the sessions are stored. +set -o pipefail +errors=0 + +find "$1" -type f \! -size 0 | + while read sessfile ; do + [[ $VERBOSE ]] && echo -n "Processing ${sessfile##*/}..." ${DRYRUN:+(dry-run)...} + if [[ $DRYRUN ]]; then + echo + session_2_redis_pipe "$sessfile" + else + session_2_redis_pipe "$sessfile" | redis-cli --pipe &>/dev/null + fi + if [[ $? != 0 ]]; then + errors=1 + if [[ $VERBOSE ]] ; + then echo "Failed"; + else echo >&2 "Failed to process $1" + fi + fi + [[ $VERBOSE ]] && echo "Success!" ${DRYRUN:+(dry-run)...} +done +exit $errors From 9e1aceaf75b3bcc1271b9463a6d13ebdc0ebee65 Mon Sep 17 00:00:00 2001 From: Otheus Date: Mon, 9 Dec 2019 22:44:32 +0100 Subject: [PATCH 2/3] Create CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d686512 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +== Changelog : php-redis-migrate-sessions == + +* 2013-02-15 *cavallari* Initial version From dd5fd4ee67ec5c4d76a52cf1c5ba1b42f135ce04 Mon Sep 17 00:00:00 2001 From: Otheus Date: Mon, 9 Dec 2019 22:46:57 +0100 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d686512..09e490e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ -== Changelog : php-redis-migrate-sessions == +## Changelog : php-redis-migrate-sessions +* 2019-12-09 *otheus* Refactor + * Command-line argument now dictates direction of php session directory or file (only one at present) + * Program is no longer re-entrant: find pipes to a while-loop which invokes redis-cli for each session + * DRYRUN environment variable, if set, will disable execution of redis-cli and only output what would have been sent to it + * VERBOSE tells the user of the progress + * These "options" are set on the command-line as follows `DRYRUN=1 VERBOSE=1 php-redis-migrate-sessions /var/lib/php/sessions` + * 2013-02-15 *cavallari* Initial version