Try   HackMD

IIS設定備份與還原

情境

如果有一大堆的IIS站台,重灌之後要一個個還原回去,這個過程會十分痛苦,更可怕的是如果設定漏了,可能根本不知道設定不正確,這篇要講的就是這些不會常常用但是很重要的東西

首先,備份只能透過 commandline 的方式執行,沒有辦法透過IIS本身的GUI圖形介面操作,而且備份程式放在system32,因此請用系統管理員或是權限夠高的角色開啟 cmd 命令提示列的操作畫面

切換到 %windir%\system32\inetsrv\ 這個目錄

確定該目錄有 appcmd.exe 這個程式後,依照下面的指令執行


適用於IIS 7.5 ~ IIS 10 (win10 或 winserver 2016實測可正常運作)

備份的語法

appcmd add backup "backupName"
範例

    C:\Windows\System32\inetsrv>appcmd add backup "20180816"
    已新增 BACKUP 物件 "20180816”

如果在這個部分出現錯誤訊息
ERROR hresult:80070005 命令執行失敗。存取被拒

代表目前的使用者沒有讀取或寫入 backup的權限,請先點選開始功能表 cmd,按右鍵使用系統管理員身分執行

為什麼我已經是 Administrator群組了,但權限會不夠?
因為該目錄僅可供管理員帳戶或電腦上管理員群組成員的使用者使用。此外,Administrators 群組的成員必須以提升的權限啟動 Appcmd.exe,才能查看和更改伺服器級設定檔中的設定:Machine.config、root Web.config 檔案(位於 .NET Framework 目錄中)和應用程式主機. config。
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/jj635852(v=ws.11)

備份的資料夾存放在 %WinDir%\System32\inetsrv\backup ,如果是要重灌,請到這個資料夾將備份檔取出

還原語法

如果是從其他電腦取回備份的檔案要還原,要將檔案放置到 %WinDir%\System32\inetsrv\backup (跟上面一樣),同電腦備份還原就不用特別處理了

appcmd restore backup "backupName"

    C:\Windows\System32\inetsrv>appcmd restore backup 20180816
    已從備份 "20180816" 還原設定

自動備份

IIS 在每次更新都會自動備份,預設保留10次,自動備份的資料夾是在 %SYSTEMDRIVE%\inetpub\history

如果要增加保留的數量,可以使用以下指令

appcmd.exe set config -section:system.applicationHost/configHistory /maxHistories:"100" /commit:apphost

以下適用於IIS 7

IIS7尚未測試是否可用,沒有舊的機器可以測,只有找到方法而已

    To import the Application Pools:
    %windir%\system32\inetsrv\appcmd add apppool /in < c:\apppools.xml
    All the AppPools in the xml will be created on your second webserver.
    To Export all your website:
    %windir%\system32\inetsrv\appcmd list site /config /xml > c:\sites.xml
    This will export all the websites on your webserver, therefor you need to edit the sites.xml and remove the websites that you do not need to import for example:
        - Default Website
    And all other websites that already exist on the second webserver.
    To Import the website:
    %windir%\system32\inetsrv\appcmd add site /in < c:\sites.xml
    It’s also possible to export a single website or application pool all you need to do is add the name of the Application Pool or Website to the command line:
    To export/import a single application pool:
    %windir%\system32\inetsrv\appcmd list apppool “MyAppPool” /config /xml > c:\myapppool.xml
    Import:
    %windir%\system32\inetsrv\appcmd add apppool /in < c:\myapppool.xml
    To export/import a single website:
    %windir%\system32\inetsrv\appcmd list site “MyWebsite” /config /xml > c:\mywebsite.xml
    Import:
    %windir%\system32\inetsrv\appcmd add site /in < c:\mywebsite.xml

參考文件

tags: 程式設計