Category Archives: Ceph

Ceph RBD Backups

We have been playing with OpenNebula and Ceph and this has included crashing our cluster several times. (6 to be honest). Last night I thought it might be a good idea to backup the test cluster so we don’t need recreate the data each time a team member (mostly me) destroys the thing.

 

 

Backup script:

#!/bin/bash
###
# Author: Rhian Resnick
# Purpose: Backup CEPH RBD using snapshots, the files that are created should be stored off the ceph cluster, but you can use ceph storage during the process of backing them up.
# This should be replaced with a mirroring configuration to Boca when we have the hardware available.
# Use the restore command to create a new rbd volume of your preferred name
 
pool=$1
if [ “$pool” == “” ]
then
     echo Usage: $0 pool
    exit 1
fi
rbd ls $pool | while read vol
do
    # Make sure the directory exists
    if [ ! -d $vol ]
    then
        mkdir $vol
    fi
    # Look up latest backup file
    LASTSNAP=`ls $vol/ | sort | tail -n 1 | cut -d "." -f 1`
    echo "Last Snap: $vol/$LASTSNAP"
    
    # Create a snap, we need this to do the diff
    NEWSNAP=`date +%y%m%d%H%M`
    echo "New Snap: $NEWSNAP"
        echo rbd snap create $pool/$vol@$NEWSNAP
        rbd snap create $pool/$vol@$NEWSNAP
    
    if [ "$LASTSNAP" == "" ]
    then
        echo rbd export-diff $pool/$vol@$NEWSNAP $vol/$NEWSNAP.diff
        rbd export-diff $pool/$vol@$NEWSNAP $vol/$NEWSNAP.diff
    else
            echo rbd export-diff --from-snap $LASTSNAP $pool/$vol@$NEWSNAP $vol/$NEWSNAP.diff
            rbd export-diff --from-snap $LASTSNAP $pool/$vol@$NEWSNAP $vol/$NEWSNAP.diff
    fi
done

Restore Script

#!/bin/bash
# Restore a rbd volume from the backups
pool=$1
vol=$2
newvol=$3
if [ "$newvol" == "" ]
then
    echo Usage: $0 pool volume newvolume
    exit 1
fi
#rbd create $pool/$newvol --size 0M
echo rbd create $pool/$newvol --size 0M
rbd create $pool/$newvol --size 0M
ls $vol | cut -d "." -f 1 | sort | while read snap
do
    echo rbd import-diff $vol/$snap.diff $pool/$newvol
    rbd import-diff $vol/$snap.diff $pool/$newvol
done