forked from archzfs/archzfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmirror.sh
executable file
·43 lines (34 loc) · 1.24 KB
/
mirror.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
set -euo pipefail
# Dependencies: jq, curl, bash, rsync
# Config settings
OUTDIR='./repo'
TMPDIR='./repo-tmp'
# End of config settings
rm -rf "${TMPDIR}"
mkdir -p "${OUTDIR}" "${TMPDIR}"
# TODO: Update to use the upstream repository
RELEASE_INFO="$(curl -s https://api.github.com/repos/archzfs/archzfs/releases/experimental)"
readarray -t FILE_INFO < <(echo "${RELEASE_INFO}" | jq '.assets | map(.browser_download_url + "|" + .updated_at) | join("\n")' -r)
for info in "${FILE_INFO[@]}"; do
url="${info%|*}"
tmp_filename="${TMPDIR}/$(basename "${url}")"
filename="${OUTDIR}/$(basename "${url}")"
updated_at_str="${info#*|}"
updated_at="$(date '+%s' --date "${updated_at_str}")"
# Calculate old file modified time
current_at='0'
if [ -f "${filename}" ]; then
current_at="$(date '+%s' -r "${filename}")"
fi
# Either download or copy pre-existing file
if [ "${updated_at}" -ne "${current_at}" ]; then
echo "Downloading ${filename}"
curl -L -o "${tmp_filename}" "${url}"
touch -h -d "${updated_at_str}" "${tmp_filename}"
else
echo "Skipping ${filename}"
cp -p "${filename}" "${tmp_filename}"
fi
done
rsync --delete -av "${TMPDIR}/" "${OUTDIR}/"