A Simple BASH Backup Script

A very simple command script to mirror the all the Linkstation 2 share data to a USB drive.

It simply works out what is on the destination, but is not in the source, and deletes it from the destination. Then it uses the “copy” command with the “-u” flag (among others) to copy only those files on the source which are new or different to what is on the destination.

The Backup Script “ls-backup”

#!/bin/bash

#Name: ls-backup

#Usage: ./ls-backup > ls-backup.log


#Example crontab entry to run the script at 7am every Sunday:

# 00 7 * * 0 /mnt/usbdisk1/ls-backup > /mnt/usbdisk1/ls-backup.log


#A simple script to backup the LinkStation HDD Shares to a USB HDD.

#It is a Mirror backup. New/modified files copied & deleted files removed.


#Source & Destination directories.

srcdir="/mnt/hda"

dstdir="/mnt/usbdisk1/HDA-Backup"


if [ -d $srcdir ] && [ -d $dstdir ]; then

    echo "****************************"

    date

    echo "****************************"

    echo

    echo "Starting the LinkStation All Shares backup..."

    echo "  SRC:/$srcdir -> DST:/$dstdir"

    echo

    echo "Checking Dst files..."

    cd "$dstdir"

    find . -print0 | while read -d $'\0' file

    do

        if [ ! -e "$srcdir/$file" ]; then

            echo "No longer in Source: $file"

            rm -rfv "$file"

        fi

    done

    echo

    echo "Copying Src files..."

    cp -dfrupv $srcdir/* $dstdir

    echo

    echo "Backup finished."

    echo

    echo "****************************"

    date

    echo "****************************"

else

    echo

    echo "Path(s) not found... Check that the Src & Dst directories exist!"

fi

Automation Via “crontab”

To perform a backup at 7am every Sunday, add this to /etc/crontab

export EDITOR=vi

crontab -e


00 7 * * 0 /mnt/usbdisk1/ls-backup > /mnt/usbdisk1/ls-backup.log

Example Log Capture Example “crontab” Configuration Published on Download the  Simple BASH Backup Script