diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..34b504e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +repositories +gitRepos.txt diff --git a/gitBackuper.sh b/gitBackuper.sh new file mode 100755 index 0000000..e663af5 --- /dev/null +++ b/gitBackuper.sh @@ -0,0 +1,54 @@ +#!/bin/bash +# File containing the list of Git repository URLs +REPO_FILE="gitRepos.txt" + +# Directory where the repositories will be cloned +CLONE_DIR="repositories" + +# Create the directory if it doesn't exist +mkdir -p "$CLONE_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 +done < "$REPO_FILE" diff --git a/gitRepos.sample.txt b/gitRepos.sample.txt new file mode 100644 index 0000000..08f1ade --- /dev/null +++ b/gitRepos.sample.txt @@ -0,0 +1,7 @@ +# Comment Lines are accepted +# Own Repos - Private +https://github.com/zebrajr/cookieclicker +# Own Repos - Public +https://github.com/zebrajr/sysadmin +# Someone else Repos +https://github.com/zebrajr/zebrajr