Last active
November 25, 2015 03:14
-
-
Save malclocke/099be20175525b411d29 to your computer and use it in GitHub Desktop.
Requires `jq` binary
This file contains hidden or 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 | |
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