Python script to backup all git repos

With 20+ active repos, it's a pain to do Git pushes manually.

Python script approach

Simple script to automate the process.

import os
import subprocess
from datetime import datetime

# for variables privacy
from dotenv import load_dotenv
load_dotenv()

# Main folder with each project as a subfolder
projects_folder = os.getenv("projects_folder")

# Folders/projects to ignore
blacklist = tuple(os.getenv("PROJECTS_BLACKLIST").split(","))
print(f"\n{blacklist=}\n")

count_project = 0

# Get list of subfolders paths
def list_subfolders(path):
    subfolders = sorted([os.path.join(path, name) for name in os.listdir(path)
                if os.path.isdir(os.path.join(path, name))])
    return subfolders

def git_push(folder_path):
    os.chdir(folder_path)
    print(f"working directory: {os.getcwd()}")
    subprocess.run(["git", "add", "."])
    current_datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    subprocess.run(["git", "commit", "-m", f"{current_datetime} script-triggered backup"])
    subprocess.run(["git", "push", "origin", "main"])
    print("\n\n\n")

for path in list_subfolders(projects_folder):

    if not any(ele in path for ele in blacklist):

        count_project += 1

        print(f"\n\n\nProject #{count_project}: {path}")

        git_push(path)

Shell script approach

First approach was actually a Python script to print the commands for a shell script:

# 230312 Print shell commands for backup_all_projects.sh

import os

# for variables privacy
from dotenv import load_dotenv
load_dotenv()

print()

path = os.getenv("projects_folder")

# Folders/projects to ignore
blacklist = tuple(os.getenv("PROJECTS_BLACKLIST").split(","))
print(f"\n{blacklist=}\n")

def list_subfolders(path):
    subfolders = sorted([os.path.join(path, name) for name in os.listdir(path)
                if os.path.isdir(os.path.join(path, name))])
    return subfolders

print('echo -e "\\n\\n\\n"')
print('CURRENTDATETIME=`date +"%Y-%m-%d %T"`')

for p in list_subfolders(path):

    if not any(ele in p for ele in blacklist):

        print(f"cd {p}")
        print('echo "working directory: $(pwd)"')
        print('git add .')
        print('git commit -m $CURRENTDATETIME')
        print('git push origin main')
        print('echo -e "\\n\\n\\n"')
        print()

Used as:

#! /bin/zsh

[paste the output of the Python script here]

links

social