[Windows] 比對檔案時間並拷貝較新檔案的命令行作法

用Certbot產生SSL憑證的操作步驟並使用dns01的方法取得萬用憑證(wildcard),產生的憑證檔適用於「*.your-domain.com」,其他子域名主機必須去拷貝取得每三個更新一次的憑證檔。找了一下比對檔案時間並拷貝的方法,最終以下列批次檔處理,將批次檔加入排程後,就能自動拷貝新檔到需要的主機資料夾裡了。

批次檔

echo %DATE% %TIME% > c:\util\update-cert.log
net use x: \\certbot-server-IP\your-cert-folder  /user:UserName Password >> c:\util\update-cert.log
xcopy /L /D /Y x:\cert.pem c:\certbot\live\your-domain.com\cert.pem |findstr /B /C:"1 " && copy x:\*.pem c:\certbot\live\your-domain.com /y >> c:\util\update-cert.log

重點說明

  • xcopy 永遠回傳true,將其輸出再丟給findstr
    • /L: Displays files that would be copied.
    • /D:m-d-y Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time.
    • /Y: 回應 Yes
  • findstr
    • /B: Matches pattern if at the beginning of a line.
    • /C"1 ": /C:string Uses specified string as a literal search string. 若xcopy回傳 1 File(s) copied時會繼續執行 && 後面的命令

##

您可能也會有興趣的類似文章

您可能也會喜歡…

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *