自動化 S3 s3cmd 下載。

@echo off

@rem =========================================
@rem Author: flyfox 20130305
@rem 使用需知
@rem 1. 需安裝 7-zip
@rem 2. 需安裝 mongodb
@rem 3. 需安裝 python
@rem =========================================

@rem =========================================
@rem 設定各路徑變數
@rem =========================================
@set DDir=D:\bat\
@set MongoDir=C:\mongodb\data\
@set python="C:\Python27\python.exe"
@set zip="C:\Program Files\7-Zip\7z.exe"
@set batlog=D:\bat\log
@set mydate=%date:~0,4%%date:~5,2%%date:~8,2%
@set tar=Test.%mydate%.tar
@set s3URL=s3://test/Test-mongo/Test.%mydate%.tar.bz2
@set Test=Test.%mydate%.tar.bz2
@set mongoimport=C:\mongodb\bin\mongoimport.exe
@md %batlog%


@rem =========================================
@rem 設定備份檔名稱、log檔名稱
@rem =========================================
@set log=D:\bat\log\log-%mydate%-%time:~0,2%%time:~3,2%%time:~6,2%.txt
@set log=%log: =0%
@set Test=BackEnd.%mydate%.json
@set Session=BackendCollection.%mydate%.json
@set ItemBuy=History.%mydate%.json
@set c1=Backend
@set c2=Collection
@set c3=History

@rem =========================================
@rem 從 S3 下載備份檔
@rem =========================================
@echo ================ start ================== > %log%
@echo Date: %date% Time: %time% >> %log%
@if exist %DDir%%Test% (del /F /S %DDir%%Test%)
@if exist %DDir%%tar% (del /F /S %DDir%%tar%)
@echo python s3cmd get %s3URL% %Test% >> %log%
@%python% C:\Python27\Scripts\s3cmd get %s3URL% %DDir%%Test% >> %log%
@rem move %Test% %DDir% >> %log%
@echo download Success. >> %log%
@goto decompress


:decompress
@rem =========================================
@rem 解壓縮、還原備份檔
@rem =========================================
@if exist %DDir%%Test% (%zip% e %DDir%%Test% -o%DDir% && echo. >> %log%) else (goto nofile)
@echo bz2 file decompress Success! >> %log%
@if exist %DDir%%tar% (%zip% e %DDir%%tar% -o%DDir% && echo. >> %log%) else (goto nofile)
@echo decompress Success! >> %log%
@move %DDir%%Test% %MongoDir% >> %log%
@move %DDir%%Session% %MongoDir% >> %log%
@move %DDir%%ItemBuy% %MongoDir% >> %log%
@echo move file Success! >> %log%
@%mongoimport% -d Test -c %c1% %MongoDir%%Test% >> %log%
@%mongoimport% -d Test -c %c2% %MongoDir%%Session% >> %log%
@%mongoimport% -d Test -c %c3% %MongoDir%%ItemBuy% >> %log%
@echo Mongodb import Success! >> %log%
@del %MongoDir%%Test% >> %log%
@del %MongoDir%%Session% >> %log%
@del %MongoDir%%ItemBuy% >> %log%
@echo Mongodb file delete Success! >> %log%
@if exist %DDir%%Test% (del /F /S %DDir%%Test%)
@if exist %DDir%%tar% (del /F /S %DDir%%tar%)
@goto close

:close
@pause
@exit
@echo on

:rewget
@rem =========================================
@rem 下載備份檔失敗,重新下載
@rem =========================================
@echo ================ rewget file ================== >> %log%
@echo Date: %date% Time: %time% >> %log%
@if exist D:\bat\%Test% (del /F /S D:\bat\%Test%)
@echo python s3cmd get %s3URL% %Test% >> %log%
@%python% C:\Python27\Scripts\s3cmd get %s3URL% %Test% >> %log%
@move %Test% %DDir% >> %log%
@echo rewget Success. >> %log%
@goto decompress

:nofile
@rem =========================================
@rem 找不到 Test 備份檔
@rem =========================================
@echo Can not find MongoDB bz2 file! >> %log%
@forfiles /P nofile /S /M *.* /D -30 /C "cmd /c del @path"
@exit
@echo on

flyfox 發表在 痞客邦 PIXNET 留言(0) 人氣()