Created
September 16, 2012 23:22
-
-
Save larzconwell/3734804 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
shopt -e nocaseglob | |
set -e | |
clear | |
# | |
# Variables | |
# | |
gitTmp=$(mktemp -d) | |
gitTmpUrls=$(mktemp) | |
backupPath="" | |
githubUsername="" | |
# | |
# Function declarations | |
# | |
# Exit with a message and status | |
die() { | |
echo -e "$1\n" | |
if [[ "$2" == "" ]]; then | |
exit 1 | |
else | |
exit "$2" | |
fi | |
} | |
# Main function | |
init() { | |
if [[ "$@" == "" ]]; then | |
die "Backup\n\nOptions:\n -b # Path to backup arcives to\n -u # Github username" 0 | |
fi | |
read -e -p "Are you sure you want to backup your system? " sure | |
if [[ "$sure" != "y" ]]; then | |
die "Okay, maybe next time." 0 | |
fi | |
# Check if dependencies are installed | |
if [[ ! -f $(which curl) ]]; then | |
die "Curl must be insalled to use this program." 1 | |
fi | |
if [[ ! -f $(which git) ]]; then | |
die "Git must be insalled to use this program." 1 | |
fi | |
# Parse given arguments | |
while getopts ":b:e:i:u:" optionName; do | |
case "$optionName" in | |
b) backupPath="$OPTARG";; # Path to backup archives to | |
u) githubUsername="$OPTARG";; # Github username | |
esac | |
done | |
shift $((OPTIND-1)) | |
# If no backup path is given then read it | |
if [[ "$backupPath" == "" ]]; then | |
read -e -p "Where would you like to store your backups? " backupPath | |
if [[ "$backupPath" == "" ]]; then | |
die "Backup path can't be empty." 1 | |
fi | |
fi | |
# Check if backup path exists | |
if [[ ! -d "$backupPath" ]]; then | |
die "Backup path $backupPath doesn't exist." 1 | |
fi | |
# If no github username is given then read it | |
if [[ "$githubUsername" == "" ]]; then | |
read -e -p "What's your Github username? " githubUsername | |
if [[ "$githubUsername" == "" ]]; then | |
die "Username can't be empty." 1 | |
fi | |
fi | |
# Delete old archives | |
rm -rf "$backupPath"/gitBackup.tar.xz | |
rm -rf "$backupPath"/packages.list | |
rm -rf "$backupPath"/homeBackup.tar.xz | |
# Get JSON repo data and parse out the URLs to the git tmp file | |
echo "Retrieving repo list for $githubUsername" | |
curl -# https://api.github.com/users/"$githubUsername"/repos | \ | |
grep -i "git_url" | \ | |
sed -e 's/^.*git:/git:/' -e 's/",//' >> "$gitTmpUrls" | |
echo "" | |
# Clone repos into the git tmp directory | |
cd "$gitTmp" | |
for url in $(cat "$gitTmpUrls"); do | |
echo "Cloning $url" | |
git clone -q "$url" | echo -e "Please wait...\n" | |
done | |
# Create a archive for the git repos in the backupPath directory | |
echo "Creating Git backup archive" | |
tar -cpJf "$backupPath"/gitBackup.tar.xz -C "$gitTmp" "$gitTmp" >> /dev/null 2>&1 | echo -e "Please wait...\n" | |
echo "Creating list of installed packages" | |
# TODO: Make this compatible with more than just Debian based systems | |
# Use something similar to what's being done in github.com/larzconwell/railsready | |
dpkg --get-selections > "$backupPath"/packages.list | |
echo "" | |
# Copy the raw /home directory to the backupPath directory | |
echo "Creating copy of raw home directory" | |
cp -r "/home" "$backupPath" | echo -e "Please wait...\n" | |
# Backup from into an archive excluding and including any given dirs/files | |
echo "Creating archive of home directory excluding the given paths" | |
tar -cpJf "$backupPath"/homeBackup.tar.xz -C "/home" \ | |
"/home" >> /dev/null 2>&1 | echo -e "Please wait...\n" | |
# Clean up created temp files | |
rm -rf "$gitTmp" | |
rm -rf "$gitTmpUrls" | |
read -e -p "Would you like to shutdown? " -t 35 shutdown | |
if [[ "$shutdown" == "y" ]]; then | |
sudo shutdown -h now | |
fi | |
} | |
init "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment