Files
apt_rollback/reverse_dpkg.sh

50 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
# Usage: ./reverse_dpkg.sh <dpkg.log_file> [--run]
if [ -z "$1" ]; then
echo "Usage: $0 <dpkg.log_file> [--run]"
exit 1
fi
log_file="$1"
run_flag=false
# Check for --run flag
if [ "$2" == "--run" ]; then
run_flag=true
fi
remove_commands=""
install_commands=""
while IFS= read -r line; do
if [[ "$line" == *"remove"* ]]; then
package=$(echo "$line" | awk '{print $3}')
remove_commands="$remove_commands $package"
elif [[ "$line" == *"install"* ]]; then
package=$(echo "$line" | awk '{print $3}')
install_commands="$install_commands $package"
fi
done < "$log_file"
# Generate commands
apt_commands=""
if [ ! -z "$remove_commands" ]; then
apt_commands+="sudo apt remove $remove_commands\n"
fi
if [ ! -z "$install_commands" ]; then
apt_commands+="sudo apt install $install_commands\n"
fi
apt_commands+="sudo apt --fix-broken install\n"
apt_commands+="sudo apt autoremove\n"
# Output or run commands
if [ "$run_flag" == true ]; then
echo "Executing commands:"
echo "$apt_commands" | bash
else
echo "Generated commands:"
echo "$apt_commands"
fi