Skip to content

Instantly share code, notes, and snippets.

@kijart
Forked from darekkay/trakt-backup.php
Last active May 10, 2024 23:19
Show Gist options
  • Save kijart/4974b7b61bcec092dc3de3433e6e00e2 to your computer and use it in GitHub Desktop.
Save kijart/4974b7b61bcec092dc3de3433e6e00e2 to your computer and use it in GitHub Desktop.
Trakt.tv backup bash script
#!/usr/bin/env bash
# Trakt backup script (note that user profile must be public)
# Trakt API documentation: http://docs.trakt.apiary.io
# Trakt client API key: http://docs.trakt.apiary.io/#introduction/create-an-app
set -e
# custom variables
API_KEY="CLIENT_API_KEY"
USERNAME="YOUR_USERNAME"
# global variables
API_URL="https://api.trakt.tv"
BACKUP_DIR="trakt-backup_$(date +"%Y-%m-%d_%H-%M-%S")"
# trak.tv endpoints
endpoints=(
watchlist/movies
watchlist/shows
watchlist/episodes
watchlist/seasons
ratings/movies
ratings/shows
ratings/episodes
ratings/seasons
collection/movies
collection/shows
watched/movies
watched/shows
history/movies
history/shows
)
echo -e "Starting backup..."
# create backup folder
mkdir -p ${BACKUP_DIR}
# Trakt requests
for endpoint in ${endpoints[*]}
do
filename="${USERNAME}-${endpoint//\//_}.json"
wget --quiet \
-O ${BACKUP_DIR}/${filename} \
--header "Content-Type: application/json" \
--header "trakt-api-key: ${API_KEY}" \
--header "trakt-api-version: 2" \
"${API_URL}/users/${USERNAME}/${endpoint}" \
&& echo -e "\e[32m${USERNAME}/${endpoint}\e[0m backup done" \
|| echo -e "\e[31m${USERNAME}/${endpoint}\e[0m request fail"
done
echo -e "Backup done\n"
# compress backup folder
echo -e "Compressing backup..."
tar -czvf ${BACKUP_DIR}.tar.gz ${BACKUP_DIR}
echo -e "Backup compressed: \e[32m${BACKUP_DIR}.tar.gz\e[0m\n"
# cleanup
rm -r ${BACKUP_DIR}
echo -e "Cleanup done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment