以我的Garden主题为例,在Functions.php中引入以下内容(我是放在单独文件中的)

<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
//读取数据库 并将现在的主题配置内容赋值给 $backup_from_val
$db         = Typecho_Db::get();
$backup_from_db  = $db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Garden'));
$backup_from_val = $backup_from_db['value'];
$restore_from_db  = $db->fetchRow($db->select()->from ('table.options')->where ('name = ?', 'theme:Garden_backup'));
$restore_from_val = $restore_from_db['value'];

// 判断交互请求

if(isset($_POST['type'])){
    if($_POST["type"]=="备份模板数据"){
        if($restore_from_db){
            // 更新 theme:Garden_backup 字段 
            $update = $db->update('table.options')->rows(array('value'=>$backup_from_val))->where('name = ?', 'theme:Garden_backup');
            $updateRows= $db->query($update);
            echo '<div class="success">备份已更新,即将自动刷新页面!';
            ?>    
            <a href="<?php Helper::options()->adminUrl('options-theme.php'); ?>">;您也可以手动刷新</a></div>
            <script language="JavaScript">window.setTimeout("location=\'<?php Helper::options()->adminUrl('options-theme.php'); ?>\'", 2500);</script>
            <?php
        }else{
            // 备份不存在 则新建 theme:Garden_backup 字段 并插入value为$backup_from_val
            // 这个if($backup_val)的判断其实可以去掉,没用
          if($backup_from_val){
             $insert = $db->insert('table.options')->rows(array('name' => 'theme:Garden_backup','user' => '0','value' => $backup_from_val));
             $insertId = $db->query($insert);
            echo '<div class="success">新建备份完成,即将自动刷新页面!';
          ?>    
            <a href="<?php Helper::options()->adminUrl('options-theme.php'); ?>">;您也可以手动刷新</a></div>
            <script language="JavaScript">window.setTimeout("location=\'<?php Helper::options()->adminUrl('options-theme.php'); ?>\'", 2500);</script>
          <?php
          }
        }
    }
    

    if($_POST["type"]=="还原模板数据"){
        if($restore_from_db){
            // 更新 主题配置表字段theme:Garden内容为 $restore_from_val
            $update = $db->update('table.options')->rows(array('value'=>$restore_from_val))->where('name = ?', 'theme:Garden');
            $updateRows= $db->query($update);
            echo '<div class="success">恭喜你,模板配置数据已经恢复完成,即将自动刷新页面!';
            ?>    
            <a href="<?php Helper::options()->adminUrl('options-theme.php'); ?>">;您也可以手动刷新</a></div>
            <script language="JavaScript">window.setTimeout("location=\'<?php Helper::options()->adminUrl('options-theme.php'); ?>\'", 2500);</script>
            <?php
        }else{
            // 如果不存在已备份数据
            echo '<div class="notice">没有模板备份数据,恢复不了哦!</div>';
        }
    }

    if($_POST["type"]=="删除备份数据"){
        // 如果存在备份内容 则删除备份并提示
        if($restore_from_db){
            $delete = $db->delete('table.options')->where ('name = ?', 'theme:Garden_backup');
            $deletedRows = $db->query($delete);
            echo '<div class="success">删除成功,请等待自动刷新,如果等不到请点击';
            ?>    
            <a href="<?php Helper::options()->adminUrl('options-theme.php'); ?>">这里</a></div>
            <script language="JavaScript">window.setTimeout("location=\'<?php Helper::options()->adminUrl('options-theme.php'); ?>\'", 2500);</script>
            <?php
        // 如果不存在备份数据直接提示不存在
        }else{
            echo '<div class="notice">备份不存在!</div>';
        }
    }
}
echo '<form class="protected" action="?Garden_backup" method="post">
<input type="submit" name="type" class="btn btn-s btn-s-b" value="备份模板数据" />&nbsp;&nbsp;<input type="submit" name="type" class="btn btn-s btn-s-r" value="还原模板数据" />&nbsp;&nbsp;<input type="submit" name="type" class="btn btn-s btn-s-d" value="删除备份数据" /></form>';