Skip to content

Instantly share code, notes, and snippets.

@mschuwalow
Last active June 30, 2016 17:22
Show Gist options
  • Save mschuwalow/8c5d24d82233c146c53131e36e394196 to your computer and use it in GitHub Desktop.
Save mschuwalow/8c5d24d82233c146c53131e36e394196 to your computer and use it in GitHub Desktop.
backup-gitlab.fish
set BACKUP_DIR /home/pi/backup/gitlab
set TOKEN
set PRUNE_AFTER 3
set TSTAMP (date "+%Y%m%d-%H%M")
set API https://gitlab.com/api/v3/projects/
function tgz
echo $argv
tar -zcf {$argv}.tar.gz $argv
rm -rf $argv
end
echo "=== INITIALIZING ==="
echo "Using backup directory $BACKUP_DIR"
mkdir -p $BACKUP_DIR
set REPOLIST (curl -H "PRIVATE-TOKEN: $TOKEN" https://gitlab.com/api/v3/projects/ | awk -v "ORS=," '{print}'| sed 's/,/\n/g' | grep "http_url_to_repo" | sed "s/\"http_url_to_repo\"://" | sed "s/https/http/" | sed 's/"//' | sed 's/"//')
for REPO in $REPOLIST
echo "Backing up $REPO"
set TEMP (echo $REPO | sed "s/http:.*\///" | sed "s/.git//")
git clone $REPO {$BACKUP_DIR}/{$TEMP}-{$TSTAMP}
tgz {$BACKUP_DIR}/{$TEMP}-{$TSTAMP}
end
echo "=== PRUNING ==="
echo "Pruning backup files $PRUNE_AFTER days old or older."
echo Found (find $BACKUP_DIR -name '*.tar.gz' -mtime +{$PRUNE_AFTER} | wc -l) files to prune.
rm -rf (find $BACKUP_DIR -name '*.tar.gz' -mtime +{$PRUNE_AFTER})
echo "=== DONE ==="
echo "GitHub backup completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment