Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save benbai123/8df98eb4f7499254df2cacbb26184ba5 to your computer and use it in GitHub Desktop.
Save benbai123/8df98eb4f7499254df2cacbb26184ba5 to your computer and use it in GitHub Desktop.
Use WinSCP to Get Files From Remote Ubuntu Daily
open USERNAME:PASSWORD@UBUNTU_HOST_ADDR
synchronize local C:\Users\Bai\SCP\Latest /home/benbai/SCP
exit
@echo off
SETLOCAL enabledelayedexpansion
SET scpbase=C:\Users\Bai\SCP\
SET scplatest=C:\Users\Bai\SCP\Latest
:: install WinSCP on Windows
:: install openssh server on Ubuntu
:: sudo apt-get install openssh-server
:: sudo ufw allow 22
:: 取當日
:loop
SET /a count=0
FOR /F "skip=1" %%D IN ('wmic path win32_localtime get dayofweek') DO (
if "!count!" GTR "0" GOTO next
set dow=%%D
IF !dow!==0 (
set dowday=Sunday
set nextday=Monday
)
IF !dow!==1 (
set dowday=Monday
set nextday=Tuesday
)
IF !dow!==2 (
set dowday=Tuesday
set nextday=Wednesday
)
IF !dow!==3 (
set dowday=Wednesday
set nextday=Thursday
)
IF !dow!==4 (
set dowday=Thursday
set nextday=Friday
)
IF !dow!==5 (
set dowday=Friday
set nextday=Saturday
)
IF !dow!==6 (
set dowday=Saturday
set nextday=Sunday
)
SET /a count+=1
)
:next
:: 若沒有 latest 資料夾就建立
IF NOT EXIST %scplatest% (
mkdir %scplatest%
)
::若沒有當天資料夾就同步並儲存
IF NOT EXIST %scpbase%%dowday% (
echo "### sync and backup"
:: 做 SCP 同步
"C:\Program Files (x86)\WinSCP\WinSCP.com" /script=full_remote_to_local_synchronization.txt
:: 建立當天資料夾
mkdir %scpbase%%dowday%
:: 將同步後內容複製進當天資料夾
xcopy %scplatest% %scpbase%%dowday% /s /e
:: 當日備份後清空 latest 資料夾
:: 以免遠端刪除檔案堆積 (sync 不會刪除遠端已刪除的檔案)
RMDIR /S /Q %scplatest%
) ELSE (
:: Don't know why same fragment above not working this case
:: Do it again here
:: 若沒有 latest 資料夾就建立
IF NOT EXIST %scplatest% (
mkdir %scplatest%
)
echo "sync only"
:: 否則就只做同步
"C:\Program Files (x86)\WinSCP\WinSCP.com" /script=full_remote_to_local_synchronization.txt
)
:: 若有隔天的資料夾, 先砍掉它
IF EXIST %scpbase%%nextday% (
echo "exist %scpbase%%nextday%"
RMDIR /S /Q %scpbase%%nextday%
) ELSE (
echo "not exist %scpbase%%nextday%"
)
:: 等一段時間再跑過
timeout /t 30
GOTO loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment