Snapshot with BTRFS (On remote volume)

Run the bash script below.
Change the srcdir, dstdir, mt (mount point), old (for deleting 365 days older backups) variable to your preference.


## Variable declaration
# date string for the subvolume name of a remote backup
datetime=`date +%d-%h-%Y_%H-%M`
# Local snapshot subvolume
# Remote snapshot subvolume

# Check if whether the destination Volume is mounted. If not, exit the script
if [ `df | grep -c 2tbhdd` -eq 0 ]; then
printf "backup destination is not mounted\n"
exit 1

# Check if there is an original snapshot locally for creating an inceremental backup. If not, start the initial backup
if [ ! -d "$srcdir" ]; then
printf "Issuing an initial backup\n"
btrfs subvolume snapshot -r / ${srcdir}
printf "Copy snapshot to remote destinatioin"
btrfs send ${srcdir} | btrfs receive ${dstdir}

# Start incremental backup from the local snapshot subvolume
printf "Creating a local snapshot\n"
btrfs subvolume snapshot -r / ${srcdir}_${datetime}
printf "Copying incremental snapshot to remote destination\n"
btrfs send -p ${srcdir} ${srcdir}_$datetime | btrfs receive ${dstdir}

# Replace the original subvolume with the snapshot newly created snapshot
printf "Replace the original subvolume with the snapshot newly created snapshot\n"
btrfs subvolume delete ${srcdir}
printf "Renaming the subvolume"
mv ${srcdir}_$datetime ${srcdir}

# Delete snapshots older than a year on remote volume.
old=`find ${dstdir} -maxdepth 1 -mtime +365`
if [ -z "$old" ]; then
printf "no older backups\n"
printf "Deleting snapshot more than one year old."
find ${dstdir} -maxdepth 1 -mtime +365 -exec btrfs subvolume delete {} \;

Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: