Skip to content

Instantly share code, notes, and snippets.

@Hashbrown777
Last active February 17, 2020 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hashbrown777/1d75ec46e6c84a007683d4698f8d98c4 to your computer and use it in GitHub Desktop.
Save Hashbrown777/1d75ec46e6c84a007683d4698f8d98c4 to your computer and use it in GitHub Desktop.
performs releases from zips meant to overwrite live files, backs up & allows you to preview changes beforehand
#!/bin/bash
method="$1"
name="$2"
archives="$(dirname "${BASH_SOURCE[0]}")"
live=`pwd`
release="$archives/$name.zip"
backup="$archives/${name}_BAK.zip"
log="$live/released.txt"
mapfile -t files < <(zipinfo -1 "$release" 2>/dev/null | grep -v '/$' | sort)
if [ ${#files[@]} -eq 0 ]
then
echo "Release '$name' not found"
exit 1
fi
if [ -f "$backup" ]
then
echo "This had been released $(date -d "@$( stat -c '%Y' "$backup" )")"
mapfile -t saved < <(zipinfo -1 "$backup" 2>/dev/null | grep -v '/$' | sort)
else
echo 'This has not been released'
mapfile -t saved < <(
printf '%s\n' "${files[@]}" \
| while IFS= read -r file
do
[[ -f "$live/$file" ]] && echo "$file"
done
)
fi
echo 'Calculating modifications'
mapfile -t changed < <(
printf '%s\n' "${saved[@]}" \
| while IFS= read -r file
do
diff -q \
<(unzip -p "$release" "$file") \
<([[ -f "$backup" ]] && unzip -p "$backup" "$file" || cat "$live/$file") \
>/dev/null \
|| echo "$file"
done
)
if [ ${#changed[@]} -eq 0 ]
then
echo 'No changes!'
exit 1
fi
mapfile -t introduced < <(comm -23 <(printf '%s\n' "${files[@]}") <(printf '%s\n' "${saved[@]}"))
echo "You are attempting to $method"
(
echo "You are attempting to $method"
echo "Below are the files associated with $name"
echo
echo "Files introduced:"
printf '%s\n' "${introduced[@]}"
echo
echo "Changed files:"
printf '%s\n' "${changed[@]}"
#echo
#echo "Others:"
#comm -13 <(printf '%s\n' "${changed[@]}") <(printf '%s\n' "${saved[@]}")
) | less
read -p "Hit Ctrl-C if that didn't look right"
case "$method" in
release)
if [ -f "$backup" ]
then
echo 'This has already been backed up'
echo 'Please restore the backup before trying again'
exit 1
fi
echo "Backing up to '$backup'"
pushd "$live"
for file in "${files[@]}"
do
zip "$backup" "$file"
done
popd
echo "Releasing '$release' to '$live'"
for file in "${changed[@]}" "${introduced[@]}"
do
unzip -d "$live" -o "$release" "$file"
done
;;
restore)
if [ ! -f "$backup" ]
then
echo 'No backup has been made'
exit 1
fi
echo "Temporary backup at '$archives/tmp.zip'"
rm "$archives/tmp.zip"
pushd "$live"
for file in "${files[@]}"
do
zip "$archives/tmp.zip" "$file"
done
popd
echo "Restoring pre-$name @$(date -d "@$( stat -c '%Y' "$backup" )") to '$live'"
unzip -d "$live" -o "$backup"
read -p "Hit Ctrl-C if there are errors, otherwise we will remove the backup"
rm "$backup"
;;
#difference between backup and release
changed)
vimdiff -c 'set diffopt+=iwhite | set t_Co=0' \
<(for file in "${saved[@]}"
do
printf 'Archive: %s\n inflating: %s\n' "$release" "$file"; \
unzip -p "$release" "$file" \
| file -b --mime-encoding - \
| grep binary >/dev/null \
&& (unzip -p "$release" "$file" | sha512sum -b | cut -d' ' -f1) \
|| (unzip -p "$release" "$file"); \
echo
done
) \
<(for file in "${saved[@]}"
do
printf 'Archive: %s\n inflating: %s\n' "$backup" "$file"; \
unzip -p "$backup" "$file" \
| file -b --mime-encoding - \
| grep binary >/dev/null \
&& (unzip -p "$backup" "$file" | sha512sum -b | cut -d' ' -f1) \
|| (unzip -p "$backup" "$file"); \
echo
done
)
;;
#difference between live and release
preview)
vimdiff -c 'set diffopt+=iwhite | set t_Co=0' \
<(for file in "${changed[@]}"
do
printf 'Archive: %s\n inflating: %s\n' "$release" "$file"; \
unzip -p "$release" "$file" \
| file -b --mime-encoding - \
| grep binary >/dev/null \
&& (unzip -p "$release" "$file" | sha512sum -b | cut -d' ' -f1) \
|| (unzip -p "$release" "$file"); \
echo
done
) \
<(for file in "${changed[@]}"
do
printf 'Archive: %s\n inflating: %s\n' "$live" "$file"; \
file -b --mime-encoding "$live/$file" \
| grep binary >/dev/null \
&& (sha512sum -b "$live/$file" | cut -d' ' -f1) \
|| (cat "$live/$file"); \
echo
done
)
;;
#difference between live and backup
since)
vimdiff -c 'set diffopt+=iwhite | set t_Co=0' \
<(for file in "${saved[@]}"
do
printf 'Archive: %s\n inflating: %s\n' "$backup" "$file"; \
unzip -p "$backup" "$file" \
| file -b --mime-encoding - \
| grep binary >/dev/null \
&& (unzip -p "$backup" "$file" | sha512sum -b | cut -d' ' -f1) \
|| (unzip -p "$backup" "$file"); \
echo
done
) \
<(for file in "${saved[@]}"
do
printf 'Archive: %s\n inflating: %s\n' "$live" "$file"; \
file -b --mime-encoding "$live/$file" \
| grep binary >/dev/null \
&& (sha512sum -b "$live/$file" | cut -d' ' -f1) \
|| (cat "$live/$file"); \
echo
done
)
;;
*)
echo "'$method' unrecognised"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment