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 # 通過 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. 通過命令行手動(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ù)(具體我沒有實(shí)操,看資料所說如此)。
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 - # 安裝成功之后退出終端,然后可以通過 `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ù)庫中的數(shù)據(jù),比如說 8 號(hào)數(shù)據(jù)庫 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í),貌似可以這樣(沒有實(shí)操過,具體不清楚) 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ù)庫 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. 通過腳本實(shí)現(xiàn)遷移
#!/bin/bash # 通過這個(gè)腳本執(zhí)行備份有兩個(gè)缺點(diǎn),一是使用了 `keys *` ,二是那就是會(huì)將 source_host 中所有的 key # 同步到 target_host 時(shí),會(huì)自動(dòng)改成永不過期 # 詳見 `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)文章
關(guān)于Redis數(shù)據(jù)庫入門詳細(xì)介紹
大家好,本篇文章主要講的是關(guān)于Redis數(shù)據(jù)庫入門詳細(xì)介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12Redis 通過 RDB 方式進(jìn)行數(shù)據(jù)備份與還原的方法
這篇文章主要介紹了Redis 通過 RDB 方式進(jìn)行數(shù)據(jù)備份與還原,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Caffeine實(shí)現(xiàn)類似redis的動(dòng)態(tài)過期時(shí)間設(shè)置示例
這篇文章主要為大家介紹了Caffeine實(shí)現(xiàn)類似redis的動(dòng)態(tài)過期時(shí)間示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08redis 實(shí)現(xiàn)登陸次數(shù)限制的思路詳解
這篇文章主要介紹了redis 實(shí)現(xiàn)登陸次數(shù)限制的思路詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08面試常問:如何保證Redis緩存和數(shù)據(jù)庫的數(shù)據(jù)一致性
在實(shí)際開發(fā)過程中,緩存的使用頻率是非常高的,只要使用緩存和數(shù)據(jù)庫存儲(chǔ),就難免會(huì)出現(xiàn)雙寫時(shí)數(shù)據(jù)一致性的問題,那我們又該如何解決呢2021-09-09Redis大key多key拆分實(shí)現(xiàn)方法解析
這篇文章主要介紹了Redis大key多key拆分實(shí)現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11