Created
July 4, 2014 21:46
-
-
Save miguelbaldi/a8346cd132b24acef93e to your computer and use it in GitHub Desktop.
Backup/restore script for debian packages
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 | |
# --------------------------------------------------------------------------- | |
# debian-backup - Tool for backup/restore debian packages | |
# Copyright 2013, Miguel A. Baldi Horlle ([email protected]) | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License at (http://www.gnu.org/licenses/) for | |
# more details. | |
# Defaults | |
_EXEC_MODE="" | |
_BKP_FOLDER="debian.bak" | |
usage() { | |
echo "Usage: $0 [-b [<BACKUP_FOLDER>]] | [-r [<BACKUP_FOLDER>]]" 1>&2; | |
echo "Examples" 1>&2; | |
echo "" 1>&2; | |
echo " Backup debian repositories and installed packages information" 1>&2; | |
echo " to default folder: $(pwd)/$_BKP_FOLDER" 1>&2; | |
echo " terminal>$0 -b" 1>&2; | |
echo "" 1>&2; | |
echo " Backup debian repositories and installed packages information" 1>&2; | |
echo " to folder: $(pwd)/mybackup" 1>&2; | |
echo " terminal>$0 -b mybackup" 1>&2; | |
echo "" 1>&2; | |
echo " Restore debian repositories and installed packages information" 1>&2; | |
echo " from default folder: $(pwd)/$_BKP_FOLDER" 1>&2; | |
echo " terminal>$0 -r" 1>&2; | |
echo "" 1>&2; | |
echo " Restore debian repositories and installed packages information" 1>&2; | |
echo " from folder: $(pwd)/mybackup" 1>&2; | |
echo " terminal>$0 -r mybackup" 1>&2; | |
exit 2 | |
} | |
check_tools() { | |
hash dpkg 2>/dev/null || { | |
echo >&2 "In order to run we require 'dpkg' but it's not installed. Aborting."; | |
exit 1; | |
} | |
hash apt-key 2>/dev/null || { | |
echo >&2 "In order to run we require 'apt-key' but it's not installed. Aborting."; | |
exit 1; | |
} | |
hash apt-get 2>/dev/null || { | |
echo >&2 "In order to run we require 'apt-get' but it's not installed. Aborting."; | |
exit 1; | |
} | |
hash sudo 2>/dev/null || { | |
echo >&2 "In order to run we require 'sudo' but it's not installed. Aborting."; | |
echo >&2 "Debian: apt-get install sudo"; | |
exit 1; | |
} | |
} | |
while getopts ":br" o; do | |
case "${o}" in | |
b) | |
if [ -n "$_EXEC_MODE" ]; then | |
echo "You can't specify both options at same time!" | |
else | |
_EXEC_MODE="backup" | |
fi | |
;; | |
r) | |
if [ -n "$_EXEC_MODE" ]; then | |
echo "You can't specify both options at same time!" | |
else | |
_EXEC_MODE="restore" | |
fi | |
;; | |
\?) | |
usage | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [ -z "$_EXEC_MODE" ]; then | |
echo "You must specify one of the preceding basic options!" | |
usage | |
exit 1 | |
fi | |
echo "[debian-backup] executing $_EXEC_MODE ..." | |
echo "[debian-backup] checking tools..." | |
check_tools | |
# Custom backup folder informed | |
if [ -n "$1" ]; then | |
_BKP_FOLDER="$1" | |
fi | |
echo "[debian-backup] using backup folder[$_BKP_FOLDER]" | |
echo "[debian-backup] in order to proceed, we need sudo here. Sorry" | |
echo "" | |
case "$_EXEC_MODE" in | |
backup) | |
mkdir -p $_BKP_FOLDER | |
echo "[debian-backup] backing up Debian package sources list..." | |
sudo cp -Rv /etc/apt/sources.list* $_BKP_FOLDER | |
echo "[debian-backup] backing up Debian installed packages list..." | |
dpkg --get-selections > $_BKP_FOLDER/package.list | |
echo "[debian-backup] backing up repositories GPG keys..." | |
sudo apt-key exportall > $_BKP_FOLDER/repo.keys | |
echo "[debian-backup] backup completed: " | |
cd $_BKP_FOLDER | |
echo "$(pwd)" | |
ls -lh | |
;; | |
restore) | |
if [ ! -d "$_BKP_FOLDER" ]; then | |
echo "Backup folder does not exist!" | |
exit 3 | |
fi | |
cd $_BKP_FOLDER | |
echo "[debian-backup] restoring Debian package sources list..." | |
sudo cp -Rv sources.list* /etc/apt/ | |
echo "[debian-backup] restoring repositories GPG keys..." | |
sudo apt-key add repo.keys | |
echo "[debian-backup] updating package information..." | |
sudo apt-get update | |
echo "[debian-backup] restoring Debian installed packages list..." | |
sudo dpkg --set-selections < package.list | |
echo "[debian-backup] installing Debian packages ..." | |
sudo apt-get install dselect | |
;; | |
esac | |
echo "done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment