From 75048fd6350998884d3b0b4d84fe1b135cdd8ba2 Mon Sep 17 00:00:00 2001 From: Victor Giers Date: Fri, 27 May 2022 15:36:00 +0200 Subject: [PATCH] initial commit --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ backup_filelist.txt | 2 ++ backup_routine.sh | 30 ++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 README.md create mode 100644 backup_filelist.txt create mode 100644 backup_routine.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..a851794 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +This script checks if changes happened to a given list of files, and if so, it'll store them into a next version as back up. + +## Setup +Install git. +Create a folder that should acts as your automatic archive. +Open that folder with your terminal and execute ```git init``` +Create a file in the folder called ".gitignore" and put the following in it: +``` +backup_routine.sh +backup_filelist.txt +log.txt +``` +Create the files "backup_routine.sh" and "backup_filelist.txt" in this folder. +Copy the code from "backup_routine.sh" in this repository into yours. +Put the complete filepaths to the files you want to automatically be versioned into "backup_filelist.txt". +Now use ```git add *``` and ```git commit -m "initial commit"``` +You're done setting up your automatic archive. + +--- +## Automation +Make it so that "sh backup_routine.sh" gets called automatically. I recommend setting up a cron job, for example by executing ```EDITOR=nano crontab -e``` and writing the following into crontab: +``` +0,15,30,45 * * * * cd ~/Automatic\ Git\ Versioned\ Backups && sh backup_routine.sh +``` +This will run the script every 15 minutes. + +--- +## Restoration +Use +``` +git log -- filename.ext +``` +to view all the past versions. +Copy the commit-id of the version you wish to restore. +To recreate a past version, use +``` +git show commit-id:filename.ext > path/to/restoration/filename.restored.ext +``` +For example: ```git show 8f1dc7362dd5f6781ccf9ccc5a9cfdccb57ed709:ToDo.xlsx >> ToDo.restored.xlsx``` diff --git a/backup_filelist.txt b/backup_filelist.txt new file mode 100644 index 0000000..a3863b5 --- /dev/null +++ b/backup_filelist.txt @@ -0,0 +1,2 @@ +/Users/giers/Library/Mobile Documents/com~apple~CloudDocs/Electronic Art/Bestand.xlsx +/Users/giers/Library/Mobile Documents/com~apple~CloudDocs/Electronic Art/ToDo.xlsx diff --git a/backup_routine.sh b/backup_routine.sh new file mode 100644 index 0000000..762091d --- /dev/null +++ b/backup_routine.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +IFS=$'\r\n' GLOBIGNORE='*' command eval 'FILES=($(cat backup_filelist.txt))' + +for i in "${FILES[@]}" +do + FILENAME=$(basename "$i") + if [ -f "$FILENAME" ]; then + #echo "$FILENAME exists." + DIF=$(diff "$i" "$FILENAME") + if [ -z "$DIF" ] + then + #echo "$FILENAME has not changed, skipping." + else + #echo "$FILENAME has changed, copying..." + cp "$i" . + fi + else + #echo "$FILENAME does not exist, copying..." + cp "$i" . + fi +done + +if [[ `git status --porcelain` ]]; then + #echo "Git reports changes. Perfoming next generic commit." + git add * + git commit -m "next commit" +else + #echo "Git doesn't report any changes. No new commit. Exiting." +fi