Skip to content

Instantly share code, notes, and snippets.

@garethdigby
Forked from paultopia/backup-to-git.py
Created January 15, 2018 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garethdigby/ab7283a8ea097d9832fda20796e70134 to your computer and use it in GitHub Desktop.
Save garethdigby/ab7283a8ea097d9832fda20796e70134 to your computer and use it in GitHub Desktop.
quick python script to run to backup on an unversioned directory (like dropbox, icloud, where git would break) of files with given extension to git repo. Just modify the first three lines, and stick the target directory in a private github repo or something
#!/usr/bin/env python
target_dir = "FULL_PATH_TO_YOUR_TARGET_DIRECTORY_WITH_GIT_REPO" # example: /Users/you/github/backup/
source_dir = "FULL_PATH_TO_YOUR_TARGET_DIRECTORY_WITH_NO_GIT" # example: /Users/you/Dropbox/ImportantDocuments/
extensions = ["txt", "md"] # example list of extensions to copy, change to suit you.
import glob, shutil, subprocess, os
source_wildcards = [source_dir + "*." + x for x in extensions]
source_filenames = []
for wildcard in source_wildcards:
source_filenames += glob.glob(wildcard)
for name in source_filenames:
target = target_dir + name.rpartition("/")[-1]
shutil.copy(name, target)
os.chdir(target_dir)
subprocess.call("git add .", shell=True) # because I just CANNOT get subprocess to work without raw shell calls.
subprocess.call('git commit -am "update git backup"', shell=True)
subprocess.call("git push", shell=True)
# then stick this into a cron job or launchd or something, and blammo, you've got yourself an automated git backup and versioning
# of something that isn't going to place nice with git, like files in dropbox/icloud etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment