Mirroing GitHub

As you may (or may not) know, I swichted a while back from self-hosted mercurial repositories to GitHub. While I'm happy with GitHub, I still want to have my favourites repositories available at hand (if/when GitHub is down).

Although there are already a lot of "backup script" on the net I wanted something very stupid and simple which integrate well with FreeBSD, so I wrote a periodic(8) script.

Show me the code

The script is straightforward. Once you've completed your /etc/rc.conf.local root is going to have a nice report in your "daily run output" e-mail.

It could be improved to also pull wiki and/or issues etc, if you come up with some code let me know !

/usr/local/etc/periodic/daily/999.git-pull

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/sh
#
# update (pull) some Git repos in a given directory.
#
# config example:
# daily_git_pull_enable="YES"
# daily_git_pull_user="alex"
# daily_git_pull_dir="/home/alex/github"

# If there is a global system configuration file, suck it in.
#
if [ -r /etc/defaults/periodic.conf ]
then
    . /etc/defaults/periodic.conf
    source_periodic_confs
fi

case "$daily_git_pull_enable" in
    [Yy][Ee][Ss])
        echo
        echo 'Git pull:'

        if [ ! -d "${daily_git_pull_dir}" ]; then
            echo "${daily_git_pull_dir}: not a directory"
            rc=2
        else
            rc=1
            for dir in "${daily_git_pull_dir}"/*; do
                if [ -d "${dir}/.git" ]; then
                    command="git -C ${dir} pull --all"
                    echo "$command"
                    su -l ${daily_git_pull_user} -c "$command" || rc=3
                fi
            done
        fi;;

    *)  rc=0;;
esac

exit $rc

Happy hacking !