]> git.openstreetmap.org Git - chef.git/blob - cookbooks/planet/templates/default/planetdump.erb
dff206b35a2b0a9c271065711c850ffaa1aeda3c
[chef.git] / cookbooks / planet / templates / default / planetdump.erb
1 #!/bin/bash
2
3 # DO NOT EDIT - This file is being maintained by Chef
4
5 # Exit on error
6 set -e
7
8 # Get the name of the file and the expected pattern
9 file="$1"
10 pattern="^osm-([0-9]{4})-([0-9]{2})-([0-9]{2})\.dmp$"
11
12 # Give up now if the file isn't a database dump
13 [[ $file =~ $pattern ]] || exit 0
14
15 # Save the year and date from the file name
16 year="${BASH_REMATCH[1]}"
17 date="${year:2:2}${BASH_REMATCH[2]}${BASH_REMATCH[3]}"
18
19 # Check the lock
20 if [ -f /tmp/planetdump.lock ]; then
21     if [ "$(ps -p `cat /tmp/planetdump.lock` | wc -l)" -gt 1 ]; then
22         echo "Error: Another planetdump is running"
23         exit 1
24     else
25         rm /tmp/planetdump.lock
26     fi
27 fi
28
29 # Redirect this shell's output to a file. This is so that it
30 # can be emailed later, since this script is run from incron
31 # and incron doesn't yet support MAILTO like cron does. The
32 # command below appears to work in bash as well as dash.
33 logfile="/tmp/planetdump.log.$$"
34 exec > $logfile 2>&1
35
36 # Create Lock
37 echo $$ > /tmp/planetdump.lock
38
39 # Define cleanup function
40 function cleanup {
41     rm /tmp/planetdump.lock
42     # Send an email with the output, since incron doesn't yet
43     # support doing this in the incrontab.
44     /usr/bin/mailx -s "Planet dump output: ${file}" zerebubuth@gmail.com < $logfile
45     rm $logfile
46 }
47
48 # Remove lock on exit
49 trap cleanup EXIT
50
51 # Change to working directory
52 cd /store/planetdump
53
54 # Cleanup
55 rm -rf users
56 rm -rf changesets changeset_tags
57 rm -rf nodes node_tags
58 rm -rf ways way_tags way_nodes
59 rm -rf relations relation_tags relation_members
60
61 # Run the dump
62 time nice -n 19 /opt/planet-dump-ng/planet-dump-ng \
63      -c "pbzip2 -c" -f "/store/backup/${file}" --dense-nodes=1 \
64      -C "changesets-${date}.osm.bz2" \
65      -x "planet-${date}.osm.bz2" -X "history-${date}.osm.bz2" \
66      -p "planet-${date}.osm.pbf" -P "history-${date}.osm.pbf"
67
68 # Function to install a dump in place
69 function install_dump {
70   type="$1"
71   format="$2"
72   dir="$3"
73   year="$4"
74   name="${type}-${date}.osm.${format}"
75   latest="${type}-latest.osm.${format}"
76
77   md5sum "${name}" > "${name}.md5"
78   mkdir -p "${dir}/${year}"
79   mv "${name}" "${name}.md5" "${dir}/${year}"
80   ln -sf "${year:-.}/${name}" "${dir}/${latest}"
81   rm -f "${dir}/${latest}.md5"
82   sed -e "s/${name}/${latest}/" "${dir}/${year}/${name}.md5" > "${dir}/${latest}.md5"
83 }
84
85 # Move dumps into place
86 install_dump "changesets" "bz2" "<%= node[:planet][:dump][:xml_directory] %>" "${year}"
87 install_dump "planet" "bz2" "<%= node[:planet][:dump][:xml_directory] %>" "${year}"
88 install_dump "history" "bz2" "<%= node[:planet][:dump][:xml_history_directory] %>" "${year}"
89 install_dump "planet" "pbf" "<%= node[:planet][:dump][:pbf_directory] %>"
90 install_dump "history" "pbf" "<%= node[:planet][:dump][:pbf_history_directory] %>"