Azure Cache for Redis 如何迁移
【Azure Redis 缓存】Azure Cache for Redis有默认备份可以用于恢复么?一文中,介绍了使用RDB文件的方式来迁移Redis中的数据或者事备份恢复。与此同时,在Azure Cache for Redis的官方文档描述中,也列举出了有一下三种方式:
- 将数据导出到 RDB 文件并将该文件导入 Azure Cache for Redis: 此方法需要在Redis高级版中完成。https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-migration-guide#export-data-to-an-rdb-file-and-import-it-into-azure-cache-for-redis
- 在迁移期间同时写入两个 Redis 缓存: https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-migration-guide#write-to-two-redis-caches-simultaneously-during-migration-period
- https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-migration-guide#migrate-programmatically
开源工具下载地址:https://github.com/deepakverma/redis-copy 最核心的代码为:
await sourcedb.KeyDumpAsync(key).ContinueWith(dump => { if (dump.IsFaulted || dump.IsCanceled) { throw new AggregateException(dump.Exception); } else { //Redis > 3.0, if key already exists it won't overwrite destdb.KeyRestoreAsync(key, dump.Result, ttl.Result).ContinueWith(restore => { Interlocked.Increment(ref totalKeysCopied); percent = ((double)totalKeysCopied / totalKeysSource) * 100; TasksInProgress[source] = percent; }); } });
其最核心的思想使用的是Redis DUMP和RESTORE命令。
DUMP key:
Serialize the value stored at key in a Redis-specific format and return it to the user. The returned value can be synthesized back into a Redis key using the RESTORE command.
Redis DUMP 命令用于序列化给定 key ,并返回被序列化的值。
RESTORE key
Create a key associated with a value that is obtained by deserializing the provided serialized value (obtained via DUMP).
参考资料
https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-migration-guide#migrate-programmatically
Redis 数据备份与恢复: https://www.runoob.com/redis/redis-backup.html