﻿# 如何設定 Cisco R2951 在寫入設定時自動儲存備份

公司有台 Cisco R2951 最近在架構上有做一些異動，之前設定的自動備份失效了，所以就重新設定一次，這篇文章就順便記錄一下設定過程。

![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/5b36bd555de4fdd812dd-200596173-3fb79611-5e46-44e9-8ba3-516c25934d3b.png)

在開始設定之前，你要先準備好一個備份空間，目前 Cisco 支援的傳輸協議包含以下幾種：

```
flash0:  Write archive on flash0: file system
flash1:  Write archive on flash1: file system
flash:   Write archive on flash: file system
ftp:     Write archive on ftp: file system
http:    Write archive on http: file system
https:   Write archive on https: file system
pram:    Write archive on pram: file system
rcp:     Write archive on rcp: file system
scp:     Write archive on scp: file system
tftp:    Write archive on tftp: file system
```

最這邊選擇最簡單的 TFTP 來進行寫入，因為公司的 Synology NAS 就有內建 TFTP 服務，設定檔相當簡單。

以下是設定步驟：

1.  進入 Global Configuration Mode (全域設定模式)
    
    ```bash
    conf t
    ```
    
2.  進入 `config-archive` 模式
    
    ```bash
    archive
    ```
    
    ```bash
    R2951(config-archive)#?
    Archive configuration commands:
      default       Set a command to its defaults
      exit          Exit from archive configuration mode
      log           Logging commands
      maximum       maximum number of backup copies
      no            Negate a command or set its defaults
      path          path for backups
      rollback      Rollback parameters
      time-period   Period of time in minutes to automatically archive the running-config
      write-memory  Enable automatic backup generation during write memory
    ```
    
3.  設定備份路徑
    
    ```bash
    path tftp://192.168.99.199/$h$t.cfg
    ```
    
    > 這裡的 `$h` 是一個 Placeholder，代表 `Hostname` (主機名稱) 的意思。而 `$t` 則代表當下時間，格式會像這樣 `Nov-18-14-30-59.734`。
    
4.  啟用自動備份功能
    
    ```bash
    write-memory
    ```
    
5.  立即寫入設定
    
    ```bash
    do write
    ```
    
    > 你也可以按下 `Ctrl-Z` 退出 Global Configuration Mode，然後輸入 `write` 寫入設定。
    
6.  立即查詢寫入紀錄
    
    如果你還在全域設定模式中，可以這樣執行：
    
    ```bash
    do show archive
    ```
    
    如果你已經退出全域設定模式，可以這樣執行：
    
    ```bash
    show archive
    ```
    
    接著就會列出最近 10 次備份記錄：
    
    ```batch
    The maximum archive configurations allowed is 10.
    The next archive file will be named tftp://192.168.99.199/R2951-Nov--8-14-35-59.734.cfg-1
    Archive #  Name
      1        tftp://192.168.99.199/R2951-Nov--8-14-30-59.734.cfg-0 <- Most Recent
      2
      3
      4
      5
      6
      7
      8
      9
      10
    ```
    

### 相關連結

-   [Configuration Archiving - Cisco IOS Cookbook, 2nd Edition \[Book\]](https://www.oreilly.com/library/view/cisco-ios-cookbook/0596527225/ch01s22.html)
-   [Using Cisco’s IOS Archive | Weberblog.net](https://weberblog.net/using-ciscos-ios-archive/)
