Menu

How I used clonezilla to backup and restore a software raided and LVM linux machine

2017-06-03
2017-06-05
  • jeff.sadowski

    jeff.sadowski - 2017-06-03

    I booted clonezilla.
    I knew the system layout already so I customised my backup and restore scripts.
    I first attempted to backup using dd if=/dev/system/root | bzip -9z > backup.bz2 but that was slow and huge.
    I ended up using tar to copy all the files into tar balls. I created a temp directory to mount /dev/system/root
    then cd'ed to the mounted filesystem

    #!/bin/bash
    mkdir -p /mnt/root
    mount /dev/system/root /mnt/root 2>/dev/null
    hostname=$(cat /mnt/root/etc/hostname)
    if [ "${hostname}" = "" ];then
    hostname="unknown_host"
    fi
    start=/home/partimag/raid_backup/${hostname}
    backup=${start}/backup-$(date +%Y-%m-%d_%H.%M.%S)
    mkdir -p ${backup}
    bkup() {
    mkdir -p /mnt/$1
    mount $2 /mnt/$1 2>/dev/null
    cd /mnt/$1
    SIZE=`du -sk ./ | cut -f 1`
    tar -cpf - ./ | pv -p -s ${SIZE}k | gzip > ${backup}/$1.tar.gz
    }
    bkup data /dev/data/data
    bkup root /dev/system/root
    bkup boot /dev/md/*boot
    blkid /dev/md/*boot | sed "s/.* UUID=\"\([^\"]*\).*/\1/" > "${backup}/boot.uuid"
    cd ${start}
    rm latest
    ln -s "$(basename ${backup})" latest
    

    that was all I really needed for my backup script.
    My restore looked as follows

    #!/bin/bash
    if [ "$1" = "" ];then
    echo "I need a hostname and optionally a restore point if not the latest"
    echo "$0 <hostname>"
    echo "$0 <hostname>/backup-<year>-<month>-<day>_<hour>.<minute>.<second>"
    exit
    fi
    
    if [ "$(parted /dev/sda print 2>/dev/null|grep primary)" != "" ];then
     echo "you need to clobber /dev/sda before running this"
     exit
    fi
    
    hostname=$(dirname $1)
    
    if [ "${hostname}" = "." ];then
     latest="latest"
     hostname=$(basename $1)
    else
     latest=$(basename $1)
    fi
    hostname ${hostname}
    
    backup=/home/partimag/raid_backup/${hostname}/${latest}
    
    pd() {
    parted /dev/$1 mklabel msdos yes
    parted /dev/$1 mkpart p 0% 1G
    parted /dev/$1 mkpart p 1G 100%
    parted /dev/$1 set 1 boot on
    parted /dev/$1 set 1 raid on
    parted /dev/$1 set 2 raid on
    }
    pd sda
    pd sdb
    
    restore() {
    echo "building $2"
    mkfs.ext3 $1
    if [ -f ${backup}/$2.uuid ];then
     tune2fs $1 -U $(cat ${backup}/$2.uuid)
    fi
    mkdir -p /mnt/$2
    mount $1 /mnt/$2
    cd /mnt/$2
    pv ${backup}/$2.tar.gz | tar -xzf -
    cd
    umount /mnt/$2
    }
    
    rr() {
    mdadm --create /dev/md/$1 --metadata 1.0 --level=raid1 --raid-devices=2 $2 $3 <<< "yes"
    }
    
    lvm() {
    pvcreate $1
    vgcreate $2 $1
    lvcreate -l 100%FREE -n $3 $2
    }
    
    rr boot /dev/sda1 /dev/sdb1
    restore /dev/md/boot boot
    
    rr lvm /dev/sda2 /dev/sdb2
    lvm /dev/md/lvm system root
    restore /dev/system/root root
    
    if [ ! -e /dev/data/data ];then
     parted /dev/sdc mklabel msdos yes
     parted /dev/sdc mkpart P 0% 100%
     parted /dev/sdc set 1 lvm on
     lvm /dev/sdc1 data data
     restore /dev/data/data data
    fi
    
    mount /dev/system/root /mnt/root
    bd() { mount --bind $1 /mnt/root$1; }
    bd /dev
    bd /sys
    bd /proc
    
    echo mount /boot > /tmp/chrootlist
    echo grub-install /dev/sda >> /tmp/chrootlist
    echo grub-install /dev/sdb >> /tmp/chrootlist
    echo update-grub >> /tmp/chrootlist
    echo exit >> /tmp/chrootlist
    chroot /mnt/root < /tmp/chrootlist
    

    I put these scripts in a directory raid_backup that is on a disk big enough to hold the backups.
    I mount the disk as /home/partimag
    On my end it is a nfs mount, but that shouldn't matter.

     

    Last edit: jeff.sadowski 2017-06-09
  • Steven Shiau

    Steven Shiau - 2017-06-05

    Great! Thanks for sharing that.

    Steven

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.