Search code examples
bashgitshellgithubcron

Shell Script to backup Git Repo w/ crontab


My goal is to back up some markdown notes I keep in my /Users/<me>/codes/notes folder daily. I have a crontab job setup to run every minute to troubleshoot, but eventually I'd like this to be daily.

crontab job

* * * * * cd ~/codes/notes && ./backup_notes.sh

backup_notes.sh

#!/bin/sh

# create a timestamp alias for the commit message
timestamp() {
  date +"%Y-%m-%d @ %T"
}

# Go to my notes folder
cd /Users/<me>/codes/notes

# pull & push
if [[ `git status --porcelain` ]]; then
    git pull origin main
    git add .
    git commit -m "Update: $(timestamp)"
    git push origin main
fi

I believe the script by itself works because I can do:

❯ ./backup_notes.sh

And changes are pushed to the remote repository on github.

I also believe the crontab job is firing because if I add echo "hello world" > ./log.txt to the top of my backup_notes.sh I will get the expected hello world in ./log.txt

I'm pretty stuck and would appreciate it if anyone has any advice on how I can troubleshoot.

Things I've tried:

  • Replace git with /usr/local/bin/git
  • Remove the conditional
  • Double checked chmod 744 ~/codes/notes/backup_notes.sh
  • Added * * * * * cd ~/codes/notes && echo "hello world" > ./log.txt to verify that the crontab was working, and this will put hello world into log.txt.

I've also added some echos:

#!/bin/bash

# create a timestamp alias for the commit message
timestamp() {
  date +"%Y-%m-%d @ %T"
}

# Go to my notes 
cd /Users/jesse.spevack/codes/notes

echo "job started: $(timestamp)" > log.txt

# pull & push
if [[ `git status --porcelain` ]]; then
  echo "Running Git commands: $(timestamp)" >> log.txt
  git pull origin main
  git add .
  git commit -m "Update: $(timestamp)"
  git push origin main
  echo "Finished running Git commands: $(timestamp)" >> log.txt
fi

echo "job done: $(timestamp)" >> log.txt

Which adds the following to my log.txt:

job started: 2021-03-14 @ 09:12:00
Running Git commands: 2021-03-14 @ 09:12:00
Finished running Git commands: 2021-03-14 @ 09:12:01
job done: 2021-03-14 @ 09:12:01

However, there is no affect on the remote repository on github.


Solution

  • Run your script under a reduced environment, like

     env -i HOME=$HOME ./backup_notes.sh
    

    You might find that the script should set PATH or some other variable not found in a cron environment. To inspect the (quite limited) cron environment, place set at the beginning of your script.