Last active
March 25, 2016 21:21
-
-
Save mikecharles/11dcf4c9fbfc92bed4ed to your computer and use it in GitHub Desktop.
Backup an anaconda environment
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/sh | |
# Set usage function | |
usage() { | |
status=$1 | |
read -rd '' usage << EOM | |
Usage: [OPTIONS] SOURCE DEST | |
Backup the SOURCE anaconda environment to DEST | |
OPTIONS | |
-h Prints this help message | |
-f Force creating the destination environment even if it exists | |
EOM | |
printf "$usage\n"; | |
exit $status | |
} | |
# Get flags | |
force='false' | |
while getopts ':hf' option; do | |
case "$option" in | |
h) | |
usage 0 | |
exit | |
;; | |
f) | |
force='true' | |
;; | |
\?) printf "Invalid option: -%s\n" "$OPTARG" >&2 | |
usage 1 >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift $((OPTIND - 1)) | |
# Get positional args | |
[[ $# < 2 ]] && { echo $usage; exit 1; } | |
SOURCE=$1 | |
DEST=$2 | |
# Define some colors for printfs | |
RED='\e[0;31m' | |
GREEN='\e[0;32m' | |
YELLOW='\e[0;33m' | |
BLUE='\e[0;34m' | |
MAGENTA='\e[0;35m' | |
CYAN='\e[0;36m' | |
WHITE='\e[0;37m' | |
BLACK='\e[0;38m' | |
BOLDYELLOW='\e[1;33m' | |
NOCOLOR='\e[m' | |
# See if this environment already exists - remove it if the -f option was used | |
if [[ -d $DEST ]] ; then | |
if [[ "$force" == "true" ]] ; then | |
printf "${DEST} already exists, but since you used the ${YELLOW}-f${NOCOLOR} flag, ${RED}it will be removed${NOCOLOR}...\n" | |
rm -rf $DEST | |
else | |
printf "${DEST} already exists...\n" | |
exit 1 | |
fi | |
fi | |
printf "Backing up ${BLUE}${SOURCE}${NOCOLOR} to ${BLUE}${DEST}${NOCOLOR}...\n" | |
# Clone the source environment | |
conda create -qy --clone $SOURCE -p $DEST --copy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment