This repository contains an enhanced shell script for backing up and restoring key system elements of a Debian-based system. It is useful for creating a recovery point before making significant changes to the system.
- Backs up the list of installed packages with verification
- Backs up configuration files in
/etc
with compression - Optionally: Backs up the entire file system (with configurable exclusions)
- Provides a restore function to revert the system to the saved state
- Comprehensive logging with detailed error reporting
- New in v3.0: Enhanced error handling and validation
- New in v3.0: Permission checks and file size verification
- New in v3.0: Improved debugging capabilities
- A Debian-based system (e.g., Debian, Ubuntu)
- Sufficient disk space for the backup
- Write permissions to the backup directory (typically
/backup
) - Root privileges for system-wide operations
Clone this repository to your local machine:
git clone https://github.com/VolkanSah/Debian-System-Backup-and-Restore-Script
cd Debian-System-Backup-and-Restore-Script
Create and configure the backup directory:
sudo mkdir -p /backup
sudo chown $USER:$(id -gn) /backup
sudo chmod 755 /backup
Note: We use $USER:$(id -gn)
instead of $(whoami):$(whoami)
because:
$(whoami)
returns only the username, not the group$(id -gn)
returns the user's primary group name- Using
$(whoami):$(whoami)
can fail if the user and group have different names
Ensure the script has execution permissions:
chmod +x backup_script.sh
To create a backup of packages and configuration files:
./backup_script.sh backup
For a complete file system backup:
./backup_script.sh backup full
What happens during backup:
- Creates a timestamped backup directory in
/backup/YYYYMMDD_HHMMSS
- Saves the list of installed packages to
installed_packages.list
- Compresses and stores configuration files from
/etc
intoetc_backup.tar.gz
- Optionally backs up the entire file system (if "full" is specified)
- Generates detailed logs in
backup.log
To restore packages and configuration files from a backup:
./backup_script.sh restore /backup/YYYYMMDD_HHMMSS
For a complete system restore:
./backup_script.sh restore /backup/YYYYMMDD_HHMMSS full
What happens during restore:
- Restores the list of installed packages and installs missing ones
- Extracts and restores configuration files from
etc_backup.tar.gz
- Optionally restores the entire file system (if "full" is specified)
- Logs all restoration activities
If backup files are created but remain empty:
-
Check permissions:
ls -ld /backup sudo chown $USER:$(id -gn) /backup
-
Run with debug output:
bash -x ./backup_script.sh backup
-
Check log file:
tail -f /backup/YYYYMMDD_HHMMSS/backup.log
- Ensure you have write permissions to
/backup
- Run restore operations with
sudo
when needed - Check that the script is executable
The script now includes automatic verification:
- Checks if backup directory is writable
- Verifies file creation and sizes
- Logs detailed error messages
- Enhanced Error Handling:
check_command()
function validates each operation - Permission Validation: Checks directory permissions before operations
- File Verification: Confirms files are created and contain data
- Detailed Logging: Comprehensive logging with timestamps and file sizes
- Debug Support: Better error reporting and troubleshooting information
The script automatically excludes these directories from full backups:
/proc/*
- Process information/sys/*
- System information/dev/*
- Device files/tmp/*
- Temporary files/run/*
- Runtime data/mnt/*
- Mount points/media/*
- Removable media/lost+found
- File system recovery/backup/*
- Backup directory itself
- Test on a non-production system first
- Ensure adequate disk space (full backups can be large)
- Verify backup integrity after creation
- Document your backup schedule
- Clean old backups regularly to save disk space
- Test restore procedures periodically
- Monitor backup logs for errors or warnings
- Keep the script updated to the latest version
Add to crontab for automated backups:
# Daily backup at 2 AM
0 2 * * * /path/to/backup_script.sh backup >> /var/log/backup_cron.log 2>&1
# Weekly full backup on Sundays at 3 AM
0 3 * * 0 /path/to/backup_script.sh backup full >> /var/log/backup_cron.log 2>&1
Modify the BACKUP_DIR
variable in the script to use a different location:
BACKUP_DIR="/custom/backup/path/$(date +%Y%m%d_%H%M%S)"
- Added comprehensive error handling with
check_command()
function - Implemented permission checks for backup directory
- Added file size verification and logging
- Enhanced debugging capabilities with detailed error messages
- Improved log file structure with operation status tracking
- Added backup summary with file listings
- Detailed logging of all actions
- Configurable options for important settings
- An exclusion list for full backups
- Improved structure with separate functions for backup and restore
- Flexible options for normal and full backup/restore
- Basic error handling and checks
- Added full system backup capability
- Implemented restore functionality
- Added configuration file backup
- Initial release with basic package backup functionality
Feel free to submit issues, fork the repository, and create pull requests for any improvements.
This project is open source and available under the GPL-3.0 License.
If you encounter issues:
- Check the troubleshooting section above
- Review the log files for detailed error messages
- Open an issue on GitHub with your log files and system information
If you find this project helpful and would like to support it, there are several ways to do so:
- If you find this script useful, please give it a ⭐ on GitHub. This helps make the project more visible and reach more people.
- Follow me: If you're interested in updates and future improvements, please follow my GitHub account to stay informed.
- Learn more about my work: I invite you to view all my work on GitHub and visit my developer website at https://volkansah.github.io. There, you'll find detailed information about me and my projects.
- Share the project: If you know someone who could benefit from this project, please share it. The more people who can use it, the better.
If you appreciate my work and would like to support it, please visit my GitHub Sponsor page. Any kind of support is greatly appreciated and helps me improve and expand my work.
Thank you for your support! ❤️