3 # DO NOT EDIT - This file is being maintained by Chef
8 # Get the name of the file and the expected pattern
10 pattern="^osm-([0-9]{4})-([0-9]{2})-([0-9]{2})\.dmp$"
12 # Give up now if the file isn't a database dump
13 [[ $file =~ $pattern ]] || exit 0
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]}"
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"
25 rm /tmp/planetdump.lock
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
37 echo $$ > /tmp/planetdump.lock
39 # Define cleanup function
41 # Remove the lock file
42 rm /tmp/planetdump.lock
44 # Send an email with the output, since incron doesn't yet
45 # support doing this in the incrontab
46 if [[ -s "$logfile" ]]
48 mailx -s "Planet dump output: ${file}" zerebubuth@gmail.com < "${logfile}"
58 # Change to working directory
63 rm -rf changesets changeset_tags changeset_comments
64 rm -rf nodes node_tags
65 rm -rf ways way_tags way_nodes
66 rm -rf relations relation_tags relation_members
69 time nice -n 19 /opt/planet-dump-ng/planet-dump-ng \
71 -c "pbzip2 -c" -f "/store/backup/${file}" --dense-nodes=1 \
72 -C "changesets-${date}.osm.bz2" \
73 -D "discussions-${date}.osm.bz2" \
74 -x "planet-${date}.osm.bz2" -X "history-${date}.osm.bz2" \
75 -p "planet-${date}.osm.pbf" -P "history-${date}.osm.pbf"
77 # Function to create bittorrent files
83 web_dir="${dir}${s_year}"
84 name="${type}-${date}.osm.${format}"
85 web_path="${web_dir}/${name}"
86 rss_web_dir="https://planet.openstreetmap.org/${dir}"
87 rss_file="${type}-${format}-rss.xml"
88 torrent_file="${name}.torrent"
89 torrent_url="${rss_web_dir}${s_year}/${torrent_file}"
91 # create .torrent file
92 mktorrent -l 22 "${name}" \
93 -a udp://tracker.opentrackr.org:1337 \
94 -a udp://tracker.datacenterlight.ch:6969/announce,http://tracker.datacenterlight.ch:6969/announce \
95 -a udp://tracker.torrent.eu.org:451 \
96 -a udp://tracker-udp.gbitt.info:80/announce,http://tracker.gbitt.info/announce,https://tracker.gbitt.info/announce \
97 -a http://retracker.local/announce \
98 -w "https://planet.openstreetmap.org/${web_path}" \
99 -w "https://ftp5.gwdg.de/pub/misc/openstreetmap/planet.openstreetmap.org/${web_path}" \
100 -w "https://ftpmirror.your.org/pub/openstreetmap/${web_path}" \
101 -w "https://mirror.init7.net/openstreetmap/${web_path}" \
102 -w "https://free.nchc.org.tw/osm.planet/${web_path}" \
103 -w "https://ftp.fau.de/osm-planet/${web_path}" \
104 -w "https://ftp.spline.de/pub/openstreetmap/${web_path}" \
105 -w "https://osm.openarchive.site/${name}" \
106 -w "https://downloads.opencagedata.com/planet/${name}" \
107 -w "https://planet.osm-hr.org/${web_path}" \
108 -w "https://planet.maps.mail.ru/${web_path}" \
109 -c "OpenStreetMap ${type} data export, licensed under https://opendatacommons.org/licenses/odbl/ by OpenStreetMap contributors" \
110 -o "${torrent_file}" > /dev/null
112 # create .xml global RSS headers if missing
113 torrent_time_rfc="$(date -R -r ${torrent_file})"
114 test -f "${rss_file}" || echo "<x/>" | xmlstarlet select --xml-decl --indent \
115 -N "atom=http://www.w3.org/2005/Atom" \
116 -N "dcterms=http://purl.org/dc/terms/" \
117 -N "content=http://purl.org/rss/1.0/modules/content/" \
122 --attr "version" --output "2.0" --break \
123 --attr "atom:DUMMY" --break \
125 --elem "title" --output "OpenStreetMap ${type} ${format} torrent RSS" --break \
126 --elem "link" --output "${rss_web_dir}" --break \
128 --attr "href" --output "${rss_web_dir}/${rss_file}" --break \
129 --attr "rel" --output "self" --break \
130 --attr "type" --output "application/rss+xml" --break \
132 --elem "description" --output "${type}.osm.${format}.torrent RSS feed" --break \
133 --elem "copyright" --output "Source: OpenStreetMap contributors, under ODbL 1.0 licence" --break \
134 --elem "generator" --output "OpenStreetMap xmlstarlet powered shell script v1.0" --break \
135 --elem "language" --output "en" --break \
136 --elem "lastBuildDate" --output "${torrent_time_rfc}" \
139 # add newly created .torrent file as new entry to .xml RSS feed, removing excess entries
140 torrent_size="$(stat --format="%s" ${torrent_file})"
141 xmlstarlet edit --inplace \
142 -a "//lastBuildDate" -t elem -n item -v "" \
143 -s "//item[1]" -t elem -n "title" -v "${torrent_file}" \
144 -s "//item[1]" -t elem -n "guid" -v "${torrent_url}" \
145 -s "//item[1]" -t elem -n "link" -v "${torrent_url}" \
146 -s "//item[1]" -t elem -n "pubDate" -v "${torrent_time_rfc}" \
147 -s "//item[1]" -t elem -n "category" -v "OpenStreetMap data" \
148 -s "//item[1]" -t elem -n "enclosure" \
149 -s "//item[1]"/enclosure -t attr -n "type" -v "application/x-bittorrent" \
150 -s "//item[1]"/enclosure -t attr -n "length" -v "${torrent_size}" \
151 -s "//item[1]"/enclosure -t attr -n "url" -v "${torrent_url}" \
152 -s "//item[1]" -t elem -n "description" -v "OpenStreetMap torrent ${torrent_file}" \
153 -u /rss/channel/lastBuildDate -v "${torrent_time_rfc}" \
154 -d /rss/@atom:DUMMY \
155 -d "//item[position()>5]" \
159 # Function to install a dump in place
160 function install_dump {
165 name="${type}-${date}.osm.${format}"
166 latest="${type}-latest.osm.${format}"
167 rss_file="${type}-${format}-rss.xml"
169 md5sum "${name}" > "${name}.md5"
170 mkdir -p "${dir}/${year}"
171 mv "${name}" "${name}.md5" "${dir}/${year}"
172 ln -sf "${year:-.}/${name}" "${dir}/${latest}"
173 test -f "${name}.torrent" && mv "${name}.torrent" "${dir}/${year}" && ln -sf "${year:-.}/${name}.torrent" "${dir}/${latest}.torrent"
174 test -f "${rss_file}" && xmllint --noout "${rss_file}" && cp -f "${rss_file}" "${dir}"
175 rm -f "${dir}/${latest}.md5"
176 sed -e "s/${name}/${latest}/" "${dir}/${year}/${name}.md5" > "${dir}/${latest}.md5"
179 # Create *.torrent files
180 mk_torrent "changesets" "bz2" "planet" "/${year}"
181 mk_torrent "discussions" "bz2" "planet" "/${year}"
182 mk_torrent "planet" "bz2" "planet" "/${year}"
183 mk_torrent "history" "bz2" "planet/full-history" "/${year}"
184 mk_torrent "planet" "pbf" "pbf"
185 mk_torrent "history" "pbf" "pbf/full-history"
187 # Move dumps into place
188 install_dump "changesets" "bz2" "<%= node[:planet][:dump][:xml_directory] %>" "${year}"
189 install_dump "discussions" "bz2" "<%= node[:planet][:dump][:xml_directory] %>" "${year}"
190 install_dump "planet" "bz2" "<%= node[:planet][:dump][:xml_directory] %>" "${year}"
191 install_dump "history" "bz2" "<%= node[:planet][:dump][:xml_history_directory] %>" "${year}"
192 install_dump "planet" "pbf" "<%= node[:planet][:dump][:pbf_directory] %>"
193 install_dump "history" "pbf" "<%= node[:planet][:dump][:pbf_history_directory] %>"
195 # Remove pbf dumps older than 90 days
196 find "<%= node[:planet][:dump][:pbf_directory] %>" "<%= node[:planet][:dump][:pbf_history_directory] %>" \
197 -maxdepth 1 -mindepth 1 -type f -mtime +90 \
199 -iname 'planet-*.pbf' \
200 -o -iname 'history-*.pbf' \
201 -o -iname 'planet-*.pbf.md5' \
202 -o -iname 'history-*.pbf.md5' \