Last active
December 11, 2015 18:08
-
-
Save brianmcallister/4638982 to your computer and use it in GitHub Desktop.
Bash script for archiving project directories. Learning bash, so this is very much a work in progress.
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
#!/bin/bash | |
# Archive old projects | |
# | |
# - Create a tar.gz file out of a folder | |
dir=~/Archive | |
# Echo a message. TODO - Colors. | |
function msg() { | |
echo "> $*" >&2 | |
} | |
# Prompt. | |
# | |
# $1 - The variable to save the prompt to. | |
# $2 - The message to prompt with. | |
function prompt() { | |
echo -n "$2" | |
read -e $1 | |
} | |
# Echo an error. TODO - Colors. | |
function error() { | |
msg $1 | |
exit 1 | |
} | |
# Check if the archive exists. | |
if [[ ! -d $dir ]]; then | |
mkdir $dir; | |
msg "Created the $dir directory."; | |
fi | |
prompt archivesource "Which directory do you want to archive?: " | |
archivesource=${archivesource%/} # Strip any trailing slashes. | |
# Check if the source directory exists. | |
if [[ ! -d $archivesource ]]; then | |
error "The $archivesource directory does not exist!" | |
fi | |
# Prompt for an archive name, default to the source directory name. | |
prompt archivename "Name? [$archivesource]: " | |
if [[ -z $archivename ]]; then archivename=$archivesource; fi | |
archivename=${archivename%/} # Strip any trailing slashes. | |
# Archive the directory and move it to the archive. | |
function archiveSource() { | |
msg "Workin..." | |
tar -pczf $archivename.tar.gz $archivesource | |
msg "Created the $archivename.tar.gz archive!" | |
mv $archivename.tar.gz $dir | |
msg "Moved $archivename.tar.gz to $dir!" | |
removeSource | |
} | |
# Remove the archive source directory. | |
function removeSource() { | |
prompt remove "Do you want to remove the $archivesource directory? [yes]: "; | |
if [[ $remove == '' ]]; then | |
remove='yes'; | |
fi | |
if [[ $remove == 'yes' ]]; then | |
msg "Workin..."; | |
rm -rf $archivesource; | |
msg "Removed the $archivesource directory!"; | |
fi | |
} | |
# Check if the archive already exists. | |
if [[ -e $dir'/'$archivename'.tar.gz' ]]; then | |
prompt overwrite "This archive already exists. Do you want to overwrite it? [yes]: " | |
if [[ $overwrite == "" ]]; then | |
overwrite="yes"; | |
fi | |
if [[ $overwrite == "yes" ]]; then | |
msg "Overwriting $dir/$archivename.tar.gz..." | |
archiveSource | |
else | |
error "Stopping. TODO - should prompt for a new name." | |
fi | |
else | |
archiveSource | |
fi | |
msg "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment