Mark Ku's Blog
首頁 關於我
Powershell automatically syncs docker redis data
PowerShell
Powershell automatically syncs docker redis data
Mark Ku
Mark Ku
November 11, 2022
1 min

Powershell automatically syncs docker redis data

Problem

Because our development sever is in USA.
So In front-end development often many network delays and instability.

Solution concept

Use Windows Task Scheduler and Powershell to sync data with USA’s redis

First. You need create auto-sync.ps1 script and put Powershell to 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"
$sleepSecond = 900

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

Write-Host "wait $sleepSecond sec"
Start-Sleep -Seconds $sleepSecond

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 power shell

$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 the Windows Task Scheduler. You will see the task you just created.

P.S. In during the synchronization process, the slave redis cannot modify any redis data.


Tags

Mark Ku

Mark Ku

Software Developer

擁有豐富網站開發經驗,直播系統、POS系統、電子商務、平台網站、SEO,專業的網站開發鐵三工程師。

Expertise

前端(React)
後端(C#)
網路管理

Social Media

facebook github website

Related Posts

Quick set up developer environment using chocolatey and powershell
Quick set up developer environment using chocolatey and powershell
January 29, 2023
1 min

Quick Links

關於我

Social Media