Memcached备份、还原的的示例
注1: Memcached restart 后, 所有资料都会清空
注2: 这次遇到的主要是实验用的项目, 刚好要搬机器, 实验不想中断, 所以才用此方式
Memcached 备份、还原的方式
先看 Memcached 的限制条件:
Key 长度 250 bytes
预设 Value 1M(可透过 -I 2M 设定)
设定过期时间的话, 过期时间最多设定30天 (设 0 为永不过期的意思)
Ref: NewCommands - memcached - Make Me a Sandwich - Memcached
一般 Memcached 能提供的方式都是 Dump key, 然后自己再写程式捞出来, 做写入(ADD)的动作, 这边有比较简易的操作方式. (利用 memcached-tool + nc 来达成)
Memcached 备份、还原的步骤
假设要将 192.168.1.1 的 memcached data 备份到 localhost (192.168.1.1. 那台 memcached ip 不能跑 localhost)
代码如下 | |
wget https://raw.githubusercontent.com/memcached/memcached/master/scripts/memcached-tool chmod +x ./memcached-tool ./memcached-tool 192.168.1.1:11211 dump > data # 备份, data 内容大致如下: add 19bc2701c3898279jkds1jklc272b35f 0 1413178136 506 a:3:{s:4:"text";..................} |
注: 1413178136 # timestamp, 若是资料过期, 汇入后找不到
代码如下 | |
nc localhost 11211 < data # 还原 # 验证 ./memcached-tool localhost:11211 dump > local-data wc -l local-data wc -l data |
看看上述两个资料笔数是否正确
上述参考自此篇: memcache dump and load
PHP 版 Memcached backup / restore
另外有 PHP 的 Memcached backup / restore 程式, 可以参考看看(注: 我没用过)
下述摘录自此篇: Meabed/memcached-php-backup-restore - Save / Restore data from Memcache to File
php m.php -h 127.0.0.1 -p 112112 -op restore
-h : Memcache Host address ( default is 127.0.0.1 )
-p : Memcache Port ( default is 11211 )
-p : Operation is required !! ( available options is : restore , backup )
php m.php -h 127.0.0.1 -p 112112 -op backup
php m.php -h 127.0.0.1 -p 112112 -op restore
其它 Memcached 相关工具
Memcached 有其它方便使用的工具, 可以尝试看看.
sudo apt-get install libmemcached-tools # 下述取自: Welcome to the libmemcached documentation
memcapable - Checking a Memcached server capibilities and compatibility
memcat - "cat" data from a server
memcp - Copy data to a server
memdump - Dumping your server
memerror - translate an error code to a string
memflush - flush all data from a server
memrm - Remove data from a server
memslap - Load testing and benchmarking a server
memaslap - Load testing and benchmarking a server
memstat - Gather statistics from a server
memexist - Check for the existance of a key
memparse - Parse an option string
memping - Test to see if a server is available.
memtouch - Touches a key.
ex: memccat --servers=127.0.0.1