#!/bin/bash # # # Dieser Code enthält Codeteile aus # https://www.linux-tips-and-tricks.de/raspiBackup.sh # Copyright (C) 2013-2016 framp at linux-tips-and-tricks dot de #Config autoMount=true localMountPoint="/media/nas/backup" ipNAS="192.168.233.2" shareNAS="backup" backupName="pi-backup" ###### function calcSumSizeFromSFDISK() { # sfdisk file name local file="$1" local partitionregex="${dev}[p]?([0-9]+).*start=[^0-9]*([0-9]+).*size=[^0-9]*([0-9]+).*Id=[^0-9a-z]*([0-9a-z]+)" local sumSize=0 while read line; do if [[ -z $line ]]; then continue fi if [[ $line =~ $partitionregex ]]; then local p=${BASH_REMATCH[1]} local start=${BASH_REMATCH[2]} local size=${BASH_REMATCH[3]} local id=${BASH_REMATCH[4]} if [[ $id == 85 || $id == 5 ]]; then continue fi if [[ $sumSize == 0 ]]; then sumSize=$((start+size)) else (( sumSize+=size )) fi fi done < $file (( sumSize *= 512 )) echo "$sumSize" } function restoreSdCard () { echo "Backup......" /usr/local/bin/raspiBackup.sh -d $restoreDev $rootPathOfBackup } function readAnswer() { read -p "Vorgang fortsetzen? ([j]a/[n]ein)" answer answer=${answer:0:1} # first char only answer=${answer:-"n"} # set default no if [ "$answer" != "j" ];then exit 10 fi } function correctSFdisk () { local partitionregex="${dev}[p]?([0-9]+).*start=[^0-9]*([0-9]+).*size=[^0-9]*([0-9]+).*Id=[^0-9a-z]*([0-9a-z]+)" local lineNo=0 local sumSize=0 tac $rootPathOfBackup/${backupName}.sfdisk| while read line; do (( lineNo++ )) if [[ -z $line ]]; then continue fi if [[ $line =~ $partitionregex ]]; then local p=${BASH_REMATCH[1]} local start=${BASH_REMATCH[2]} local size=${BASH_REMATCH[3]} local id=${BASH_REMATCH[4]} if [[ $id == 85 || $id == 5 ]]; then continue fi if [[ $start == 0 ]]; then continue else sizeRestoreSdCardSectors=$(blockdev --getsz $restoreDev) newSizeLastPartition=$(( $sizeRestoreSdCardSectors - $start )) sed -i'.rBR.org' "s/$size/$newSizeLastPartition/g" $rootPathOfBackup/${backupName}.sfdisk break fi fi done } #main # if [ $# != 2 ]; then echo -e "\nUSAGE: $(basename $0) \n" exit 1 fi if [ $autoMount ]; then if [ ! -d ${localMountPoint} ]; then mkdir -p ${localMountPoint} fi if [ $(mount | grep -i "$localMountPoint" | wc -l) != "1" ]; then mount.nfs ${ipNAS}:/${shareNAS} ${localMountPoint} if [ $? != 0 ]; then echo "Mount fehlerhaft!!!" fi fi fi rootPathOfBackup="$2" restoreDev=$1 if [ ! -f $rootPathOfBackup/$backupName.sfdisk ] || [ ! -f $rootPathOfBackup/$backupName.fdisk ]; then echo "$rootPathOfBackup/$backupName.sfdisk $rootPathOfBackup/$backupName.fdisk" echo -e "\nKein Backupverzeichniss oder kein Backup mit dem Namen $backupName gefunden." exit 3 fi sizeOrginalSdCard=$(calcSumSizeFromSFDISK $rootPathOfBackup/${backupName}.sfdisk|sed 's/^[ \t]*//;s/[ \t]*$//') sizeRestoreSdCard=$(blockdev --getsize64 $restoreDev|sed 's/^[ \t]*//;s/[ \t]*$//') if (( $sizeOrginalSdCard > $sizeRestoreSdCard )); then sizeRestoreSdCardGB=$(($sizeRestoreSdCard / 1024 / 1024 / 1024)) echo "Backup wird auf $restoreDev wiederhergestellt. Dieses Device ist ca. $sizeRestoreSdCardGB GB gross." readAnswer correctSFdisk #correct Fdisk sed -i'.rBR.org' "s/$sizeOrginalSdCard/$sizeRestoreSdCard/g" $rootPathOfBackup/pi-backup.fdisk restoreSdCard else readAnswer restoreSdCard fi