Some posts I write and immediately forget what I wrote. Others, seemingly boring to everyone else like this one, I look up every few months because otherwise I’d spend ages googling. That’s exactly why I write these notes. This is another one of those specialist entries that few will find interesting.
My goal: I want my tiny Raspberry Pi Zero to pull a Git repository daily via git pull, essentially as a backup. Two challenges stood in my way:
- The SSH passphrase, once entered, shouldn’t be asked for again. After all, I wanted to automate the daily pull.
I installed keychain for this:
sudo apt-get install keychain
The password, once entered, gets stored in the keychain. Add this to the end of ~/.bashrc (nano ~/.bashrc):
*# setup keychain - ssh-agent management keychain ~/.ssh/id_rsa . ~/.keychain/$HOSTNAME-sh
To set it up, the keychain needs to be cleared once:
keychain –clear
After that, you enter the passphrase one last time after “git pull”. Then it’s stored in the keychain.
- Automating Git Pull with crontab
With “crontab -e” you can set up the crontab. Enter this:
0 * * * su -s /bin/sh root -c ‘cd /YOUR GIT DIRECTORY/ && git pull origin master’
This makes your system pull the repository to your pi once per day via git pull.
How do you find the directory where your git repo lives? Go to that directory and type:
pwd
Mine is here: /home/pi/texts
So I entered:
0 * * * su -s /bin/sh root -c ‘cd /home/pi/texts/ && git pull origin master’
Is it working? Check if the cronjob runs without errors:
service cron status
First published in German at reinergaertner.de, where I’ve been at it since 1997. AI did the heavy lifting on the translation. I did the heavy squinting at the result.