1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| # Redis 目录 $redis_dir = "e:\redis" # Redis 主机地址 $redis_host = "localhost" # Redis 端口号 $redis_port = 6379 # Redis 密码 $redis_pwd = "123123"
# 备份根目录 $backup_dir = "e:\redis_bak" # 当前日期 $date = Get-Date -Format "yyyyMMdd" # 备份时间 $time = Get-Date -Format "HHmmss"
# 备份文件名格式 $rdb_file = "rdb_$($date)_$($time).rdb" $aof_file = "aof_$($date)_$($time).aof" $zip_file = "$($date)_$($time).zip"
# 创建备份目录 New-Item -ItemType Directory -Path $backup_dir\$date | Out-Null
# 备份 Redis 数据库 & "$redis_dir\redis-cli.exe" -h $redis_host -p $redis_port -a $redis_pwd bgsave # 备份 RDB 文件 Copy-Item "$redis_dir\dump.rdb" "$backup_dir\$date\$rdb_file" # 备份 AOF 文件 Copy-Item "$redis_dir\appendonly.aof" "$backup_dir\$date\$aof_file"
# 压缩备份文件 Compress-Archive -Path "$backup_dir\$date\*" -DestinationPath "$backup_dir\$zip_file" -Force # 删除 7 天前的备份文件 Get-ChildItem -Path $backup_dir -Recurse | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } | Remove-Item -Force
|