github-backupper/manual_based/gitBackupper.sh

63 lines
1.7 KiB
Bash
Executable File

#!/bin/bash
# File containing the list of Git repository URLs
REPO_FILE="gitRepos.txt"
# Directory where the repositories will be cloned
CLONE_DIR="repositories"
# Directory where the tar.gz files will be stored
ARCHIVE_DIR="archives"
# Create the directories if they don't exist
mkdir -p "$CLONE_DIR"
mkdir -p "$ARCHIVE_DIR"
# Function to convert HTTPS URL to SSH format
convert_to_ssh() {
local https_url="$1"
local ssh_url
# Remove the 'https://' part
ssh_url="git@${https_url#https://}"
# Replace '/' with ':' after 'github.com'
ssh_url="${ssh_url/github.com\//github.com:}"
# Append '.git' at the end
ssh_url="${ssh_url}.git"
echo "$ssh_url"
}
# Read each line from the file
while IFS= read -r repo_url
do
# Skip lines that start with '#'
[[ "$repo_url" =~ ^# ]] && continue
# Convert HTTPS URL to SSH URL
ssh_repo_url=$(convert_to_ssh "$repo_url")
# Extract the repo name from the URL
repo_name=$(basename "$repo_url" .git)
# Path to the local repository
repo_path="$CLONE_DIR/$repo_name"
# Check if the directory exists
if [ -d "$repo_path" ]; then
# If the repo is already cloned, fetch and update
echo "Updating repository: $repo_name"
git -C "$repo_path" fetch --all
git -C "$repo_path" pull
else
# If the repo is not cloned, clone it using SSH URL
echo "Cloning repository: $repo_name"
git clone "$ssh_repo_url" "$repo_path"
fi
# Create a tar.gz archive of the repository
tar -czf "$ARCHIVE_DIR/$repo_name.tar.gz" -C "$CLONE_DIR" "$repo_name"
echo "Archived repository: $repo_name to $repo_name.tar.gz"
done < "$REPO_FILE"