Skip to content

Instantly share code, notes, and snippets.

@malclocke
Last active November 25, 2015 03:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save malclocke/099be20175525b411d29 to your computer and use it in GitHub Desktop.
Requires `jq` binary
#!/bin/bash
usage() {
cat <<EOT
Usage: PAPERTRAIL_API_TOKEN=abc123 $0 directory
Save all papertrail archives to outdir. Existing files will not be
overwritten if they have the correct filesize.
PAPERTRAIL_API_TOKEN can be found under the 'Me -> Profile' in Papertrail,
or from heroku config.
EOT
}
outdir=$1
if [ -z "$outdir" ] ; then
usage
exit 1
fi
archives_url=https://papertrailapp.com/api/v1/archives.json
if [ -z "$PAPERTRAIL_API_TOKEN" ] ; then
echo "PAPERTRAIL_API_TOKEN must be set"
usage
exit 1
fi
curl -s -H "X-Papertrail-Token: $PAPERTRAIL_API_TOKEN" $archives_url \
| jq -r '.[] | "\(.filename) \(.filesize) \(._links.download.href)"' \
| while read filename filesize href ; do
outfile=$outdir/$filename
echo -n "$outfile"
if [ -f "$outfile" ] ; then
if [ `stat -c "%s" "$outfile"` -eq $filesize ] ; then
echo " [SKIP]"
continue
else
echo " [OVERWRITE]"
fi
else
echo " [NEW]"
fi
curl -sL -H "X-Papertrail-Token: $PAPERTRAIL_API_TOKEN" -o $outfile $href
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment