L'objet est de créer une sauvegarde de la carte SD vers un serveur distant sous samba et de réduire la taille de se fichier .img.
Prérequis
Un serveur sous linux avec samba ayant un espace libre supérieur à 3 fois la taille de la carte SD
Installation du client samba
sudo apt-get install samba-common smbclient samba-common-bin cifs-utilsMontage du dossier distant
Le serveur samba est en 192.168.1.100 et partage un dossier nommé "NAS" pour l'utilisateur "nas" avec mot de passe "nas", "/media/nas" est le chemin local vers le serveur samba.sudo mkdir /media/nas
sudo mount -t cifs //192.168.1.100/NAS /media/nas -o user=nas,pass=nas
Lancement de la copie de la carte SD
Option: utilisation d'un console déportée :sudo apt-get install screen
sudo screen -S backup
Le dossier partagé a pour chemin locale: /media/omv/tmp/
sudo dd if=/dev/mmcblk0 of=/media/omv/tmp/image.img bs=1M
Le temps passe et cours... et puis à la fin:
On quitte la console déportée par Ctrl a + d et on y revient par sudo screen -x backup15193+1 enregistrements lus
15193+1 enregistrements écrits
15931539456 bytes (16 GB, 15 GiB) copied, 5734,18 s, 2,8 MB/s
Maintenant on se connecte au serveur samba (192.168.1.100) pour réduire la taille de l'image créée
Création d'une console déportée
sudo screen -S shrinkLancement du script de réduction (voir plus bas)
sudo chmod +x pishrink.sh
sudo ./pishrink.sh /media/usb/tmp/image.img /media/usb/tmp/cabass.img
Ctrl a + d pour quitter la console déportée
Pour vérifier l'avancement du script on ré-ouvre la console déportée par:
sudo screen -x shrink
Ctrl a + d
etc...
A la fin le script donne les tailles des images avant et après, c'est beau.
Annexe
Voici le script pishrink.sh proposé par Matteo Mattei :
#!/bin/bash
#
# Copyright 2016 - Matteo Mattei <matteo.mattei@gmail.com>
# This script is intended to be used to shrink raspberry pi images
# created with dd client command.
# Check if you are root
if [ ! $(id -u) -eq 0 ]; then
echo "ERROR: This program must run as root"
exit 1
fi
IMG=${1}
# Check if the file exists
if [ ! -f ${IMG} ]; then
#
# Copyright 2016 - Matteo Mattei <matteo.mattei@gmail.com>
# This script is intended to be used to shrink raspberry pi images
# created with dd client command.
# Check if you are root
if [ ! $(id -u) -eq 0 ]; then
echo "ERROR: This program must run as root"
exit 1
fi
IMG=${1}
# Check if the file exists
if [ ! -f ${IMG} ]; then
echo "ERROR: File ${IMG} does not exist"
exit 1
fi
if [ -z "$(which parted 2> /dev/null)" ]; then
echo "ERROR: parted command not found - please install it and retry"
exit 1
fi
if [ -z "$(which losetup 2> /dev/null)" ]; then
echo "ERROR: losetup command not found - please install it and retry"
exit 1
fi
if [ -z "$(which resize2fs 2> /dev/null)" ]; then
echo "ERROR: resize2fs command not found - please install it and retry"
exit 1
fi
if [ -z "$(which truncate 2> /dev/null)" ]; then
echo "ERROR: truncate command not found - please install it and retry"
exit 1
fi
if [ -z "$(which gzip 2> /dev/null)" ]; then
echo "ERROR: gzip command not found - please install it and retry"
exit 1
fi
INFO=$(parted -m ${IMG} unit B print | grep ext4)
NUM=$(echo ${INFO} | awk -F':' '{print $1}')
START=$(echo ${INFO} | awk -F':' '{print $2}')
OLD=$(echo ${INFO} | awk -F':' '{print $3}')
DUMMY=$(echo ${INFO} | awk -F':' '{print $4}')
START=${START::-1}
OLD=${OLD::-1}
LOOPBACK=$(losetup -f --show -o $START $IMG)
e2fsck -p -f ${LOOPBACK}
if [ ! ${?} -eq 0 ]; then
echo "ERROR: filesystem seems corrupted"
losetup -d ${LOOPBACK}
exit 1
fi
INFO=$(resize2fs -P ${LOOPBACK} 2>&1)
SIZE=$(echo ${INFO} | awk -F': ' '{print $2}')
SIZE=$((${SIZE} + 1024))
resize2fs -p ${LOOPBACK} ${SIZE}
losetup -d ${LOOPBACK}
SIZE=$(( ${SIZE} * 4096 + ${START} ))
parted ${IMG} rm ${NUM}
parted -s ${IMG} unit B mkpart primary ${START} ${SIZE}
SIZE=$(( ${SIZE} + 58720257 ))
truncate -s ${SIZE} ${IMG}
gzip -9 ${IMG}
exit 1
fi
if [ -z "$(which parted 2> /dev/null)" ]; then
echo "ERROR: parted command not found - please install it and retry"
exit 1
fi
if [ -z "$(which losetup 2> /dev/null)" ]; then
echo "ERROR: losetup command not found - please install it and retry"
exit 1
fi
if [ -z "$(which resize2fs 2> /dev/null)" ]; then
echo "ERROR: resize2fs command not found - please install it and retry"
exit 1
fi
if [ -z "$(which truncate 2> /dev/null)" ]; then
echo "ERROR: truncate command not found - please install it and retry"
exit 1
fi
if [ -z "$(which gzip 2> /dev/null)" ]; then
echo "ERROR: gzip command not found - please install it and retry"
exit 1
fi
INFO=$(parted -m ${IMG} unit B print | grep ext4)
NUM=$(echo ${INFO} | awk -F':' '{print $1}')
START=$(echo ${INFO} | awk -F':' '{print $2}')
OLD=$(echo ${INFO} | awk -F':' '{print $3}')
DUMMY=$(echo ${INFO} | awk -F':' '{print $4}')
START=${START::-1}
OLD=${OLD::-1}
LOOPBACK=$(losetup -f --show -o $START $IMG)
e2fsck -p -f ${LOOPBACK}
if [ ! ${?} -eq 0 ]; then
echo "ERROR: filesystem seems corrupted"
losetup -d ${LOOPBACK}
exit 1
fi
INFO=$(resize2fs -P ${LOOPBACK} 2>&1)
SIZE=$(echo ${INFO} | awk -F': ' '{print $2}')
SIZE=$((${SIZE} + 1024))
resize2fs -p ${LOOPBACK} ${SIZE}
losetup -d ${LOOPBACK}
SIZE=$(( ${SIZE} * 4096 + ${START} ))
parted ${IMG} rm ${NUM}
parted -s ${IMG} unit B mkpart primary ${START} ${SIZE}
SIZE=$(( ${SIZE} + 58720257 ))
truncate -s ${SIZE} ${IMG}
gzip -9 ${IMG}