Last active
March 29, 2020 17:35
-
-
Save flandersen/6d33c609de80247c9059bf8c3e2ade05 to your computer and use it in GitHub Desktop.
Backing up Git repositories by creating bundles and syncing them to a remote server using Rsync.
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/bash | |
# Author: flandersen | |
# Date: 29.03.2020 | |
# https://gist.github.com/flandersen/6d33c609de80247c9059bf8c3e2ade05 | |
# | |
# Backups the repositories to a backup directory and syncronizes | |
# the backup dir to a backup server using rsync. | |
# | |
# Example usage: | |
# backup-git-repos.sh /srv/repos /var/backups/repos | |
if [ "$#" -ne 2 ]; then | |
echo "Usage: $0 <sourcedir> <backupdir>" | |
exit 1 | |
fi | |
sourcedir="$1" | |
backupdir="$2" | |
if [ ! -d "$sourcedir" ] | |
then | |
echo "Source dir $sourcedir does not exists." | |
exit 1 | |
fi | |
if [ ! -d "$backupdir" ] | |
then | |
echo "Backup dir $backupdir does not exists." | |
exit 1 | |
fi | |
cd "$sourcedir" | |
for dir in *.git; do | |
echo "Creating a bundle for repo $dir." | |
cd "$dir" | |
# getting refcount of the repository. | |
refcount=$(git show-ref | wc -l) | |
echo "Repo has $refcount ref(s)." | |
if [ $refcount -gt 0 ]; then | |
echo "---------------------------->" | |
bundlepath="$backupdir/$dir.bundle" | |
# create repo bundle. | |
echo "Creating git-bundle." | |
git bundle create "$bundlepath" --all | |
[ $? -eq 0 ] || exit 1 | |
else | |
echo "Skipping because it is empty." | |
fi | |
echo "<---------------------------" | |
cd .. | |
done | |
# Rsyncing the bundles to the backup server using ssh without RSA SSH-key. | |
rsync -avHPe "ssh -p8022" "$backupdir" -e "ssh -vi ~/.ssh/rsyncTest" rsync@backupsrv:/volume1/NetBackup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment