A Python script for automated backup of MikroTik routers, creating both binary (.backup) and plaintext configuration (.rsc) backups with automatic cleanup of old backup files.
Most of this was generated with AI, but has been reviewed and tested for accuracy. I'm using it to backup my local MikroTik routers. Haven't run long enough test backup clean-up. I wanted to have it stagger some of the removal so as to keep something out there for 1 a month, then 1 a year, etc. But figured to just manually copy out a back-up to save for posterity now and then.
- Creates both binary and plaintext configuration backups
- Supports multiple routers
- Automatic cleanup of old backup files
- Organized backup directory structure
- Secure SSH connection handling
- Automatic remote file cleanup after download
- Configurable backup retention period
- Python 3.6 or higher
- MikroTik router(s) with SSH access enabled
- Network connectivity to the router(s)
- Clone the repository:
git clone https://github.com/yourusername/mikrotik-backup.git
cd mikrotik-backup- Install required dependencies:
pip install paramikoCreate a config.json file in the script directory with the following structure:
{
"backup_age": 30,
"routers": [
{
"host": "192.168.88.1",
"username": "admin",
"password": "your_password",
"port": 22
},
{
"host": "192.168.88.2",
"username": "admin",
"password": "your_password",
"port": 22
}
]
}backup_age: Number of days to keep backups (default: 30)routers: Array of router configurationshost: Router IP address or hostnameusername: SSH usernamepassword: SSH passwordport: SSH port (default: 22)
mikrotik_backups/
├── binary/
│ └── [hostname]_backup_[timestamp].backup
└── plaintext/
└── [hostname]_config_[timestamp].rsc
Run the script:
python mikrotik_backup.pyThe script will:
- Create backup directories if they don't exist
- Connect to each router in the configuration
- Create and download both binary and plaintext backups
- Clean up old backups based on the specified retention period
- Complete system backup
- Includes all router settings
- Version-specific (can only be restored on similar RouterOS versions)
- Created using
/system backup save
- Human-readable configuration script
- Portable between different RouterOS versions
- Can be manually edited if needed
- Created using
/export
The script includes comprehensive error handling for:
- SSH connection failures
- Backup creation errors
- File transfer issues
- Cleanup process errors
- Store credentials securely
- Use strong passwords
- Consider using SSH keys instead of passwords
- Restrict SSH access to specific IP addresses
- Keep backup files in a secure location
- Regular testing of backup files
- Monitoring of disk space usage
- Verification of backup integrity
- Review of backup retention period
-
SSH Connection Failed
- Verify router IP address and credentials
- Check SSH service status on router
- Verify network connectivity
-
Permission Denied
- Check user permissions on router
- Verify credentials in config.json
-
Backup Creation Failed
- Check available storage on router
- Verify user permissions
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- MikroTik for RouterOS documentation
- Paramiko SSH library developers
- Community contributors
- 1.0.0
- Initial release
- Basic backup functionality
- Automatic cleanup
- Multi-router support
- Add SSH key authentication support
- Implement backup verification
- Add email notifications
- Add compression for long-term storage
- Create backup restoration script
- Add logging functionality
For support, please open an issue in the GitHub repository or contact the maintainers.
This tool is provided as-is with no warranty. Always test backups regularly and maintain multiple copies of critical configurations.