0

I'm planning to launch a server on latest OpenBSD. (Web, cloud and more.) I have to make regular backups of my ffs2 partitions (e. g. using cron).

But there are no documentation about this, only man for dump utility. As I understand, only unmounted devices can be dumped. (FreeBSD's dump has -L option, which allows to backup live filesystems, but OpenBSD lacks of this functionality.)

How could I dump mounted/live partitions? Maybe you propose using of another filesystem?

2 Answers 2

1

You can use dump on live partitions. rsnapshots (not on OpenBSD base, but available as a package) might also be useful.

As a general advice, plan your backups carefully. Databases (MySQL, etc) usually have their own backup tools and/or master-slave syncing, which might be helpful and usually makes restoring a lot easier. Mail backups depend a lot on how you/your users plan to use it (e.g. daily backups might not be of much aid if you want to recover an accidentally deleted message, and mail in general is a fast moving target (again at least Dovecot has IMAP master-slave sycning). It might not make sense to regularly backup versioned storage (e.g. git repositories), as they inherently "backup themselves", etc...

2
  • Thanks! How about filesystem safety? My assumption is that OpenBSD's dump just do dd on a partiiton. It is unsafe. FreeBSD's dump, on the other hand, do snapshot before any copying operation. Dec 31, 2022 at 16:23
  • You can't use dump for live paritions! It remounts it as R/O! Also, dump works at file-level, not as dd byte-level. May 21 at 15:05
0

The answer is use dump.

OpenBSD version of dump works for mounted filesystems without any flags, e. g. -L as for FreeBSD. (UPD: actually, no, see below)

Be sure, all read/write operations on live FS don't require locks, e. g. maildir (most popular format for storing mail; imap/smtp servers use it) format designed in way that read and write operations can intersect each other.

Don't use dd. It works on byte-level, not level of FSs, so using dd may produce inconsistent copy of your data. Don't use OpenBSD /altroot feature, it is wrapper for dd.

UPD:

dump on live FSs remounts it with R/O flag.

:(

So I can't use it for dumping /var partition. Use tar for live FS.

It is my solution for backuping the OpenBSD server. If you interested, adopt my backup.sh for your system:

#!/usr/bin/env bash

# TODO:
#
# - full backup => incremental backup?

function use 
{
    echo "Usage: $0 local backup_dir"
    exit 1
}

if [ $# -ne 2 ]
then
    use
fi

if [[ $1 == local ]]
then
    :
else
    use
fi  
    
BACKUPS="/home/neva_blyad/Облако/Загрузки/Система/Резервные образы/srv | LoveCry.pt/$2"

function remount_rw
{   
    DEV="$1"
    
    # Filesystem path
    DIR=`mount | grep "$DEV" | awk '{ print $3 }'`
    [ "$DIR" = / ] || DIR="$DIR/"

    # Remount back in R/W mode
    mount -fu -o rw "$DIR"
}

function backup
{
    DISK="$1"
    DEV="$2"
    FILENAME="$3"

    # Filesystem path
    DIR=`mount | grep "$DEV" | awk '{ print $3 }'`
    [ "$DIR" = / ] || DIR="$DIR/"

    # Backup a partition
    echo "Backing up partition $DEV ($DIR)..."

    # Store filesystem into archive
    if [ "$DIR" = /var/ ]
    then
        # TODO
        #
        # Warning! This branch was not tested.
        # (Do exclusions work properly?)

        # Can't remount as R/O even for several minutes.
        # We should use tar.
        cd "$DIR"
        gtar cf - --exclude=home      \
                  --exclude=mail      \
                  --exclude=opt/local \
                  --exclude=tmp       \
                  --exclude=usr/local \
                  --exclude=var       \
                  --exclude=Облако ./ |
            pv -perab -s $(du -ksx ./ | cut -f1)K | \
            #lz4 --force --quiet - "$BACKUPS/${DISK}_${FILENAME}.tar.lz4" && chown neva_blyad:neva_blyad "$BACKUPS/{$DI
            xz --quiet > "$BACKUPS/${DISK}_${FILENAME}.tar.xz" && chown neva_blyad:neva_blyad "$BACKUPS/${DISK}_${FILEN
        cd /
    else
        # Remount in R/O mode.
        # (Really, it is not neccessary, dump will do it anyway.)
        mount -fu -o ro "$DIR"

        # Use dump
        cd "$DIR"
        dump -0uan -h0 -f - ./ |
            pv -perab -s $(du -ksx ./ | cut -f1)K | \
            #lz4 --force --quiet - "$BACKUPS/${DISK}_${FILENAME}_dump.lz4" && chown neva_blyad:neva_blyad "$BACKUPS/{$D
            xz --quiet > "$BACKUPS/${DISK}_${FILENAME}_dump.xz" && chown neva_blyad:neva_blyad "$BACKUPS/${DISK}_${FILE
        cd /

        # Remount back in R/W
        mount -fu -o rw "$DIR"
    fi
}

# Make sure only root can run our script
if [ $EUID -ne 0 ]
then
    echo 'This script must be run as root'
    exit 1
fi

# Create backup directory
doas -u neva_blyad mkdir -p "$BACKUPS"

# Backup GPT of /dev/sd{0,1,2,4,5,6}*
echo 'Backing up GUID partition tables (GPT) of disks /dev/sd{0,1,2,4,5,6}*...'

fdisk sd0 > "$BACKUPS/sd0_fdisk"
fdisk sd1 > "$BACKUPS/sd1_fdisk"
fdisk sd2 > "$BACKUPS/sd2_fdisk"
fdisk sd4 > "$BACKUPS/sd4_fdisk"
fdisk sd5 > "$BACKUPS/sd5_fdisk"
fdisk sd6 > "$BACKUPS/sd6_fdisk"

chown neva_blyad:neva_blyad "$BACKUPS"/sd*_fdisk

# Backup BSD labels of /dev/sd{0,1,2,4,5,6}*
echo 'Backing up partition labels of BSD slices /dev/sd{0,1,2,4,5,6}*...'

disklabel sd0 > "$BACKUPS/sd0_disklabel"
disklabel sd1 > "$BACKUPS/sd1_disklabel"
disklabel sd2 > "$BACKUPS/sd2_disklabel"
disklabel sd4 > "$BACKUPS/sd4_disklabel"
disklabel sd5 > "$BACKUPS/sd5_disklabel"
disklabel sd6 > "$BACKUPS/sd6_disklabel"

chown neva_blyad:neva_blyad "$BACKUPS"/sd*_disklabel

# Backup BSD labels of /dev/sd{0,1,2,4,5,6}* (detailed)
echo 'Backing up partition labels of BSD slices /dev/sd{0,1,2,4,5,6}* (detailed)...'

disklabel -v sd0 > "$BACKUPS/sd0_disklabel_detailed"
disklabel -v sd1 > "$BACKUPS/sd1_disklabel_detailed"
disklabel -v sd2 > "$BACKUPS/sd2_disklabel_detailed"
disklabel -v sd4 > "$BACKUPS/sd4_disklabel_detailed"
disklabel -v sd5 > "$BACKUPS/sd5_disklabel_detailed"
disklabel -v sd6 > "$BACKUPS/sd6_disklabel_detailed"

chown neva_blyad:neva_blyad "$BACKUPS"/sd*_disklabel_detailed

#sync

# Cleanup before exiting if the program receives a signal during execution
function cleanup
{
    # Remount /dev/sd4* back in R/W
    remount_rw /dev/sd4a
    remount_rw /dev/sd4b
    remount_rw /dev/sd4d
    remount_rw /dev/sd4e
    #remount_rw /dev/sd4f

    # Mark files back with dump flag
    chflags dump /home/
    chflags dump /home/neva_blyad/Облако/
    chflags dump /opt/local/
    chflags dump /tmp/
    chflags dump /usr/local/
    chflags dump /var/
    chflags dump /var/mail/
}

trap cleanup INT TERM

# Backup /dev/sd4* (/)
chflags nodump /home/
chflags nodump /opt/local/
chflags nodump /tmp/
chflags nodump /usr/local/
chflags nodump /var/

backup sd4 /dev/sd4a root

chflags dump /home/
chflags dump /opt/local/
chflags dump /tmp/
chflags dump /usr/local/
chflags dump /var/

# Backup /dev/sd4* (/home/)
chflags nodump /home/neva_blyad/Облако/
backup sd4 /dev/sd4b home
chflags dump   /home/neva_blyad/Облако/

# Backup /dev/sd4* (/opt/local/, /tmp/, /usr/local/)
backup sd4 /dev/sd4d opt_local
#backup sd4 /dev/sd4e tmp # Used actively, should not remount as R/O
backup sd4 /dev/sd4f usr_local

# Backup /dev/sd5* (/var/)
chflags nodump /var/mail/
backup sd5 /dev/sd5a var
chflags dump   /var/mail/

echo Done

Cron daemon drives it periodically:

$ doas crontab -l

# Environment
SHELL=/bin/sh
PATH=/root/.local/bin/:/usr/local/sbin/:/usr/local/bin/:/usr/sbin/:/usr/bin/:/sbin/:/bin/
HOME=/var/log/
MAILTO=root

...

# Make backup everyday at 05:00
#0 5 * * MON backup.sh local mon
#0 5 * * TUE backup.sh local tue
#0 5 * * WED backup.sh local wed
#0 5 * * THU backup.sh local thu
#0 5 * * FRI backup.sh local fri
0 5 * * SAT backup.sh local sat
#0 5 * * SUN backup.sh local sun

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .