How To backup Raspberry into Image file on remote linux server

Sep 19, 2019 12:11

I want to backup live raspberry into image file on server.
So if sdcard crashed I cat to write current image directly.

In this example

source - Raspbian 9, sdcard 32 GB, ip 192.168.0.33

destination - Linux (Debian 7) with hostname t43 ip 192.168.0.43
dir for image
/mnt/sda8/raspi

0. on Raspi

change

/boot/cmdline.txt
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=3555ae4d-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

to
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

and /etc/fstab as

#PARTUUID=3555ae4d-01 /boot vfat defaults 0 2
#PARTUUID=3555ae4d-02 / ext4 defaults,noatime 0 1

/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / ext4 defaults,noatime,nodiratime,norelatime, 0 1

On remote Server t43

apt-get install dosfstools

1. Create destination image file on server

make the image a little smaller due to different flash drives
f.e.
cd /mnt/sda8/raspi

# for 32 GB
dd if=/dev/zero of=RASPI32.img bs=1000000 count=30000

# for 8 FB
# dd if=/dev/zero of=RASPI8.img bs=1M count=7000

2. Make block device, fdisk and mount

create block device.

losetup /dev/loop0 RASPI32.img

# create partitions

fdisk /dev/loop0
n
p
start 8192
last 98045
// set fat
t
c

n
p
2
start 98304
last

p

Command (m for help): p

Disk /dev/loop0: 30.0 GB, 30000000000 bytes
255 heads, 63 sectors/track, 3647 cylinders, total 58593750 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb31ea4a2

Device Boot Start End Blocks Id System
/dev/loop0p1 8192 98045 44927 c W95 FAT32 (LBA)
/dev/loop0p2 98304 58593749 29247723 83 Linux

w

reboot

cd /mnt/sda8/raspi

losetup /dev/loop0 RASPI32.img

partx -av /dev/loop0

# make file systems

mkdosfs /dev/loop0p1 -I
mkfs.ext4 /dev/loop0p2 -L RASPICLONE

# install grub
grub-install /dev/loop0

# make mount points

mkdir /mnt/sdfat
mkdir /mnt/sdext4

# try to mount

mount /dev/loop0p1 /mnt/sdfat/
mount /dev/loop0p2 /mnt/sdext4/

root@t43:/mnt/sda8/raspi# ls -al /mnt/sdext4/
total 24
drwxr-xr-x 3 root root 4096 Sep 19 10:18 .
drwxr-xr-x 13 root root 4096 Sep 16 23:01 ..
drwx------ 2 root root 16384 Sep 19 10:18 lost+found

cool

3. make those disks permanently

# add to rc.local
vi /etc/rc.local

.....

losetup /dev/loop0 /mnt/sda8/raspi/RASPI32.img
partx -av /dev/loop0

mount /dev/loop0p1 /mnt/sdfat/
mount /dev/loop0p2 /mnt/sdext4/

.......
exit 0

for test
reboot

4. On Raspi

I hope you already have public key for ssh, rsync etc
(
ssh-keygen -t rsa
add id_rsa.pub to server's authorized_keys2
}

create script to backup

vi raspi2t43.sh

#!/bin/sh

rsync_options="--force -rltWDEHXAgoptx"

if [ -f /etc/dphys-swapfile ]
then
swapfile=`cat /etc/dphys-swapfile | grep ^CONF_SWAPFILE | cut -f 2 -d=`
if [ "$swapfile" = "" ]
then
swapfile=/var/swap
fi
exclude_swapfile="--exclude $swapfile"
fi

# save boot to fat
echo save boot to fat
echo rsync -av /boot/ 192.168.0.43:/mnt/sdfat

rsync -av /boot/ 192.168.0.43:/mnt/sdfat

echo sync main system

rsync -v $rsync_options --delete \
$exclude_useropt \
$exclude_swapfile \
--exclude '.gvfs' \
--exclude '/dev/*' \
--exclude '/mnt/clone/*' \
--exclude '/proc/*' \
--exclude '/run/*' \
--exclude '/sys/*' \
--exclude '/tmp/*' \
--exclude 'lost\+found/*' \
--exclude '/boot/*' \
/ 192.168.0.43:/mnt/sdext4

ssh 192.168.0.43 sync

and run it

5. Burn this image
now copy this image to windows


and burn tgis image to new sdcard


(yes. i know dd for linux)

put burned sdcard to raspberry and run it

comments of Rene Snijders
1.
Rene Snijders Good Idea but not the proper way to do this :
If you already using an remote server that’s running Linux, Then don’t build a 32GB or less image.
But write it directly to the microSD card.
Don't use fdisk, but cfdisk ( see image ) it's a ncurses driven fdisk much much easy-er
with some "sed" magic you can simply identify the UUID
blkid | sed -n '/mmcblk0/s/.* UUID=\"\([^\"]*\)\".*/\1/p'
To automate the creation of partitions on a device (also image)
use parted ...
(parted) mktable msdos
(parted) mkpart primary fat32 1 512mb
(parted) mkpart primary ext4 512mb 100%
(parted) quit
this is much cleaner than : all the keystrokes : n, p, start, 8192, last, 98045, //, set, fat, t, c, n, p, 2, start, 98304, last, p
the 100% in the line means, start from 512MB and the end of the disk. So if the microSD is 32GB then the rootfs would be 32GB minus the 512mb for the boot partition.
instead of rsync use sshfs, and build a compressed copy of your system files. this saves a lot of space. And exclude /var/log those files will be created when needed by the systemd process.


2.
Rene Snijders * * * * BEWARE ALERT / CAUTION ALERT * * * *
forgot to mention how to the script / automate with parted:
Replace /device with the usb2microSD reader. for example /dev/sdc
write this in a script file for example makeTheSD and do't forget to make it executable with chmod +x makeTheSD
parted --script /device \
mktable msdos \
mkpart primary fat32 1 512mb \
mkpart primary ext4 512mb 100% \
quit
* * * * BEWARE ALERT / CAUTION ALERT * * * *
Tripple check the device name. if you run this on the system device it will wipe out the partition table of your drive
* * * * BEWARE ALERT / CAUTION ALERT * * * *

в facebook https://www.facebook.com/groups/RaspberryPiProjects/?multi_permalinks=1336102939898438&comment_id=1336119809896751¬if_id=1568887464079682¬if_t=feedback_reaction_generic
Up