forked from renasboy/php-redis-migrate-sessions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_redis_migrate_sessions
More file actions
executable file
·72 lines (58 loc) · 2.34 KB
/
php_redis_migrate_sessions
File metadata and controls
executable file
·72 lines (58 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
#---
## @Synopsis Migrate php sessions from disk to redis using mass insertion
## @Copyright Copyleft 2013 renasboy <renasboy@gmail.com>
## @License GPL v3
##
## Reads session stored in $php_session_dir and transfer the contents to
## redis database using redis-cli pipe mode and redis mass insertion protocol.
## Sessions are stored with the $php_session_prefix as prefix.
## This script calls itself providing the session file location
##
##
#---
# 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
function echoRedisProtocolInsertSession() {
local sessionFileName="$1"
if test -n "$sessionFileName" -a -f "$sessionFileName"; then
# 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" # *<args>
echo -n -e "\$3\r\n" # $<len arg0>
echo -n -e "SET\r\n" # <arg0> (command)
echo -n -e "\$${#session_id}\r\n" # $<len arg1>
echo -n -e "$session_id\r\n" # <arg1=key>
echo -n -e "\$$(echo -n -e "$(escapeValue "$sessionFileName")"| wc -c)\r\n" # $<len arg2>
echo -n -e "$(escapeValue "$sessionFileName")\r\n" # <arg2=value>
fi
}
function escapeValue() {
local sessionFileName="$1"
echo -n -e "$(sed 's/\\/\\\\/g' $sessionFileName | sed 's/\x00/\\\\x00/g')"
}
export -f echoRedisProtocolInsertSession
export -f escapeValue
export php_session_prefix
# trap method to cleanup on exit
trap cleanexit EXIT
cleanexit () {
if test -d "$tmp_dir"; then
rm -rf "$tmp_dir"
fi
}
# create temp dir where data is stored
tmp_dir=`mktemp -d`
# first cleanup all zero byte sessions
echo "removing empty session files"
find $php_session_dir -type f -size 0 -name "sess_*" -exec rm -f {} \; 2>/dev/null
echo "generating the data file with redis protocol for mass insertion"
#http://redis.io/topics/mass-insert
find $php_session_dir -type f -name "sess_*" -exec bash -c 'echoRedisProtocolInsertSession "$0"' {} \; > $tmp_dir/data.txt 2>/dev/null
echo "loading data into redis using the redis-cli pipe mode"
cat $tmp_dir/data.txt | redis-cli --pipe
# exit gracefully
exit 0