Difference between revisions of "How to setup a Alpine Linux mirror"

From Alpine Linux
Jump to: navigation, search
Line 10: Line 10:
Make sure that you have enough disk space.
Make sure that you have enough disk space.
Current (2019-01-24) disk usage in GB:
Current (2019-06-15) disk usage in GB:
Line 30: Line 30:
Line 40: Line 40:
Line 56: Line 56:
dest="$(mktemp -d)"
dest="$(mktemp -d)"
for dir in edge v2.4 v2.5 v2.6 v2.7 v3.0 v3.1 v3.2 v3.3 v3.4 v3.5 v3.6 v3.7 v3.8; do
for dir in edge v2.4 v2.5 v2.6 v2.7 v3.0 v3.1 v3.2 v3.3 v3.4 v3.5 v3.6 v3.7 v3.8 v3.9; do

Revision as of 13:24, 15 June 2019


This document describes how to set up an Alpine Linux mirror and make it available via http and rsync.

We will:

  • create the dir where we have the mirror
  • set up a cron job to sync with master mirror every hour
  • set up lighttpd for http access
  • set up rsync so other mirrors can rsync from you

Make sure that you have enough disk space.

Current (2019-06-15) disk usage in GB:

edge v2.4 v2.5 v2.6 v2.7 v3.0 v3.1 v3.2 v3.3 v3.4 v3.5 v3.6 v3.7 v3.8 v3.9 total
127.7 18.9 10.4 13.0 16.6 16.5 17.5 14.5 20.4 24.3 26.8 48.3 46.8 74.0 82.3 558.2

Script used to calculate the size:

#!/usr/bin/env bash

dest="$(mktemp -d)"

for dir in edge v2.4 v2.5 v2.6 v2.7 v3.0 v3.1 v3.2 v3.3 v3.4 v3.5 v3.6 v3.7 v3.8 v3.9; do
    size=`rsync -a -n --stats "$src" "$dest" | grep '^Total file size' | tr -d ',' | awk '{ print $4 }'`
    total=$(("$old_total" + "$size"))
    echo "$dir: $size" | awk '{ print $1 sprintf("%.1f", $2/1073741824) }'

echo "total: $total" | awk '{ print $1 sprintf("%.1f", $2/1073741824) }'
rm -r "$dest"

Setting up the cron job

Install rsync which will be used to sync from the master mirror.

apk add rsync

Save the following file as /etc/periodic/hourly/alpine-mirror

#!/usr/bin/env sh

# make sure we never run 2 rsync at the same time
if [ -z "$flock" ] ; then
  exec env flock=1 flock -n $lockfile "$0" "$@"


# uncomment this to exclude old v2.x branches
#exclude="--exclude v2.*"

mkdir -p "$dest"
/usr/bin/rsync \
        --archive \
        --update \
        --hard-links \
        --delete \
        --delete-after \
        --delay-updates \
        --timeout=600 \
        $exclude \
        "$src" "$dest"

(or use this script)

Make it executable:

chmod +x /etc/periodic/hourly/alpine-mirror

Now it will sync every hour. (given cron runs)

Setting up HTTP access via lighttpd

Install the lighttpd server

apk add lighttpd

Enable dir listings by uncommenting the following line in /etc/lighttpd/lighttpd.conf:

dir-listing.activate      = "enable"

Also set cache-control to force cache revalidate every 30 mins. Uncomment mod_setenv in /etc/lighttpd/lighttpd.conf:


Add also the following lines to /etc/lighttpd/lighttpd.conf:

setenv.add-response-header += (           
        "Cache-Control" => "must-revalidate"

Start lighttpd and make it start at boot:

rc-service lighttpd start rc-update add lighttpd

Note: You may wish to consider Darkhttpd as an alternative to Lighttpd

If so, simply install, start and auto-start the webserver:

apk add darkhttpd && rc-service darkhttpd start && rc-update add darkhttpd

Darkhttpd will, by default, offer directory listings and serve data from /var/www/localhost/htdocs/

See the main article on Darkhttpd for more configuration options

Setting up rsyncd

Add the following lines to /etc/rsyncd.conf:

        path = /var/www/localhost/htdocs/alpine
        comment = My Alpine Linux Mirror

Optionally set a bandwidth limit in /etc/conf.d/rsyncd. In this example we limit to 500Kbytes/s (approx 5Mbit/s)


Mirror statistics

Simple bandwidth statistics can be generated with vnstat.

apk add vnstat

edit /etc/vnstat.conf and replace the interface name with the appropriate one.

Start vnstatd

/etc/init.d/vnstatd start

copy the following script to /etc/periodic/15min/stats and make sure your crond is running. please not that heredoc should be tab indented or the script will fail. A working copy can be found here: http://tpaste.us/RrMv

#!/usr/bin/env sh


generate_index() {
    cat <<-EOF
    <!doctype html>
    <html lang="en">
        <meta charset="utf-8">
        <meta http-equiv="cache-control" content=no-cache">
        <meta http-equiv="refresh" content="3000">
        <title>Alpine Linux mirror statistics</title>
        <table border="0">
            <tr><td><img src="summary.png" alt="summary"></td><td><img src="hours.png" alt="hours"></td></tr>
            <tr><td rowspan="2"><img src="days.png" alt="days"></td><td><img src="top10.png" alt="top10"></td></tr>
            <tr><td><img src="months.png" alt="months"></td></tr>

if  [ ! -f "$output"/index.html ]; then
    mkdir -p $output
    generate_index > "$output"/index.html

for type in hours days months top10 summary hsummary vsummary; do
    vnstati --${type} -i $nic -o $output/${type}.png

Update mirror from mqtt

If you want your mirror to be really uptodate compared to our master mirror you can subscribe to Alpine Linux message server "msg.alpinelinux.org" and check for upload messages. Add mqtt-exec to be able to execute processes when specific topics are being send.

apk add mqtt-exec

mqtt-exec supports running multiple time so we need to setup a specific config.

ln -s /etc/init.d/mqtt-exec /etc/init.d/mqtt-exec.sync-mirror

ln -s /etc/conf.d/mqtt-exec /etc/conf.d/mqtt-exec.sync-mirror

edit /etc/conf.d/mqtt-exec.sync-mirror


Copy the following file to /usr/local/bin/sync-mirror and make it executable (dont forget to update the variables).



[ -z "$flock" ] && exec env flock=1 flock $lock $0 "$@"

if [ -n "$dir" ] && [ -d "$dest/${dir%/*}" ]; then
    logger "Syncing directory: $dir"
    logger "Syncing all directories"

/usr/bin/rsync \
    --archive \
    --update \
    --verbose \
    --progress \
    --timeout=600 \
    --delay-updates \
    --delete-after \
    "$src" \

And finally start mqtt-exec and let it listen on msg.alpinelinux.org

/etc/init.d/mqtt-exec.sync-mirror start

To make sure you are not missing any packages (in case something goes wrong with MQTT subscription) you can periodically sync all directories by adding the script to cron.

ln -s /usr/local/bin/sync-mirror /etc/periodic/hourly/sync-mirror

Now watch your syslog as it should tell you when it will update directories in your local mirror.