Last active
February 25, 2024 03:01
-
-
Save tomdaley92/6f505ada3f3895fb3e23b629087cb749 to your computer and use it in GitHub Desktop.
Synology GitHub Backup Script
This file contains 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/sh | |
[ -z "$GITHUB_TOKEN" ] && echo "error: GITHUB_TOKEN required but not set" && exit 1 | |
[ "$#" -lt 2 ] && echo "usage: bash $0 USER|ORG PATH" && exit 1 | |
GITHUB_USER=$1 | |
BACKUP_DIR=$2/${GITHUB_USER} | |
GITHUB_API_BASE=https://api.github.com | |
GITHUB_REPOS_URL=$GITHUB_API_BASE/search/repositories?q=user:$GITHUB_USER | |
REPOS=$(curl -u ${GITHUB_USER}:${GITHUB_TOKEN} -s "${GITHUB_REPOS_URL}" | jq -r '.items[] | "\(.name),\(.full_name),\(.private),\(.html_url)"') | |
clone(){ | |
mkdir -p $BACKUP_DIR | |
for LINE in $REPOS | |
do | |
REPO_NAME=`echo ${LINE} | cut -d ',' -f1` | |
REPO_FULLNAME=`echo ${LINE} | cut -d ',' -f2` | |
PRIVATE=`echo ${LINE} | cut -d ',' -f3` | |
REPO_URL=`echo ${LINE} | cut -d ',' -f4` | |
CLONE_URL="https://"$(echo ${REPO_URL} | sed -E 's/^\s*.*:\/\//'"${GITHUB_USER}:${GITHUB_TOKEN}"'@/g') | |
CLONE_PATH="${BACKUP_DIR}/${REPO_NAME}" | |
TEMP_PATH=$CLONE_PATH".temp" | |
git clone $CLONE_URL $TEMP_PATH | |
rm -rf $CLONE_PATH | |
cp -R $TEMP_PATH/ $CLONE_PATH/ | |
rm -rf $TEMP_PATH | |
done | |
} | |
archive() { | |
NOW=$(date '+%Y-%m-%d_%H:%M:%S') | |
mkdir -p "${BACKUP_DIR}/tarballs_${NOW}" | |
for LINE in $REPOS | |
do | |
REPO_NAME=`echo ${LINE} | cut -d ',' -f1` | |
REPO_FULLNAME=`echo ${LINE} | cut -d ',' -f2` | |
PRIVATE=`echo ${LINE} | cut -d ',' -f3` | |
REPO_URL=`echo ${LINE} | cut -d ',' -f4` | |
ARCHIVE_URL="https://api.github.com/repos/$GITHUB_USER/$REPO_NAME/tarball" | |
ARCHIVE_PATH="${BACKUP_DIR}/tarballs_${NOW}/${REPO_NAME}.tar.gz" | |
echo $ARCHIVE_PATH | |
echo $ARCHIVE_URL | |
curl -u ${GITHUB_USER}:${GITHUB_TOKEN} -L ${ARCHIVE_URL} -o ${ARCHIVE_PATH} | |
done | |
# Remove archives (older than 7 days) | |
find $BACKUP_DIR -name "tarballs_*" -type d -mtime +7 -prune -exec rm -rf "{}" \; | |
} | |
clone | |
archive |
Here is what I place in my Synology Task Scheduler user-defined script content box
BACKUP_DIR="/volume1/GitHub"
SCRIPT_URL="https://gist.githubusercontent.com/tomdaley92/6f505ada3f3895fb3e23b629087cb749/raw/backup-github.sh"
wget -q -O $BACKUP_DIR/backup.sh $SCRIPT_URL
export GITHUB_TOKEN=some_secret_token
bash /volume1/GitHub/backup.sh tomdaley92 $BACKUP_DIR
bash /volume1/GitHub/backup.sh Diesel-Net $BACKUP_DIR
Some people have told me this is overkill. I usually point them to something like this heh.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: The bash for loop does not iterate over whitespace on MacOS for some reason, but this syntax should work with Synology DSM 6.2.4 which is a very weird stripped down flavor of linux.