This Python script downloads a backup of all Slack users, all channels (including private channels), and all messages in those channels. The produced dump is in the same format produced by an official Slack export of workspace data (which works well for public channels but not private channels). The data dumps are then uploaded to a remote storage (AWS S3) location.
The intended use-case for preserving messages that are older than 90 days because of Slack's change in limits to the free plan.
-
Clone this repository.
-
Install Python 3 if needed.
-
pip install slack_sdk
-
Under OAuth & Permissions, in the User Token Scopes section (I've found the user token to work better than the bot token, but YMMV), add the following scopes:
admin
(not sure whether this is necessary)channels:history
channels:read
files:read
groups:history
groups:read
users:read
users:read.email
Also write down the Bot User OAuth Token. (These directions are based on this documentation.)
-
Under Install App, be sure to install the bot to the workspace you'd like to backup.
-
Assuming you're using the User Token, be sure that you have been added to all private channels that you want to backup. (Even admins/owners cannot see all private channels in the channel list; they need to be invited.) If you're using the Bot User Token, you probably need to invite the bot to all desired channels.
-
I recommend also running an official Slack export of workspace data. In JSON files containing file uploads, you'll see URLs ending with?t=xoxe-...
. Write down that token too. (This is a temporary file access token; I think it lasts 7 days.) -
Make sure you AWS credentials are up-to-date (you have write access to the remote storage)
-
Then I suggest creating a
run
script with the following contents:
#!/bin/sh
export TOKEN='xoxp-...' # Bot User OAuth Token
# Optional settings: (you can omit them)
export FILE_TOKEN='xoxe-...' # file access export token from previous step
export DOWNLOAD=1 # download all message files locally too
python slack_backup.py
-
Run the
run
script via./run
and wait. -
The output will be in a created
/tmp/backup
subdirectory and also upload the files to the remote storage. -
To produce a
backup.zip
file in the same format as a Slack export, do the following in a shell (assuming you havezip
installed):cd backup zip -9r ../backup.zip *
Please read this article if you don't know how to bundle up Python code for AWS Lambda functions.
In order to bundle up the code into a single archive that can then be deployed as Lambda function, run the following:
pip install -r requirements.txt --target packages
./bundle.sh
We use a different function name to be invoked, called main().
This will create a my-deployment-package.zip
file that can then be uploaded as function archive.
Note that AWS Lambda at the time of writing only supports Python 3.7 and 3.8.
This code was initially based on a
gist
by Benoit Courty.
The fork (original code) this repo is based on adds channel listing, user listing, file downloads, and
making the output format compatible with standard Slack dumps.
I, @michel-ds
, have further added an upload to remote storage feature.