-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup_script.sh
45 lines (35 loc) · 1.28 KB
/
backup_script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
# Create backup directory
BACKUP_DIR="/backup/$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"
# Save the list of installed packages
dpkg --get-selections > "$BACKUP_DIR/installed_packages.list"
echo "Package list saved to $BACKUP_DIR/installed_packages.list"
# Backup configuration files
tar czf "$BACKUP_DIR/etc_backup.tar.gz" /etc
echo "Configuration files saved to $BACKUP_DIR/etc_backup.tar.gz"
# Optional: Backup the entire filesystem (can consume a lot of space)
# tar czf "$BACKUP_DIR/full_backup.tar.gz" --exclude="$BACKUP_DIR" /
# Backup complete
echo "Backup complete. All important files are saved in $BACKUP_DIR."
# Restore function
restore() {
BACKUP_DIR="$1"
if [ -z "$BACKUP_DIR" ]; then
echo "Please specify the backup directory."
exit 1
fi
# Restore the list of installed packages
sudo dpkg --set-selections < "$BACKUP_DIR/installed_packages.list"
sudo apt-get -y dselect-upgrade
echo "Package list restored."
# Restore configuration files
tar xzf "$BACKUP_DIR/etc_backup.tar.gz" -C /
echo "Configuration files restored."
# Optional: Restore the entire filesystem
# tar xzf "$BACKUP_DIR/full_backup.tar.gz" -C /
}
# Automatic restore if an argument is provided
if [ "$1" == "restore" ]; then
restore "$2"
fi