Redis數(shù)據(jù)備份與恢復(fù)方式的五種方式
1. 命令行執(zhí)行 save 手動(dòng)開啟 RDB 持久化
使用 RDB 文件做遷移時(shí),需要注意需要先關(guān)閉掉目標(biāo) redis 的 aof 功能,因?yàn)槿绻咄瑫r(shí)存在的話,會(huì)優(yōu)先于 aof 的方式進(jìn)行數(shù)據(jù)恢復(fù)。
redis-cli -h {target-host} -a {target-password} config set appendonly no
備份
# 會(huì)阻塞主進(jìn)程 127.0.0.1:6379> save OK # 通過(guò) fork 一個(gè)專門 save 的子進(jìn)程,從而不會(huì)阻塞主進(jìn)程 127.0.0.1:6379> bgsave Background saving started
恢復(fù)
# 查看 redis 默認(rèn)存放備份文件的目錄路徑 127.0.0.1:6379> config get dir # 查看備份 RDB 文件的名稱,默認(rèn)為 `dump.rdb` 127.0.0.1:6379> config get dbfilename
將備份之后的 dump.rdb 文件放到 config get dir 命令得出的目錄路徑下,然后重啟 redis 即可恢復(fù)。(建議備份的時(shí)候,可以將 redis 暫時(shí)關(guān)閉)
2. 通過(guò)命令行手動(dòng)開啟 AOF 持久化
備份
# 先清空目標(biāo) redis 中全部數(shù)據(jù)
redis-cli -h {target-host} -a {target-password} flushall
# 然后在源 redis 中生成 aof 備份文件
redis-cli -h {source-host} -a {source-password} config set appendonly yes
# 查看生成后的 appendonly.aof 文件所在目錄
127.0.0.1:6379> config get dir
# 查看備份的 aof 文件的名稱,默認(rèn)為 `appendonly.aof`
127.0.0.1:6379> config get appendfilename
恢復(fù)
# 將 `appendonly.aof` 文件放在當(dāng)前路徑下
redis-cli -h {target-host} -a {target-password} --pipe < appendonly.aof
# 源 redis 關(guān)閉 aof 功能
redis-cli -h {source-host} -a {source-password} config set appendonly no
將備份之后的 appendonly.aof 文件放到 config get dir 命令得出的目錄路徑下,然后重啟 redis 也應(yīng)該可恢復(fù)(具體我沒(méi)有實(shí)操,看資料所說(shuō)如此)。
3. 使用 redis-dump
redis-dump 是一個(gè)用于 redis 數(shù)據(jù)導(dǎo)入導(dǎo)出的工具(可以以新增的形式導(dǎo)入),是基于 Ruby 實(shí)現(xiàn)的,因此需要先安裝 Ruby 環(huán)境,建議安裝 2.6.1 版本以上的 Ruby。
MAC 上使用 Homebrew 安裝 Ruby
brew install ruby
使用 rvm (ruby 版本管理器)安裝 ruby
centos 7 上安裝 rvm
curl -sSL https://rvm.io/mpapis.asc | gpg2 --import - curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import - # 安裝成功之后退出終端,然后可以通過(guò) `rvm help` 進(jìn)行查看 \curl -sSL https://get.rvm.io | bash -s stable # 如果不想退出終端,可以直接重載配置文件 source /etc/profile.d/rvm.sh
rvm 常用命令
# 列出已知的 ruby 版本 rvm list known # 安裝指定版本的 ruby rvm install 2.3.0 # 更新 rvm rvm get stable # 切換到指定 ruby 版本 rvm use 2.2.1 # 設(shè)置指定 ruby 版本為默認(rèn)版本 rvm use 2.2.2 --default # 查詢已經(jīng)安裝的 ruby 版本 rvm list # 卸載指定的 ruby 版本 rvm remove 1.9.1
安裝 redis-dump
# 安裝 ruby 2.6.1 rvm install 2.6.1 # 使用 2.6.1 rvm use 2.6.1 rvm use 2.6.1 --default # 查看 ruby 版本 ruby --version # 替換 gem 鏡像地址 gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ # 查看鏡像地址是否更換成功 gem sources -l # 安裝 redis-dump gem install redis-dump -V
redis-dump 備份與恢復(fù)
以增量的形式恢復(fù)
備份
# 數(shù)據(jù)導(dǎo)出
redis-dump -u 127.0.0.1:6379 > data.json
# 導(dǎo)出指定數(shù)據(jù)庫(kù)中的數(shù)據(jù),比如說(shuō) 8 號(hào)數(shù)據(jù)庫(kù)
redis-dump -u 127.0.0.1:6379 -d 8 > data8.json
# 如果 redis 設(shè)置了有密碼
redis-dump -u {host} -a {password} > data.json
redis-dump -u :{password}@127.0.0.1:6379 > data.json
# 如果需要導(dǎo)出的 redis 是一個(gè) URL 連接地址時(shí),貌似可以這樣(沒(méi)有實(shí)操過(guò),具體不清楚)
redis-dump -u :{password}@{domain}:{port}
# eg: redis-dump -u :123456@www.alex.com:9055
恢復(fù)
# 導(dǎo)入命令 cat data.json | redis-load # 或者 < data.json redis-load # 導(dǎo)入數(shù)據(jù)到 8 號(hào)數(shù)據(jù)庫(kù) cat data8.json | redis-load -u 127.0.0.1:6379 -a 123456 -d 8 # 或者 < data8.json redis-load -u 127.0.0.1:6379 -a 123456 -d 8 # 如果以上命令是因?yàn)?utf-8 格式報(bào)錯(cuò)時(shí),可以加上 `-n` 參數(shù) cat data8.json | redis-load -n -u 127.0.0.1:6379 -a 123456 -d 8 # 如果 redis 設(shè)置了有密碼 cat data.json | redis-load -u :password@127.0.0.1:6379
4. 通過(guò)腳本實(shí)現(xiàn)遷移
#!/bin/bash
# 通過(guò)這個(gè)腳本執(zhí)行備份有兩個(gè)缺點(diǎn),一是使用了 `keys *` ,二是那就是會(huì)將 source_host 中所有的 key
# 同步到 target_host 時(shí),會(huì)自動(dòng)改成永不過(guò)期
# 詳見 `restore` 命令
# 后期有時(shí)間了,再考慮優(yōu)化的事情吧
# document link: https://www.redis.com.cn/commands/restore.html
source_host=127.0.0.1
source_port=6379
source_password=''
source_db=1
target_host=127.0.0.1
target_port=6379
target_password=''
target_db=2
if [[ -z $source_password ]] && [[ -z $target_password ]]
then
redis-cli -h ${source_host} -p ${source_port} -n ${source_db} keys '*' | while read key
do
redis-cli -h ${source_host} -p ${source_port} -n ${source_db} --raw dump $key | perl -pe 'chomp if eof' | redis-cli -h ${target_host} -p ${target_port} -n ${target_db} -x restore $key 0
echo "migrate key $key"
done
elif [[ -z $source_password ]] && [[ -n $target_password ]]
then
redis-cli -h ${source_host} -p ${source_port} -n ${source_db} keys '*' | while read key
do
redis-cli -h ${source_host} -p ${source_port} -n ${source_db} --raw dump $key | perl -pe 'chomp if eof' | redis-cli -h ${target_host} -p ${target_port} -a ${target_password} -n ${target_db} -x restore $key 0
echo "migrate key $key"
done
elif [[ -n $source_password ]] && [[ -z $target_password ]]
then
redis-cli -h ${source_host} -p ${source_port} -a ${source_password} -n ${source_db} keys '*' | while read key
do
redis-cli -h ${source_host} -p ${source_port} -a ${source_password} -n ${source_db} --raw dump $key | perl -pe 'chomp if eof' | redis-cli -h ${target_host} -p ${target_port} -n ${target_db} -x restore $key 0
echo "migrate key $key"
done
else
redis-cli -h ${source_host} -p ${source_port} -a ${source_password} -n ${source_db} keys '*' | while read key
do
redis-cli -h ${source_host} -p ${source_port} -a ${source_password} -n ${source_db} --raw dump $key | perl -pe 'chomp if eof' | redis-cli -h ${target_host} -p ${target_port} -a ${target_password} -n ${target_db} -x restore $key 0
echo "migrate key $key"
done
fi
# 其實(shí)就是利用了 redis 的 dump 和 restore 命令
# eg:
# 127.0.0.1:6379[1]> set hello "hello, dumping world!"
# OK
# 127.0.0.1:6379[1]> dump hello
# "\x00\x15hello, dumping world!\t\x00\x03\xbfc\xcey\xa1\x9e\xfc"
# 127.0.0.1:6379[1]> restore hello1 0 "\x00\x15hello, dumping world!\t\x00\x03\xbfc\xcey\xa1\x9e\xfc"
# OK
# 127.0.0.1:6379[1]> get hello1
# "hello, dumping world!"
# 127.0.0.1:6379[1]>
5. redis 使用 migrate 命令遷移數(shù)據(jù)腳本
這種方式在 key 比較多的情況下也不是很推薦。
#!/bin/bash
src_redis="10.8.163.1"
src_port="6379"
dest_redis="10.8.132.13"
dest_port="6379"
for y in $(redis-cli -h ${src_redis} -p ${src_port} keys "*"); do
redis-cli -h ${src_redis} -p ${src_port} migrate ${dest_redis} ${dest_port} ${y} 0 1000 copy replace
echo "$(date +%F\ %T) Copy key ${y} to new redis...."
done到此這篇關(guān)于Redis數(shù)據(jù)備份與恢復(fù)方式的五種方式的文章就介紹到這了,更多相關(guān)Redis數(shù)據(jù)備份與恢復(fù)方式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
RedisDesktopManager無(wú)法遠(yuǎn)程連接Redis的完美解決方法
下載RedisDesktopManager客戶端,輸入服務(wù)器IP地址,端口(缺省值:6379);點(diǎn)擊Test Connection按鈕測(cè)試連接,連接失敗,怎么回事呢?下面小編給大家?guī)?lái)了RedisDesktopManager無(wú)法遠(yuǎn)程連接Redis的完美解決方法,一起看看吧2018-03-03
如何使用redis中的zset實(shí)現(xiàn)滑動(dòng)窗口限流
滑動(dòng)窗口限流是一種常見的流量控制方法,它限制了在一定時(shí)間窗口內(nèi)的請(qǐng)求數(shù)量,下面是使用Redis ZSet實(shí)現(xiàn)滑動(dòng)窗口限流的一個(gè)簡(jiǎn)單示例,需要的朋友可以參考下2023-09-09
在CenOS系統(tǒng)下安裝和配置Redis數(shù)據(jù)庫(kù)的教程
這篇文章主要介紹了在CenOS系統(tǒng)下安裝和配置Redis數(shù)據(jù)庫(kù)的教程,Redis是一個(gè)可基于內(nèi)存的高性能NoSQL數(shù)據(jù)庫(kù),需要的朋友可以參考下2015-11-11
Redis主從復(fù)制與讀寫分離的實(shí)現(xiàn)
Redis在作為緩存的時(shí)候,隨著項(xiàng)目訪問(wèn)量的增加,對(duì)Redis服務(wù)器的操作也越加頻繁,雖然Redis讀寫速度都很快,但是一定程度上也會(huì)造成一定的延時(shí),本文主要介紹了Redis主從復(fù)制與讀寫分離的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
使用Redis快速實(shí)現(xiàn)共享Session登錄的詳細(xì)步驟
在Web開發(fā)中,Session通常用于存儲(chǔ)用戶的會(huì)話信息,允許用戶在多個(gè)頁(yè)面之間保持登錄狀態(tài),Redis是一個(gè)開源的高性能鍵值數(shù)據(jù)庫(kù),廣泛用于緩存、消息隊(duì)列、會(huì)話存儲(chǔ)等場(chǎng)景,本文給大家介紹了如何使用Redis實(shí)現(xiàn)共享Session登錄,需要的朋友可以參考下2025-08-08
Redis中ZSet數(shù)據(jù)結(jié)構(gòu)與滑動(dòng)窗口應(yīng)用實(shí)現(xiàn)
Redis?ZSET融合哈希表與跳躍表,支持O(1)成員查詢和O(logN)排序,適用于排行榜及滑動(dòng)時(shí)間窗口限流,下面就來(lái)介紹一下Redis中ZSet數(shù)據(jù)結(jié)構(gòu)與滑動(dòng)窗口應(yīng)用,感興趣的可以了解一下2025-07-07

