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.
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 $*"; }
12 # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz)
13 # Usage: dateTimeShort ([str date])
15 # Input: arg1: 'date'-parsable timestamp string (optional)
16 # Output: stdout: timestamp (ISO-8601, no separators)
18 local argTime timeCurrent timeInput timeCurrentShort
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";
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
34 ### F: Time argument not valid; exit
35 yell
"ERROR:Invalid time argument supplied. Exiting."; exit 1;
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==
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==
51 #==BEGIN Create Git Bundles==
52 #===BEGIN Create output dir if necessary===
53 if [ ! -d "$outDir" ]; then
55 yell
"STATUS:Created output directory for bundles:$outDir";
57 #===END Create output dir if necessary===
60 #===BEGIN Loop through dirs===
61 for dir
in .
/*/ ; do # cycle through dirs
63 pushd "$dir" 1>/dev
/null
2>&1; # Enter dir
64 if [ -d .
/.git
]; # Check if git repo by seeing if `.git` subdir exists.
66 yell
"DEBUG:========================";
67 yell
"DEBUG:dir:$dir";
68 dirName
="$(basename "$dir")";
69 yell
"DEBUG:dirName:$dirName";
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;
78 yell
"ERR:No .git dir: "$
(pwd)
80 popd 1>/dev
/null
2>&1;
83 #===END Loop through dirs===
84 #==END Create Git Bundles==