当前位置: 首页 > 工具软件 > migrate-mongo > 使用案例 >

redis+mysql+mongo数据库备份恢复

周云
2023-12-01

redis

优势:缓存中间件,基于内存,速度快,适用于秒杀的库存扣件等状况

数据结构:string、hash、set、list、sortedset、、HyperLogLog、Geo、Pub/Sub

备份:开启aof备份,定期备份数据转移

数据恢复:在数据安全丢失的情况下,基于rdb冷备,如何完美的恢复数据,同时还保持aof和rdb的双开:

停止redis,关闭aof,删除aof文件,拷贝rdb备份,重启redis,确认数据恢复,直接在命令行热修改redis配置,打开aof,命令:config set appendonly yes 这时redis就会将内存中的数据对应的日志,写入aof文件中,此时aof和rdb两份数据文件的数据就同步了。可能配置文件中的实际的参数没有被持久化的修改,再次停止redis,手动修改配置文件,打开aof的命令,再次重启redis即可。

redis监控:info

127.0.0.1:6028> info
# Server 
redis_version:3.2.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:6e55487fce376704
redis_mode:standalone
os:Linux 3.10.0-957.21.3.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:14389
run_id:f4dbfcb755d55c680616229fd0894425263b76df
tcp_port:6028
uptime_in_seconds:4078415
uptime_in_days:47
hz:10
lru_clock:2083945
executable:/usr/local/redis326/bin/redis-server
config_file:/home/tyredis/redis/conf/6028.conf

# Clients
connected_clients:295
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:10809520
used_memory_human:10.31M
used_memory_rss:20439040
used_memory_rss_human:19.49M
used_memory_peak:12794520
used_memory_peak_human:12.20M
total_system_memory:33566564352
total_system_memory_human:31.26G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:1.89   #内存碎片率
mem_allocator:jemalloc-4.0.3

# Persistence 持久化信息
loading:0
rdb_changes_since_last_save:67972
rdb_bgsave_in_progress:0
rdb_last_save_time:1591841050
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:68344
total_commands_processed:771840341
instantaneous_ops_per_sec:238
total_net_input_bytes:242502894043
total_net_output_bytes:244579777313
instantaneous_input_kbps:94.44
instantaneous_output_kbps:95.14
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:4
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:29792.31
used_cpu_user:11960.99
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=1,expires=0,avg_ttl=0
127.0.0.1:6028>

安装启动

# 初始化redis
cd ./conf
echo "slaveof conf_game.tyredis.me 6338" >> ./6338.conf # 非主数据宿主机,配置库要进行slave

sed -i "s/save/#save/g" ./*.conf # 不需要存储 rdb 文件
sed -i "s/appendfilename/#appendfilename/g" ./*.conf # 不需要存储 aof 文件
sed -i "s/appendonly yes/appendonly no/g" ./*.conf # 不需要存储 aof 文件
echo 'requirepass "xxx"' |tee -a ./*.conf
echo 'masterauth "xxx"' |tee -a ./*.conf

# 制作全部启动脚本
cd ../bin
ls 6*.sh >> ./_all.sh
sed -i "s/^/.\//g" ./_all.sh
echo "cd ${MYLODER}/bin/" > ./all.sh
cat ./_all.sh >> ./all.sh
rm -fr ./_all.sh
chmod +x all.sh

echo 'start all redis use : redis/bin/all.sh'

# 启动脚本
#!/bin/bash
cd /usr/local/redis326/bin/
echo "shutdown" | ./redis-cli -p 6338 > /dev/null 2>&1
sleep 1
DBPATH=`pwd`
nohup $DBPATH/redis-server /redis/conf/6338.conf > redis/logs/6338.log 2>&1 &

mongo

安装

wget http://op.aliyun.tuyoo.com/percona-mongodb.tar
tar -xf  percona-mongodb.tar
yum localinstall Percona-Server-MongoDB-36* -y
rm Percona-Server-MongoDB-36-* -f
rm percona-mongodb.tar -f

# 数据查询
db.collection.find({“key1”:“value1”,“key2”:“value2”})
# https://blog.csdn.net/sinat_32366329/article/details/81784562

#1.数据删除:
use dbname
db.dropDatabase()

# 2.数据恢复:
mongorestore --port 20000 --host ip  -u admin -p xxx  --authenticationDatabase admin -d dbname   /tmp/dbname/

# 3.设置片键:
for i in `cat shard.txt`; do mongo  --port 20000 -u admin -p xxx --authenticationDatabase admin --eval "$i" ;done

备份:

mongodump --port 20000  --host ip  -u xxx -p xxx  --authenticationDatabase admin -d dbname  -o /tmp/dbname

恢复:

mongorestore --host new-ip --port 20000 -u xxx -p xxx --authenticationDatabase admin -d dbname --noIndexRestore /tmp/dbname 

mysql

备份恢复

# 备份库结构-d参数:
mysqldump -u root -p -d hellodb > /root/hellodb.sql
mysqldump -u root -p --all-databases > /root/alldb.sql
mysqldump -u root -p userdb > /root/userdb.sql
mysqldump -u root -p -B userdb hellodb test > /root/userdb+hellodb+test.sql

# 恢复:
mysql -u root -p  userlist2 < /root/userdb.sql。       //数据库userlist2为新建,不建议直接覆盖旧库
# 检查结果:
mysql> select sn ,homedir from userlist limit 5;



# 简述binlog日志:

my.cnf中添加log-bin=mysql-bin //重启mysql后每次执行操作,binlog会记录所有的数据库、表更改操作,
# 查看日志内容:
mysqlbinlog /var/lib/mysql/mysql-bin.000006
# 通过mysqlbinlog指定时间范围输出,结合管道交给msyql命令执行导入重做
mysqlbinlog  --start-datetime="  "   --stop-datetime=" " /var/lib/mysql/mysql-bin.000006 | mysql -u root -p


 类似资料: