Automating Github housekeeping on local Ubuntu machine

I am writing a book in Github, I was using Gitbook and decided to move it accross to Github for more GIT functionality and automated backups etc

If you already have a book in Gitbook (like me) you will need to transfer the git book to git hub. I am doing this manually with Git commands rather than relying on the web interface which I believe can import a Gitbook into Github etc.

Transfering an existing Gitbook to Github

git clone --bare https://git.gitbook.com/user/repos.git

cd repos.git

git push --mirror https://github.com/user/repos.git

cd ..

rm -rf repos.git

Now here comes the best part – the functionality/automated backups to a local machine and Dropbox ( I have some other code over on Github which transfers files to my Dropbox account using the Dropbox APIs ) https://github.com/tpmccallum/bitnami-wordpress-backup-to-dropbox

We can now clone the new Github repository to our local machine

git clone https://github.com/user/repos.git

If you generate a key on your local machine like this we can start automating communication between the systems

ssh-keygen

Copy the contents of the public key (which will now be in the /home/ubuntu/.ssh/id_rsa.pub file on your computer, if you are running Ubuntu like me) to Github (click on the gear icon in Github and select SSH keys from the menu).

Test the connection using the following command (use git@ not your username, this is deliberate)

ssh -T git@github.com

Change the Git URL to the SSH URL not the HTTP URL (or you could have simply cloned using the SSH option on your Github page)

git remote set-url origin git@github.com:user/repos.git

Now write some BASH and perform what ever Git commands you want to. I will be setting Cron jobs for example fetching changes on the hour and sending a backup to my Dropbox account daily etc.

My simple bash script is below, I did have a few issues getting it working using Cron …

I found that I had to re run ssh git@github.com in order for it to accept the key exchange on one of my private repositories

AND

For some strange reason the .git/FETCH_HEAD file was owned by root (perhaps because the repository is private. In my case I had to
sudo chown ubuntu:ubuntu repos/.git/FETCH_HEAD
on my local copy of the repository in order for it to work

#!/bin/bash

#just initializes private/public key
ssh git@github.com

#add for each repos which you want to update
string=”\n repos-name”
echo -e $string
cd /home/ubuntu/repos-name
git pull

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s