-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem_backup_and_restore
57 lines (55 loc) · 2.98 KB
/
system_backup_and_restore
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
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
# Backup List: Creates a backup of all installed packages into a file (packages.txt by default or custom path if specified).
# Reinstall Backup: Reinstalls the packages from the packages_list.txt file. If the list contains i386 packages, it will ensure the i386 architecture is added and updated.
function system_backup_and_restore() {
echo "Make a backup list of programs installed, or reinstall from a backup list?"
select packagelist in "Backup List" "Reinstall Backup";
do
case "$packagelist" in
"Backup List")
# Default file path
local file="${1:-./packages.txt}"
# Check if dpkg-query exists (APT is installed)
if ! command -v dpkg-query &> /dev/null; then
echo "Error: APT package manager not found. Ensure you're running this on a Debian-based system."
return 1
fi
# Get the list of installed packages using dpkg-query
local installed_packages
installed_packages=$(dpkg-query -f '${binary:Package}\n' -W)
# Backup the installed packages to the specified file
echo "Backing up installed packages to $file..."
echo "$installed_packages" > "$file"
echo "Package backup completed and saved to $file."
return
;;
"Reinstall Backup")
# Check if backup file exists
if [ ! -f "packages.txt" ]; then
echo "Error: packages.txt not found. Please ensure the backup file is available."
return 1
fi
# Ensure i386 architecture is supported, if necessary
if grep -q i386 packages.txt; then
echo "Found i386. Installing i386 architecture."
sudo dpkg --add-architecture i386
sudo apt update
fi
# Reinstall packages from the backup file
echo "Reinstalling packages from packages.txt..."
while IFS= read -r package; do
if [ -n "$package" ]; then
echo "Installing package: $package"
sudo apt install -y "$package" || echo "Failed to install $package"
fi
done < packages.txt
echo "Reinstallation completed."
return
;;
*)
echo "Invalid selection, please choose 'Backup List' or 'Reinstall Backup'."
return 1
;;
esac
done
}