Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## 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
70 changes: 33 additions & 37 deletions php_redis_migrate_sessions
Original file line number Diff line number Diff line change
Expand Up @@ -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