理論上,Memcached 不需要做備份、還原這種事情,因為存在彥面的東西應該都是 Cache,沒有使用 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
看看上述兩個資料筆數(shù)是否正確.
上述參考自此篇: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 --Vevb.com
- 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
新聞熱點
疑難解答
圖片精選