LoginSignup
3

More than 5 years have passed since last update.

Redisのダンプ取得

Posted at

多少古いデータでも構わなければ、redis.confで指定した頻度で dump.rdb が更新されているが、RedisのBGSAVEコマンドを実行し、LASTSAVEが更新されるのを待つ。 これでdump.rdbが最新の状態となる。

redis_bgsave.sh
#!/bin/bash

# RedisのBGSAVEコマンドを実行し、redis.rdbが更新されるのを待つ。
# タイムアウト時間を超えたら exit status 1 で終了する。

TIMEOUT=30

# Timeout handler
# http://stackoverflow.com/questions/1226094/how-to-include-a-timer-in-bash-scripting
set_timer() {
        trap handle_timer ALRM
        (sleep $TIMEOUT; kill -ALRM $$)&
        timer_pid=$!
}

unset_timer() {
        kill $timer_pid
        trap - ALRM
}

handle_timer() {
        exit 1
}


export PATH=/bin:/usr/bin

set_timer

# 丁度この瞬間に組込のBGSAVEが走る可能性があるので、
# BGSAVEの前にLASTSAVEを取得
LASTSAVE=`redis-cli LASTSAVE`

redis-cli BGSAVE

# LASTSAVEが変わるまで待つ。
while [ $LASTSAVE == `redis-cli LASTSAVE` ]; do
        sleep 1
done

unset_timer

あとは /var/lib/redis/dump.rdb (もしくはその他の場所) を好きにすれば良い。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3