Mark Ku's Blog

Automatically Syncing Docker Redis Data with PowerShell

The Problem

Our development server is located in the US. This often causes network delays and instability during front-end development.

The Solution

Use Windows Task Scheduler and PowerShell to sync data with the Redis instance in the US.

First, create an auto-sync.ps1 script and place it in the D:\Powershell\ folder.

## auto-sync.ps1
$masterRedisIp = 192.168.40.30 // usa redis 
$masterRedisPort = 6399
$slaveRedisIp = 192.168.0.39  // taiwan redis 
slaveRedisPort = 6400

$Logfile = "D:\Logs\proc_$env:computername.log"

function WriteLog
{
Param ([string]$LogString)
$Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$LogMessage = "$Stamp $LogString"
Add-content $LogFile -value $LogMessage
}

WriteLog "The script is run"
docker exec -u 0 -it f08e2fc8c5a4 redis-cli -h $slaveRedisIp -p $slaveRedisPort slaveof $masterRedisIp $masterRedisPort

# Initial state
$syncCompleted = $false

# Monitor the replication status
while($syncCompleted -eq $false){
    Write-Host "Waiting for replication sync..."
    Start-Sleep -Seconds 10
    $replicaSyncStatus = docker exec -u 0 -it $containerId redis-cli -h $localRedis -p $localPort info replication | Select-String -Pattern "master_sync_in_progress:0"
    if($null -ne $replicaSyncStatus){
        $syncCompleted = $true
    }
}

docker exec -u 0 -it f08e2fc8c5a4 redis-cli -h $slaveRedisIp -p $laveRedisPort slaveof no one
WriteLog "The script is successfully executed"

Second, create create-sync-redis-task.ps1 and run this PowerShell script.

$account='admin'
$password='password'
$Action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-ExecutionPolicy Bypass -File D:\Powershell\sync-redis.ps1"'

$Trigger = New-ScheduledTaskTrigger -Daily -At 3am

$Settings = New-ScheduledTaskSettingsSet

$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings

Register-ScheduledTask -TaskName 'RedisSync' -InputObject $Task -User $account -Password $password

Open Windows Task Scheduler. You will see the task you just created.

Windows Task Scheduler displaying the highlighted RedisSync task
Windows Task Scheduler displaying the highlighted RedisSync task

P.S. During the synchronization process, the slave Redis instance cannot be modified.

Author

Mark Ku

擁有 10+ 年經驗的資深軟體工程師,現為 AI 應用 Builder,專注於大型平台架構與簡化複雜系統設計,從電商系統到訂閱與收費平台,結合 AI Agent、AI 整合與自動化開發,打造高效率且可持續演進的產品技術基礎。Read More

Found this useful?

The author's free tools, daily podcasts and newsletter are all here.

Mark Ku · This article is licensed under CC BY 4.0. Credit the author and link back to the original when reusing it.

Comments

Subscribe to Newsletter

Subscribe to get new posts delivered instantly — never miss a tech share.

By submitting, you agree to receive emails. You can anytime.

Popular Posts

View all
Mark Ku
··602

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution
Mark Ku
··490

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.
Mark Ku
··333

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki
Mark Ku
··264

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning
Mark Ku
··221

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1
Mark Ku
··215

Setting Up Samba on Ubuntu to Share Folders with Windows 11

Setting Up Samba on Ubuntu to Share Folders with Windows 11