Skip to content

Commit dd80b14

Browse files
authored
Merge pull request #1 from VolkanSah/v2
V2
2 parents 75886ec + abf7ca1 commit dd80b14

File tree

2 files changed

+135
-103
lines changed

2 files changed

+135
-103
lines changed

README.md

Lines changed: 45 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,97 @@
11

2-
# Debian System Backup and Restore Script
2+
# Debian System Backup and Restore Script (Version 2.0)
33

4-
This repository contains a shell script to back up and restore important system elements of a Debian-based system. This is useful for creating a rollback point before making significant changes to the system.
4+
This repository contains a 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.
55

66
## Features
77
- Backs up the list of installed packages.
88
- Backs up configuration files in `/etc`.
9-
- Optional: Backs up the entire filesystem (excluding the backup directory itself).
9+
- Optionally: Backs up the entire file system (with configurable exclusions).
1010
- Provides a restore function to revert the system to the saved state.
11+
- Logs all actions in detail.
1112

12-
## Prerequisites
13+
## Requirements
1314
- A Debian-based system (e.g., Debian, Ubuntu).
1415
- Sufficient disk space for the backup.
1516

1617
## Usage
1718

18-
### 1. Clone the Repository
19+
### 1. Clone the repository
1920
Clone this repository to your local machine:
2021
```sh
2122
git clone https://github.com/VolkanSah/Debian-System-Backup-and-Restore-Script
2223
cd Debian-System-Backup-and-Restore-Script
2324
```
2425

25-
### 2. Make the Script Executable
26-
Ensure the script has execute permissions:
26+
### 2. Make the script executable
27+
Ensure the script has execution permissions:
2728
```sh
2829
chmod +x backup_script.sh
2930
```
3031

31-
### 3. Run the Backup Script
32-
To create a backup, run the script without any arguments:
32+
### 3. Run the backup script
33+
To create a backup, run the script with the `backup` argument:
3334
```sh
34-
./backup_script.sh
35+
./backup_script.sh backup
3536
```
37+
For a full file system backup:
38+
```sh
39+
./backup_script.sh backup full
40+
```
41+
3642
This will:
37-
- Create a backup directory with a timestamp.
43+
- Create a timestamped backup directory.
3844
- Save the list of installed packages to `installed_packages.list`.
39-
- Compress and save configuration files from `/etc` to `etc_backup.tar.gz`.
40-
- Optionally, you can uncomment the lines in the script to back up the entire filesystem.
45+
- Compress and store configuration files from `/etc` into `etc_backup.tar.gz`.
46+
- Optionally back up the entire file system (if "full" is specified).
4147

42-
### 4. Restore from a Backup
48+
### 4. Restore from a backup
4349
To restore the system from a backup, run the script with the `restore` argument and the path to the backup directory:
4450
```sh
4551
./backup_script.sh restore /path/to/backup_directory
4652
```
53+
For a full system restore:
54+
```sh
55+
./backup_script.sh restore /path/to/backup_directory full
56+
```
57+
4758
This will:
4859
- Restore the list of installed packages.
4960
- Extract and restore configuration files from `etc_backup.tar.gz`.
50-
- Optionally, uncomment the lines to restore the entire filesystem.
61+
- Optionally restore the entire file system (if "full" is specified).
5162

5263
## Script Details
5364

54-
### Backup Script
55-
```bash
56-
#!/bin/bash
57-
58-
# Create backup directory
59-
BACKUP_DIR="/backup/$(date +%Y%m%d_%H%M%S)"
60-
mkdir -p "$BACKUP_DIR"
61-
62-
# Save the list of installed packages
63-
dpkg --get-selections > "$BACKUP_DIR/installed_packages.list"
64-
echo "Package list saved to $BACKUP_DIR/installed_packages.list"
65-
66-
# Backup configuration files
67-
tar czf "$BACKUP_DIR/etc_backup.tar.gz" /etc
68-
echo "Configuration files saved to $BACKUP_DIR/etc_backup.tar.gz"
69-
70-
# Optional: Backup the entire filesystem (can consume a lot of space)
71-
# tar czf "$BACKUP_DIR/full_backup.tar.gz" --exclude="$BACKUP_DIR" /
72-
73-
# Backup complete
74-
echo "Backup complete. All important files are saved in $BACKUP_DIR."
75-
76-
# Restore function
77-
restore() {
78-
BACKUP_DIR="$1"
79-
if [ -z "$BACKUP_DIR" ]; then
80-
echo "Please specify the backup directory."
81-
exit 1
82-
fi
83-
84-
# Restore the list of installed packages
85-
sudo dpkg --set-selections < "$BACKUP_DIR/installed_packages.list"
86-
sudo apt-get -y dselect-upgrade
87-
echo "Package list restored."
88-
89-
# Restore configuration files
90-
tar xzf "$BACKUP_DIR/etc_backup.tar.gz" -C /
91-
echo "Configuration files restored."
92-
93-
# Optional: Restore the entire filesystem
94-
# tar xzf "$BACKUP_DIR/full_backup.tar.gz" -C /
95-
}
96-
97-
# Automatic restore if an argument is provided
98-
if [ "$1" == "restore" ]; then
99-
restore "$2"
100-
fi
101-
```
65+
The updated script offers the following improvements:
66+
- Detailed logging of all actions.
67+
- Configurable options for important settings.
68+
- An exclusion list for full backups.
69+
- Improved structure with separate functions for backup and restore.
70+
- Flexible options for normal and full backup/restore.
71+
- Enhanced error handling and checks.
10272

