Skip to main content
Glama

restore_backup

Restore files from their most recent backup copies to recover previous versions or fix errors, with options to preserve backup files after restoration.

Instructions

Restore a file from its most recent backup (.bak)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fileYesFile to restore from backup
keepBackupNoKeep the backup file after restoring

Implementation Reference

  • The handler function for 'restore_backup' tool. It restores the specified file from its .bak backup (or alternatives), creates a .before-restore safety backup of the current file, writes the backup content back to the original file, optionally deletes the backup, and returns a success message.
    case 'restore_backup': {
      const { file, keepBackup = true } = args;
      
      const backupFile = `${file}.bak`;
      
      // Check if backup exists
      if (!existsSync(backupFile)) {
        // Look for other common backup patterns
        const alternatives = [
          `${file}~`,
          `${file}.backup`,
          `${file}.orig`
        ].filter(existsSync);
        
        if (alternatives.length > 0) {
          throw new Error(`No .bak file found, but found: ${alternatives.join(', ')}`);
        }
        throw new Error(`No backup file found for ${file}`);
      }
      
      // Read backup content
      const backupContent = await readFile(backupFile, 'utf-8');
      
      // Check if current file exists and create a safety backup
      if (existsSync(file)) {
        await writeFile(`${file}.before-restore`, await readFile(file, 'utf-8'));
      }
      
      // Restore the backup
      await writeFile(file, backupContent);
      
      // Remove backup if requested
      if (!keepBackup) {
        await execAsync(`rm -f '${backupFile}'`);
      }
      
      return {
        content: [{
          type: 'text',
          text: `Successfully restored ${file} from backup${!keepBackup ? ' (backup removed)' : ''}\nSafety backup created: ${file}.before-restore`
        }]
      };
    }
  • src/index.ts:227-245 (registration)
    Registration of the 'restore_backup' tool in the ListToolsRequestSchema handler, including name, description, and input schema.
    {
      name: 'restore_backup',
      description: 'Restore a file from its most recent backup (.bak)',
      inputSchema: {
        type: 'object',
        properties: {
          file: {
            type: 'string',
            description: 'File to restore from backup'
          },
          keepBackup: {
            type: 'boolean',
            default: true,
            description: 'Keep the backup file after restoring'
          }
        },
        required: ['file']
      }
    },
  • Input schema definition for the restore_backup tool, specifying parameters 'file' (required) and 'keepBackup' (optional boolean).
    type: 'object',
    properties: {
      file: {
        type: 'string',
        description: 'File to restore from backup'
      },
      keepBackup: {
        type: 'boolean',
        default: true,
        description: 'Keep the backup file after restoring'
      }
    },
    required: ['file']
  • Help text and usage examples for the restore_backup tool in the helpContent object.
      restore_backup: `restore_backup - Restore from backup
    =================================
    Restore a file from its .bak backup file.
    
    Examples:
      // Basic restore
      restore_backup({ file: "config.json" })
      // Looks for config.json.bak and restores it
      
      // Restore and remove backup
      restore_backup({ file: "data.txt", keepBackup: false })
      
      // After a bad edit
      sed_edit({ file: "app.js", pattern: "s/function/fungtion/g" }) // Oops!
      restore_backup({ file: "app.js" }) // Fixed!
    
    Safety features:
    - Creates .before-restore backup of current file
    - Checks for alternative backup formats (.backup, .orig, ~)
    - Clear error if no backup found
    `,

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/MikeyBeez/mcp-smalledit'

If you have feedback or need assistance with the MCP directory API, please join our Discord server