Last active
October 17, 2021 14:23
-
-
Save pratikbin/f1939561e17ac5cb342b65cdb2959e39 to your computer and use it in GitHub Desktop.
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 -e | |
# | |
# Caching to persistence volume for self hosted ghar | |
[[ $DEBUG ]] && set -x | |
export PZSTD_NUM_THREADS=${PZSTD_NUM_THREADS:-"$(nproc)"} | |
[[ -z "${1}" ]] && echo "KEY is not set (arg 1)" >&2 && exit 2 | |
[[ -z "${2}" ]] && echo "FROM is not set (arg 2)" >&2 && exit 2 | |
[[ -z "${3}" ]] && echo "TO is not set (arg 3)" >&2 && exit 2 | |
key="${1}" | |
src="${2}" | |
src="${src%\/}" | |
des="${3}" | |
des="${des%\/}" | |
function setCache() { | |
echo "Checking exising cache..." | |
hit="$(find "${des}/" -maxdepth 1 -type f -name "${key}*" | head -n1 | xargs)" | |
if [[ "${hit}" ]]; then | |
echo "Cache hit for key: ${key}, not saving cache" | |
else | |
echo "Cache miss, Saving cache for key: ${key}" | |
time tar --use-compress-program=pzstd -cf "${des}/${key}.tar.zst" "${src}" | |
echo "Cache saved successfully" | |
echo "Cache saved from key: ${key}" | |
fi | |
} | |
function getCache() { | |
echo "Checking cache..." | |
hit="$(find "${des}/" -maxdepth 1 -type f -name "${key}*" | head -n1 | xargs)" | |
if [[ "${hit}" ]]; then | |
echo "Cache hit for key: ${key}, restoring..." | |
time tar --use-compress-program=pzstd -xf "${des}/${key}.tar.zst" | |
echo "Cache restored successfully" | |
echo "Cache restored from key: ${key}" | |
else | |
echo "Cache miss, Save cache for key: ${key} on post-cache step" | |
fi | |
} | |
main() { | |
mkdir -p "${des}/" | |
if [[ "${MODE}" ]]; then setCache; else getCache; fi | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment