Skip to content

Instantly share code, notes, and snippets.

@tomdaley92
Last active February 25, 2024 03:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomdaley92/6f505ada3f3895fb3e23b629087cb749 to your computer and use it in GitHub Desktop.
Save tomdaley92/6f505ada3f3895fb3e23b629087cb749 to your computer and use it in GitHub Desktop.
Synology GitHub Backup Script
#!/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
@tomdaley92
Copy link
Author

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.

@tomdaley92
Copy link
Author

tomdaley92 commented Jul 20, 2021

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

@tomdaley92
Copy link
Author

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