10373
## Recommendations
104-
- **Error Handling**: Add error handling and checks for available disk space before proceeding with the backup.
105-
- **Notifications**: Integrate notifications (e.g., via email) to inform you upon successful backup or restoration.
74+
- **Error Handling**: The script now includes basic error handling. Consider adding further checks, such as for available disk space.
75+
- **Notifications**: Integrate notifications (e.g., via email) to inform you of successful backups or restores.
10676

10777
## License
108-
This project is licensed under the GPLv3 License. See the `LICENSE` file for details.
78+
This project is licensed under the GPLv3 license. See the `LICENSE` file for details.
10979

11080
## Contributing
111-
Contributions are welcome! Please open an issue or submit a pull request for any improvements or suggestions.
81+
Contributions are welcome! Please open an issue or submit a pull request for improvements or suggestions.
11282

11383
## Contact
114-
For any questions or support, please open an issue in the repository.
84+
For questions or support, please open an issue in the repository.
11585

116-
## Your Support
117-
If you find this project useful and want to support it, there are several ways to do so:
86+
## Supporting the Project
87+
If you find this project helpful and would like to support it, there are several ways to do so:
11888

119-
- If you find the white paper helpful, please it on GitHub. This helps make the project more visible and reach more people.
120-
- Become a Follower: If you're interested in updates and future improvements, please follow my GitHub account. This way you'll always stay up-to-date.
121-
- Learn more about my work: I invite you to check out all of my work on GitHub and visit my developer site https://volkansah.github.io. Here you will find detailed information about me and my projects.
89+
- If you find this script useful, please give it a ⭐ on GitHub. This helps make the project more visible and reach more people.
90+
- Follow me: If you're interested in updates and future improvements, please follow my GitHub account to stay informed.
91+
- 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.
12292
- 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.
123-
**If you appreciate my work and would like to support it, please visit my [GitHub Sponsor page](https://github.com/sponsors/volkansah). Any type of support is warmly welcomed and helps me to further improve and expand my work.**
93+
94+
**If you appreciate my work and would like to support it, please visit my [GitHub Sponsor page](https://github.com/sponsors/volkansah). Any kind of support is greatly appreciated and helps me improve and expand my work.**
12495

12596
Thank you for your support! ❤️
12697

backup_script.sh

Lines changed: 90 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,106 @@
11
#!/bin/bash
22

3-
# Create backup directory
3+
# Konfiguration
44
BACKUP_DIR="/backup/$(date +%Y%m%d_%H%M%S)"
5-
mkdir -p "$BACKUP_DIR"
5+
LOG_FILE="$BACKUP_DIR/backup.log"
6+
EXCLUDE_FILE="/tmp/backup_exclude.txt"
67

7-
# Save the list of installed packages
8-
dpkg --get-selections > "$BACKUP_DIR/installed_packages.list"
9-
echo "Package list saved to $BACKUP_DIR/installed_packages.list"
8+
# Funktion zum Loggen
9+
log() {
10+
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
11+
}
12+
13+
# Backup-Funktion
14+
backup() {
15+
mkdir -p "$BACKUP_DIR"
16+
log "Backup gestartet in $BACKUP_DIR"
1017

11-
# Backup configuration files
12-
tar czf "$BACKUP_DIR/etc_backup.tar.gz" /etc
13-
echo "Configuration files saved to $BACKUP_DIR/etc_backup.tar.gz"
18+
# Liste der installierten Pakete speichern
19+
dpkg --get-selections > "$BACKUP_DIR/installed_packages.list"
20+
log "Paketliste gespeichert in $BACKUP_DIR/installed_packages.list"
1421

15-
# Optional: Backup the entire filesystem (can consume a lot of space)
16-
# tar czf "$BACKUP_DIR/full_backup.tar.gz" --exclude="$BACKUP_DIR" /
22+
# Konfigurationsdateien sichern
23+
tar czf "$BACKUP_DIR/etc_backup.tar.gz" /etc
24+
log "Konfigurationsdateien gesichert in $BACKUP_DIR/etc_backup.tar.gz"
1725

18-
# Backup complete
19-
echo "Backup complete. All important files are saved in $BACKUP_DIR."
26+
# Ausschlussliste erstellen
27+
cat << EOF > "$EXCLUDE_FILE"
28+
/proc/*
29+
/sys/*
30+
/dev/*
31+
/tmp/*
32+
/run/*
33+
/mnt/*
34+
/media/*
35+
/lost+found
36+
/backup/*
37+
EOF
38+
39+
# Gesamtes Dateisystem sichern (optional)
40+
if [ "$FULL_BACKUP" = true ]; then
41+
log "Starte vollständiges Backup..."
42+
tar czf "$BACKUP_DIR/full_backup.tar.gz" --exclude-from="$EXCLUDE_FILE" /
43+
log "Vollständiges Backup abgeschlossen: $BACKUP_DIR/full_backup.tar.gz"
44+
fi
2045

21-
# Restore function
46+
log "Backup abgeschlossen."
47+
}
48+
49+
# Wiederherstellungsfunktion
2250
restore() {
23-
BACKUP_DIR="$1"
24-
if [ -z "$BACKUP_DIR" ]; then
25-
echo "Please specify the backup directory."
51+
local restore_dir="$1"
52+
if [ -z "$restore_dir" ]; then
53+
log "Bitte geben Sie das Backup-Verzeichnis an."
2654
exit 1
2755
fi
2856

29-
# Restore the list of installed packages
30-
sudo dpkg --set-selections < "$BACKUP_DIR/installed_packages.list"
31-
sudo apt-get -y dselect-upgrade
32-
echo "Package list restored."
57+
log "Starte Wiederherstellung aus $restore_dir"
3358

34-
# Restore configuration files
35-
tar xzf "$BACKUP_DIR/etc_backup.tar.gz" -C /
36-
echo "Configuration files restored."
59+
# Paketliste wiederherstellen
60+
if [ -f "$restore_dir/installed_packages.list" ]; then
61+
sudo dpkg --set-selections < "$restore_dir/installed_packages.list"
62+
sudo apt-get -y dselect-upgrade
63+
log "Paketliste wiederhergestellt."
64+
else
65+
log "Warnung: Paketliste nicht gefunden."
66+
fi
3767

38-
# Optional: Restore the entire filesystem
39-
# tar xzf "$BACKUP_DIR/full_backup.tar.gz" -C /
68+
# Konfigurationsdateien wiederherstellen
69+
if [ -f "$restore_dir/etc_backup.tar.gz" ]; then
70+
sudo tar xzf "$restore_dir/etc_backup.tar.gz" -C /
71+
log "Konfigurationsdateien wiederhergestellt."
72+
else
73+
log "Warnung: Backup der Konfigurationsdateien nicht gefunden."
74+
fi
75+
76+
# Gesamtes Dateisystem wiederherstellen (optional)
77+
if [ "$FULL_RESTORE" = true ] && [ -f "$restore_dir/full_backup.tar.gz" ]; then
78+
log "Starte vollständige Wiederherstellung..."
79+
sudo tar xzf "$restore_dir/full_backup.tar.gz" -C /
80+
log "Vollständige Wiederherstellung abgeschlossen."
81+
fi
82+
83+
log "Wiederherstellung abgeschlossen."
4084
}
4185

42-
# Automatic restore if an argument is provided
43-
if [ "$1" == "restore" ]; then
44-
restore "$2"
45-
fi
86+
# Hauptprogramm
87+
case "$1" in
88+
backup)
89+
FULL_BACKUP=false
90+
[ "$2" = "full" ] && FULL_BACKUP=true
91+
backup
92+
;;
93+
restore)
94+
FULL_RESTORE=false
95+
[ "$3" = "full" ] && FULL_RESTORE=true
96+
restore "$2"
97+
;;
98+
*)
99+
echo "Verwendung: $0 {backup|restore} [Optionen]"
100+
echo " backup [full] - Führt ein Backup durch (optional: vollständiges Backup)"
101+
echo " restore DIR [full] - Stellt aus dem angegebenen Verzeichnis wieder her"
102+
exit 1
103+
;;
104+
esac
105+
106+
exit 0

0 commit comments

Comments
 (0)