Last active
August 29, 2015 14:16
-
-
Save bhgraham/81b8f07c31f207485c85 to your computer and use it in GitHub Desktop.
Migrate all installed packages between debian based machines.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Author: Benjamin H. Graham ([email protected]) | |
# Copyright: 2015 Administr8 (http://administr8.me) | |
# License : GNU GPL v2 (http://www.gnu.org/licenses/gpl-2.0.html) | |
# dump packages to pastebin, return hash | |
# restore packages from pastebin with hash input | |
VERSION="1.0.0"; | |
SCRIPT="debmigrate"; | |
DESC="Dump currently installed packages to pastebin and install from a pastebin list by hash." | |
# Help text | |
if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "-?" ]; then | |
echo $DESC; | |
echo "Usage: `basename $0` -?|-h|--help (this screen)"; | |
echo "Usage: `basename $0` (backup)"; | |
echo "Usage: `basename $0` <hash> (restore)"; | |
exit 0; | |
fi; | |
# Functions | |
confirm () { | |
# call with a prompt string or use a default | |
# confirm <prompt string> && <command> | |
# confirm && <command> | |
# prompts to confirm prior to running given command | |
read -r -p "${1:-Are you sure? [y/N]} " response | |
case $response in | |
[yY][eE][sS]|[yY]) | |
true | |
;; | |
*) | |
false | |
;; | |
esac | |
} | |
jsonval () { | |
# json="<json>" prop="<property>" jsonval | |
# returns value of a json property given | |
local temp=$(echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $prop | cut -d":" -f2 | sed -e 's/^ *//g' -e 's/*$//g'); | |
echo ${temp##*|}; | |
} | |
postbin () { | |
# postbin <text> | |
# returns pastebin url | |
local text_in="${1}"; | |
json=$(curl -s -X POST --data-urlencode "pasteEnter=$text_in" http://a8.lc/bin/api); | |
prop='id' jsonval; | |
} | |
# Main Routine | |
if [ -z "$1" ]; then | |
# No argument supplied, creating new pastebin entry with currently installed packages. | |
installed_packages=$(dpkg --get-selections); | |
myid=$(postbin "$installed_packages"); | |
echo ' | |
Your packages list has been saved for restoration, but will expire in 7 days. | |
Verify the list accuracy by going to http://a8.lc/bin/'"$myid"' prior to restoring | |
To restore on the target machine, just run "debmigrate '"$myid"'" | |
'; | |
else | |
# Pastebin hash supplied as argument. | |
package_list=$(curl -s a8.lc/bin/"$1"@raw); | |
echo ''"$package_list"' | |
REQUIRES root or sudo, WARNING this will install the listed packages. | |
'; | |
# Check if currently root user, use sudo if not. | |
SUDO='' | |
if (( $EUID != 0 )); then | |
SUDO='sudo' | |
fi | |
# Set the selections to install. | |
echo "$package_list" | $SUDO dpkg --set-selections; | |
# Confirm and install packages. | |
confirm && $SUDO apt-get -y dselect-upgrade; | |
fi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment