OpenBSD:如何对已安装的磁盘进行备份/快照?

OpenBSD:如何对已安装的磁盘进行备份/快照?

我计划在最新的 OpenBSD 上启动一个服务器。 (Web、云等等。)我必须定期备份我的 ffs2 分区(例如使用 cron)。

但没有关于此的文档,只有转储实用程序的 man。据我了解,只能转储未安装的设备。 (FreeBSD 的转储有 -L 选项,允许备份实时文件系统,但 OpenBSD 缺乏此功能。)

如何转储已安装/活动分区?也许您建议使用另一个文件系统?

答案1

您可以dump在活动分区上使用。rsnapshots(不是基于 OpenBSD,但作为包提供)也可能有用。

作为一般建议,请仔细规划备份。数据库(MySQL 等)通常有自己的备份工具和/或主从同步,这可能会有所帮助,并且通常会使恢复变得更加容易。邮件备份很大程度上取决于如何您/您的用户计划使用它(例如,如果您想恢复意外删除的邮件,那么每日备份可能没有多大帮助,并且邮件通常是一个快速移动的目标(至少 Dovecot 具有 IMAP 主从同步)。定期备份版本化存储(例如 git 存储库)可能没有意义,因为它们本质上是“备份自己”等......

答案2

答案是使用dump。

OpenBSD 版本的转储适用于没有任何标志的已安装文件系统,例如 FreeBSD 的 -L。(UPD:实际上,不,见下文

当然,实时 FS 上的所有读/写操作都不需要锁,例如 maildir(最流行的存储邮件格式;imap/smtp 服务器使用它)格式的设计方式使得读和写操作可以相互交叉。

不要使用dd。它适用于字节级别,而不是文件系统级别,因此使用 dd 可能会产生不一致的数据副本。不要使用 OpenBSD /altroot 功能,它是 dd 的包装。

更新:

实时 FS 上的转储会使用 R/O 标志重新安装它。

:(

所以我不能用它来转储 /var 分区。使用 tar 进行实时 FS。

这是我备份 OpenBSD 服务器的解决方案。如果您有兴趣,请为您的系统采用我的 backup.sh:

#!/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 守护进程定期驱动它:

$ 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

相关内容