I
I
Ivan Semenov2017-07-13 15:04:28
FreeBSD
Ivan Semenov, 2017-07-13 15:04:28

How to restore a FreeBSD system with ZFS?

Good afternoon!
For several days I have been dealing with the system restore script, I checked each piece of the script separately, except for the last part.
The script works fine except for the last few lines.
backup script:

#!/bin/sh
poolname='zroot'
DATE="`date +%Y.%m.%d_%H%M`"
echo "script start in `date +%Y.%m.%d_%H:%M`"
zfs snapshot -r ${poolname}@${DATE}
zfs list -t snapshot

echo "backup start in `date +%Y.%m.%d_%H:%M`"

/usr/sbin/mount_smbfs -I 19x.54.xx.xx //[email protected]/lso/ /mnt/

rm -f /mnt/backup/bfg/bfg.*

cp /boot/pmbr /mnt/backup/bfg/bfg.${poolname}.${DATE}.pmbr
cp /boot/gptzfsboot /mnt/backup/bfg/bfg.${poolname}.${DATE}.gptzfsboot

zfs send -R ${poolname}@${DATE} | gzip -9 > /mnt/backup/bfg/bfg.${poolname}.${DATE}.gz

/sbin/umount /mnt/

echo "backup end in `date +%Y.%m.%d_%H:%M`"

zfs destroy -r ${poolname}@${DATE}

restore script:
#!/bin/sh

# The name of the disk.
dev=da0

# Name of the created pool.
tank=zroot

# Host network address.
ip_host="19x.54.xx.xx"

# Address of NC server.
ip_ftp="19x.54.xx.xx"

# Port NC server.
port_nc="xxxx"

# Login and password of the NC server.
usr="fsbackup"
pass=${usr}
path="/mnt/drake/lso/backup/bfg"

# Date and time of the backup.
backup_data="2017.07.13_0916"

# Archive name with a packed pool.
backup_f="bfg.zroot.${backup_data}.gz"

# The names of files downloaders.
backup_gptzfsboot="bfg.zroot.${backup_data}.gptzfsboot"
backup_pmbr="bfg.zroot.${backup_data}.pmbr"

# Removing all partitions.
gpart destroy -F ${dev}

# Mashing the beginning of the disc.
dd if=/dev/zero of=/dev/${dev} bs=1M count=2048

# Partitioning the disk and creating partitions.
echo "script: Create GPT, add partitions..."
gpart create -s gpt $dev

echo "script: add freebsd-boot partition boot0"
gpart add -b 40 -s 1024 -t freebsd-boot -l boot0 ${dev}

echo "script: add freebsd-swap partition swap0"
gpart add -b 2048 -s 4G -t freebsd-swap -l swap0 ${dev}

# Calculating long 3, primary partition, ZFS.

# Starting block and long blocks in the entire device (disk).
blk_begin=`gpart show ${dev} | grep '=>' | awk '{a = $2; print a}'`
echo "script: blk_begin=${blk_begin}"
blk_end=`gpart show ${dev} | grep '=>' | awk '{a = $3; print a}'`
echo "script: blk_end=${blk_end}"

# Initial and long block in the blocks section (freebsd-swap).
blk_swop_begin=`gpart show ${dev} | grep 'freebsd-swap' | awk '{a = $1; print a}'`
echo "script: blk_swop_begin=${blk_swop_begin}"
blk_swop_end=`gpart show ${dev} | grep 'freebsd-swap' | awk '{a = $2; print a}'`
echo "script: blk_swop_end=${blk_swop_end}"

# The initial block partition ZFS.
blk_zfs_begin=$((${blk_swop_begin} + ${blk_swop_end}))
echo "script: blk_zfs_begin=${blk_zfs_begin}"

# Long desired in the blocks ZFS partition taking into account the multiplicity 8 blocks (4K).
blk_long=$(( (((${blk_begin}+${blk_end})-${blk_zfs_begin})/8)*8 ))
echo "script: blk_long=${blk_long}"

echo "script: add freebsd-zfs partition data0, long is ${blk_long} blocks"
gpart add -s ${blk_long} -t freebsd-zfs -l data0 ${dev}

echo " "
echo "script: GPT on $dev created, partitions added."
echo " "

sleep 10

/usr/sbin/mount_smbfs -I 19x.54.xx.xx //[email protected]/lso/ /mnt/

# Download from FTP server backup loaders (/boot/pmbr и /boot/gptzfsboot)
echo "script: Download backup boot records from ftp start...."
cp /mnt/backup/bfg/${backup_gptzfsboot} /tmp/gptzfsboot
cp /mnt/backup/bfg/${backup_pmbr} /tmp/pmbr

/sbin/umount /mnt/

# Record loader in the boot sector.
gpart bootcode -b /tmp/pmbr -p /tmp/gptzfsboot -i 1 ${dev}

gpart show ${dev}
sleep 3
gpart show -l ${dev}
sleep 5

# Create ZFS pool
echo "script: Create ZFS pool..."

sysctl vfs.zfs.prefetch_disable=1

gnop create -S 4096 /dev/gpt/data0
zpool create -f -o altroot=/tmp -o cachefile=/var/tmp/zpool.cache ${tank} /dev/gpt/data0.nop
zpool export ${tank}
gnop destroy /dev/gpt/data0.nop
mkdir /tmp/${tank}
zpool import -o altroot=/tmp/${tank} -o cachefile=/var/tmp/zpool.cache ${tank}

zpool set bootfs=${tank} ${tank}

sleep 3

echo "script: Create filesystem..."

zfs set checksum=fletcher4 $tank

echo "script: Receive system from nc start...."
echo "Please enter following command on storage: \"cat ${path}/${backup_f} | nc ${ip_host} ${port_nc}\""
nc -l ${port_nc} | gzip -d -c | zfs receive -vF -d ${tank} 

echo "script: Receive system from nc finish"

sleep 5

mkdir /tmp/root
zfs set mountpoint=/tmp/root ${tank}
cd /tmp
zpool export ${tank}
zpool import -o altroot=/tmp/${tank} -o cachefile=/var/tmp/zpool.cache ${tank}
echo "script: Copy zpool cache..."
cp /var/tmp/zpool.cache /tmp/${tank}/tmp/root/boot/zfs/zpool.cache

zfs unmount -af
zfs set mountpoint=legacy ${tank}

echo "script: done!"
echo "enter please command 'reboot'"

Here is the message for this command:
cp /var/tmp/zpool.cache /tmp/${tank}/tmp/root/boot/zfs/zpool.cache: No such file or directory

And accordingly when loading:
/
Can't find /boot/zfsloader

FreeBSD/x86 boot
Default: zroot:/boot/kernel/kernel
boot:
-
Can't find /boot/kernel/kernel

I've already re-read a bunch of material, but I can't figure out how to make it work.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question