| 1 | #!/bin/bash |
| 2 | # Desc: Creates git bundles from git repos in current working directory, |
| 3 | # saving them to $outDir. |
| 4 | # Usage: bkBundleDir.sh |
| 5 | # Output: .bundle files in ./archive/ directory. |
| 6 | |
| 7 | #==BEGIN Define functions== |
| 8 | yell() { echo "$0: $*" >&2; } # Yell, Die, Try Three-Fingered Claw technique; # Ref/Attrib: https://stackoverflow.com/a/25515370 |
| 9 | die() { yell "$*"; exit 111; } |
| 10 | try() { "$@" || die "cannot $*"; } |
| 11 | dateTimeShort(){ |
| 12 | # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz) |
| 13 | # Usage: dateTimeShort ([str date]) |
| 14 | # Version 1.1.1 |
| 15 | # Input: arg1: 'date'-parsable timestamp string (optional) |
| 16 | # Output: stdout: timestamp (ISO-8601, no separators) |
| 17 | # Depends: yell |
| 18 | local argTime timeCurrent timeInput timeCurrentShort |
| 19 | |
| 20 | argTime="$1"; |
| 21 | # Get Current Time |
| 22 | timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second. |
| 23 | # Decide to parse current or supplied date |
| 24 | ## Check if time argument empty |
| 25 | if [[ -z "$argTime" ]]; then |
| 26 | ## T: Time argument empty, use current time |
| 27 | timeInput="$timeCurrent"; |
| 28 | else |
| 29 | ## F: Time argument exists, validate time |
| 30 | if date --date="$argTime" 1>/dev/null 2>&1; then |
| 31 | ### T: Time argument is valid; use it |
| 32 | timeInput="$argTime"; |
| 33 | else |
| 34 | ### F: Time argument not valid; exit |
| 35 | yell "ERROR:Invalid time argument supplied. Exiting."; exit 1; |
| 36 | fi |
| 37 | fi |
| 38 | # Construct and deliver separator-les date string |
| 39 | timeCurrentShort="$(date -d "$timeInput" +%Y%m%dT%H%M%S%z)"; |
| 40 | echo "$timeCurrentShort"; |
| 41 | } # Get YYYYmmddTHHMMSS±zzzz |
| 42 | #==END Define functions== |
| 43 | |
| 44 | |
| 45 | #==BEGIN Define static vars== |
| 46 | scriptStartDir="$(pwd)"; # Note current working directory (where git repos should be located) |
| 47 | outDir="$scriptStartDir/archive/"; # Specify where .bundle files should be written |
| 48 | #==END Define static vars== |
| 49 | |
| 50 | |
| 51 | #==BEGIN Create Git Bundles== |
| 52 | #===BEGIN Create output dir if necessary=== |
| 53 | if [ ! -d "$outDir" ]; then |
| 54 | mkdir -p "$outDir"; |
| 55 | yell "STATUS:Created output directory for bundles:$outDir"; |
| 56 | fi; |
| 57 | #===END Create output dir if necessary=== |
| 58 | |
| 59 | |
| 60 | #===BEGIN Loop through dirs=== |
| 61 | for dir in ./*/ ; do # cycle through dirs |
| 62 | ( |
| 63 | pushd "$dir" 1>/dev/null 2>&1; # Enter dir |
| 64 | if [ -d ./.git ]; # Check if git repo by seeing if `.git` subdir exists. |
| 65 | then |
| 66 | yell "DEBUG:========================"; |
| 67 | yell "DEBUG:dir:$dir"; |
| 68 | dirName="$(basename "$dir")"; |
| 69 | yell "DEBUG:dirName:$dirName"; |
| 70 | #git status -s; |
| 71 | outFileName="$(dateTimeShort)".."$dirName".bundle; |
| 72 | yell "DEBUG:outFileName:$outFileName"; |
| 73 | outFilePath="$outDir""$outFileName"; |
| 74 | yell "DEBUG:outFileName:$outFilePath"; |
| 75 | git bundle create "$outFilePath" --all; |
| 76 | if [ $? -eq 0 ]; then yell "STATUS:git bundle created at:$outFilePath"; fi; |
| 77 | else |
| 78 | yell "ERR:No .git dir: "$(pwd) |
| 79 | fi; |
| 80 | popd 1>/dev/null 2>&1; |
| 81 | ); |
| 82 | done |
| 83 | #===END Loop through dirs=== |
| 84 | #==END Create Git Bundles== |