backup-db.sh: include sshkeys in the backups
[girocco.git] / jobd / update.sh
bloba76549d3fa1fca1aca1994df53d09bc49106b4bf
1 #!/bin/sh
3 . @basedir@/shlib.sh
5 # date -R is linux-only, POSIX equivalent is '+%a, %d %b %Y %T %z'
6 datefmt='+%a, %d %b %Y %T %z'
8 # darcs fast-export | git fast-import with error handling
9 git_darcs_fetch() (
10 set_utf8_locale
11 _err1=
12 _err2=
13 exec 3>&1
14 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
16 exec 4>&3 3>&1 1>&4 4>&-
18 _e1=0
19 "$cfg_basedir"/bin/darcs-fast-export \
20 --export-marks="$(pwd)/dfe-marks" \
21 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
22 echo $_e1 >&3
23 } | \
25 _e2=0
26 git fast-import \
27 --export-marks="$(pwd)/gfi-marks" \
28 --export-pack-edges="$(pwd)/gfi-packs" \
29 --import-marks="$(pwd)/gfi-marks" \
30 --force 3>&- || _e2=$?
31 echo $_e2 >&3
34 EOT
35 exec 3>&-
36 [ "$_err1" = 0 -a "$_err2" = 0 ]
37 return $?
40 # bzr fast-export | git fast-import with error handling
41 git_bzr_fetch() (
42 set_utf8_locale
43 _err1=
44 _err2=
45 exec 3>&1
46 { read -r _err1 || :; read -r _err2 || :; } <<-EOT
48 exec 4>&3 3>&1 1>&4 4>&-
50 _e1=0
51 bzr fast-export \
52 --export-marks="$(pwd)/dfe-marks" \
53 --import-marks="$(pwd)/dfe-marks" "$1" 3>&- || _e1=$?
54 echo $_e1 >&3
55 } | \
57 _e2=0
58 git fast-import \
59 --export-marks="$(pwd)/gfi-marks" \
60 --export-pack-edges="$(pwd)/gfi-packs" \
61 --import-marks="$(pwd)/gfi-marks" \
62 --force 3>&- || _e2=$?
63 echo $_e2 >&3
66 EOT
67 exec 3>&-
68 [ "$_err1" = 0 -a "$_err2" = 0 ]
69 return $?
72 set -e
73 [ -n "$cfg_mirror" ] || { echo "Mirroring is disabled" >&2; exit 0; }
74 trap 'if [ $? != 0 ]; then echo "update failed dir: $PWD" >&2; fi; rm -f "$bang_log"' EXIT
76 umask 002
77 [ "$cfg_permission_control" != "Hooks" ] || umask 000
79 proj="$1"
80 cd "$cfg_reporoot/$proj.git"
82 if check_interval lastrefresh $cfg_min_mirror_interval; then
83 progress "= [$proj] update skip (last at $(config_get lastrefresh))"
84 exit 0
86 progress "+ [$proj] update (`date`)"
88 bang_setup
89 bang_once=1
90 bang_action="update"
92 mail="$(config_get owner || :)"
93 url="$(config_get baseurl || :)"
94 case "$url" in *" "*|*" "*|"")
95 bang_eval 'echo "Bad mirror URL (\"$url\")"; ! :'
96 exit 1
97 esac
98 bang echo "Mirroring from URL \"$url\""
99 bang echo ""
100 statusok="$(git config --bool gitweb.statusupdates 2>/dev/null || echo true)"
101 mailaddrs=''
102 [ "$statusok" = "false" -o -z "$mail" ] || mailaddrs="$mail"
103 [ -z "$cfg_admincc" -o "$cfg_admincc" = "0" -o -z "$cfg_admin" ] || \
104 if [ -z "$mailaddrs" ]; then mailaddrs="$cfg_admin"; else mailaddrs="$mailaddrs,$cfg_admin"; fi
106 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
107 bang_eval "LC_ALL=C sort -k1b,1 <.refs-temp >.refs-before"
109 case "$url" in
110 svn://* | svn+http://* | svn+https://*)
111 [ -n "$cfg_mirror_svn" ] || { echo "Mirroring svn is disabled" >&2; exit 0; }
112 # Update the git svn url to match baseurl but be cognizant of any
113 # needed prefix changes. See the comments in taskd/clone.sh about
114 # why we need to put up with a prefix in the first place.
115 svnurl="${url#svn+}"
116 svnurl="${svnurl%/}"
117 svnurlold="$(config_get svnurl || :)"
118 if [ "$svnurl" != "$svnurlold" ]; then
119 # We better already have an svn-remote.svn.fetch setting
120 bang test -n "$(git config --get-all svn-remote.svn.fetch || :)"
121 # the only way to truly know what the proper prefix is
122 # is to attempt a fresh git-svn init -s on the new url
123 rm -rf svn-new-url || :
124 # We require svn info to succeed on the URL otherwise it's
125 # simply not a valid URL and without using -s on the init it
126 # will not otherwise be tested until the fetch
127 bang eval 'svn --non-interactive info "$svnurl" > /dev/null'
128 bang mkdir svn-new-url
129 GIT_DIR=svn-new-url bang git init --bare --quiet
130 # We initially use -s for the init which will possibly shorten
131 # the URL. However, the shortening can fail if a password is
132 # not required for the longer version but is for the shorter,
133 # so try again without -s if the -s version fails.
134 cmdstr='git svn init --prefix "" -s "$svnurl" < /dev/null > /dev/null 2>&1 || '
135 cmdstr="$cmdstr"'git svn init --prefix "" "$svnurl" < /dev/null > /dev/null 2>&1'
136 GIT_DIR=svn-new-url bang eval "$cmdstr"
137 gitsvnurl="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.url || :)"
138 gitsvnfetch="$(GIT_DIR=svn-new-url git config --get svn-remote.svn.fetch || :)"
139 gitsvnprefixnew="${gitsvnfetch%%:*}"
140 gitsvnsuffixnew="${gitsvnprefixnew##*/}"
141 gitsvnprefixnew="${gitsvnprefixnew%$gitsvnsuffixnew}"
142 rm -rf svn-new-url || :
143 # Using GIT_DIR= with bang leaves it set to svn-new-url, so reset it to .
144 GIT_DIR=.
145 if [ "$gitsvnurl" != "$(git config --get svn-remote.svn.url || :)" ]; then
146 # The url has been changed.
147 # We must update the url and replace the prefix on all config items
148 gitsvnfetch="$(git config --get-all svn-remote.svn.fetch | head -1 || :)"
149 gitsvnprefixold="${gitsvnfetch%%:*}"
150 gitsvnsuffixold="${gitsvnprefixold##*/}"
151 gitsvnprefixold="${gitsvnprefixold%$gitsvnsuffixold}"
152 git config --remove-section 'svn-remote.svnnew' 2>/dev/null || :
153 git config 'svn-remote.svnnew.url' "$gitsvnurl"
154 { git config --get-regexp '^svn-remote\.svn\.' || :; } | \
155 { while read sname sval; do
156 case "$sname" in
157 svn-remote.svn.fetch|svn-remote.svn.branches|svn-remote.svn.tags)
158 sname="${sname#svn-remote.svn.}"
159 sval="${sval#$gitsvnprefixold}"
160 bang git config --add "svn-remote.svnnew.$sname" "${gitsvnprefixnew}$sval"
161 esac
162 done; }
163 bang git config -f svn/.metadata svn-remote.svn.reposRoot "$gitsvnurl"
164 bang git config --remove-section svn-remote.svn
165 bang git config --rename-section svn-remote.svnnew svn-remote.svn
167 bang config_set svnurl "$svnurl"
169 # remove any stale *.lock files greater than 1 hour old in case
170 # git-svn was killed on the last update because it took too long
171 find svn -type f -name '*.lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
172 GIT_DIR=. bang git svn fetch --quiet < /dev/null
173 # git svn does not preserve group permissions in the svn subdirectory
174 chmod -R ug+rw,o+r svn
175 # git svn also leaves behind ref turds that end with @nnn
176 # We get rid of them now
177 git show-ref | \
178 { while read sha1 ref; do
179 case "$ref" in
180 ?*@[1-9]|?*@[1-9][0-9]|?*@[1-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9]|\
181 ?*@[1-9][0-9][0-9][0-9][0-9]|?*@[1-9][0-9][0-9][0-9][0-9][0-9]|\
182 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9]|\
183 ?*@[1-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9])
184 git update-ref -d "$ref"
185 esac
186 done; }
188 darcs://*)
189 [ -n "$cfg_mirror_darcs" ] || { echo "Mirroring darcs is disabled" >&2; exit 0; }
190 httpurl="http://${url#darcs://}"
191 # remove any stale lock files greater than 1 hour old in case
192 # darcs_fast_export was killed on the last update because it took too long
193 find *.darcs -maxdepth 2 -type f -name 'lock' -mmin +60 -print0 2>/dev/null | xargs -0 rm -f
194 bang git_darcs_fetch "$httpurl"
196 bzr://*)
197 [ -n "$cfg_mirror_bzr" ] || { echo "Mirroring bzr is disabled" >&2; exit 0; }
198 bzrurl="${url#bzr://}"
199 bang git_bzr_fetch "$bzrurl"
201 hg+http://* | hg+https://*)
202 [ -n "$cfg_mirror_hg" ] || { echo "Mirroring hg is disabled" >&2; exit 0; }
203 # We just remove hg+ here, so hg+http://... becomes http://...
204 hgurl="${url#hg+}"
205 # Fetch any new updates
206 bang hg -R "$(pwd)/repo.hg" pull
207 # Do the fast-export | fast-import
208 bang git_hg_fetch
211 [ "$url" = "$(git config --get remote.origin.url || :)" ] || bang config_set_raw remote.origin.url "$url"
212 GIT_SSL_NO_VERIFY=1 bang git remote update
213 GIT_SSL_NO_VERIFY=1 bang git remote prune origin
215 esac
217 # The objects subdirectories permissions must be updated now.
218 # In the case of a dumb http clone, the permissions will not be correct
219 # (missing group write) despite the core.sharedrepository=1 setting!
220 # The objects themselves seem to have the correct permissions.
221 # This problem appears to have been fixed in the most recent git versions.
222 perms=g+w
223 [ "$cfg_permission_control" != "Hooks" ] || perms=go+w
224 chmod $perms $(find objects -maxdepth 1 -type d) 2>/dev/null || :
226 bang git update-server-info
227 bang config_set lastrefresh "$(date "$datefmt")"
229 # Look at which refs changed and trigger ref-change for these
230 bang_eval "git for-each-ref --format '%(refname) %(objectname)' >.refs-temp"
231 bang_eval "LC_ALL=C sort -k1b,1 <.refs-temp >.refs-after"
232 sockpath="$cfg_chroot/etc/taskd.socket"
233 if ! cmp -s .refs-before .refs-after; then
234 bang config_set lastreceive "$(date '+%a, %d %b %Y %T %z')"
235 if [ -S "$sockpath" ]; then
236 join -j 1 .refs-before .refs-after |
237 while read ref old new; do
238 [ "$old" != "$new" ] || continue
239 echo "ref-change %$proj% $proj $old $new $ref" | nc_openbsd -w 1 -U "$sockpath"
240 done
241 join -j 1 -v 1 .refs-before .refs-after |
242 while read ref old; do
243 echo "ref-change %$proj% $proj $old 0000000000000000000000000000000000000000 $ref" | \
244 nc_openbsd -w 1 -U "$sockpath"
245 done
246 join -j 1 -v 2 .refs-before .refs-after |
247 while read ref new; do
248 echo "ref-change %$proj% $proj 0000000000000000000000000000000000000000 $new $ref" | \
249 nc_openbsd -w 1 -U "$sockpath"
250 done
252 bang config_set lastchange "$(date '+%a, %d %b %Y %T %z')"
253 bang_eval "git for-each-ref --sort=-committerdate --format='%(committerdate:iso8601)' \
254 --count=1 refs/heads > info/lastactivity"
257 rm -f .refs-before .refs-after .refs-temp
259 if [ -e .banged ]; then
260 [ -z "$mailaddrs" ] ||
262 echo "$proj update succeeded - failure recovery"
263 echo "this status message may be disabled on the project admin page"
264 } | mail -s "[$cfg_name] $proj update succeeded" "$mailaddrs" || :
265 rm -f .banged .bangagain
268 progress "- [$proj] update (`date`)"