-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathphp_redis_migrate_sessions
More file actions
executable file
·58 lines (47 loc) · 1.8 KB
/
php_redis_migrate_sessions
File metadata and controls
executable file
·58 lines (47 loc) · 1.8 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
#!/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
# 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
# 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
}
# 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