Код: Выделить всё
import shutil
import subprocess
import os
import glob
HOME_DIR = '/home/joedoe/'
REPO = os.path.join(HOME_DIR, 'git/myrepo/')
os.environ['GIT_SSH_COMMAND'] = 'ssh -i //home/joedoe/.ssh/joedoe_gitlab_key'
# Define the files and folders to copy, using wildcards and extensions if needed
files_and_folders = [
'.bash*',
'py/*.py',
'tests*',
'bin'
]
def copy_to_repo(paths, destination):
for pattern in paths:
# Expand the pattern to match files
full_pattern = os.path.join(HOME_DIR, pattern)
for path in glob.glob(full_pattern):
dest_path = os.path.join(destination, os.path.basename(path))
if os.path.isdir(path):
shutil.copytree(path, dest_path, dirs_exist_ok=True)
else:
shutil.copy2(path, dest_path)
def git_commit_and_push(repo_path):
subprocess.run(['git', '-C', repo_path, 'add', '.'], check=True)
subprocess.run(['git', '-C', repo_path, 'commit', '-m', 'Automated backup'], check=True)
subprocess.run(['git', '-C', repo_path, 'push'], check=True)
def main():
# Copy files and folders
copy_to_repo(files_and_folders, REPO)
# Commit and push changes to Git
git_commit_and_push(REPO)
print("Files copied and changes pushed to GitHub.")
if __name__ == "__main__":
main()
< /code>
для запуска сценария каждый день в 10:30 с помощью Crontab (crontab -eКод: Выделить всё
30 10 * * * /usr/bin/python3 /home/joedoe/backup.pyесть более простое решение, чем это?
Подробнее здесь: https://stackoverflow.com/questions/794 ... les-in-git