Skip to content

Instantly share code, notes, and snippets.

@bhank
Created July 1, 2024 13:58
Show Gist options
  • Save bhank/efd6da295e64c562b1e1c036baf38852 to your computer and use it in GitHub Desktop.
Save bhank/efd6da295e64c562b1e1c036baf38852 to your computer and use it in GitHub Desktop.
slackdump wrapper to use in a cron job to download one month of Slack conversations and attachments
#!/bin/bash
# using bash for `printf -v` to zero-pad month
# slackdump: https://github.com/rusq/slackdump
# instructions for getting secrets to put in secrets.txt: https://github.com/rusq/slackdump/blob/master/doc/login-manual.rst
# Run it from a directory containing subdirectories named after slack workspaces, each containing a secrets.txt:
# getmonth.sh some-slack-workspace-name lastmonth
# getmonth.sh other-slack-workspace-name 2024 6
set -e
basedir=$(dirname "$0")
workspace=$1
if [ -z "$workspace" ]; then
echo "Pass slack workspace, then either 'lastmonth' or year and month."
exit
fi
workspacedir="$basedir/$workspace"
if [ ! -d "$workspacedir" ]; then
echo "Directory does not exist: $workspacedir"
exit
fi
if [ ! -f "$workspacedir/secrets.txt" ]; then
echo "Secrets file does not exist in $workspacedir"
exit
fi
if [ "$2" = "lastmonth" ]; then
first_day=$(date -u -d "`date +%Y%m01` -1 month" +%Y-%m-%dT%H:%M:%S)
last_day=$(date -u +%Y-%m-01T00:00:00)
# for filename:
year=$(date -u -d "$first_day" +"%Y")
month=$(date -u -d "$first_day" +"%m")
else
year="$2"
if [ -z "$3" ]; then
echo "Pass year and month."
exit
fi
# zero-pad month:
printf -v month "%02d" $3
first_day=$(date -u -d "`date +${year}${month}01`" +%Y-%m-%dT%H:%M:%S)
last_day=$(date -u -d "`date +${year}${month}01` +1 month" +%Y-%m-%dT%H:%M:%S)
fi
cachedir="$workspacedir/cache"
pushd "$workspacedir"
slackdump -w "$workspace" -cache-dir "$cachedir" -dump-from $first_day -dump-to $last_day -download -export-type standard -export slackdump-${year}-${month}.zip
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment