System backup with rsync from Linux to my NAS

Using rsync to backup my documents, pictures, music, ... to my NAS.
created by on 2013-01-24

Example

Copy files from $source to $target:

rsync --progress -vitnru --delete $source $target

The rsync options explained

--progress
Shows progress during the transfer.

-v
Show verbose output.

-i
Output a change summary for all changed files.

-t
Preserve the last-modified dates of the files.

-r
Recurse into directories.

-u
Skip files which already exist. Only update them if the content has changed.

--delete
Delete files at the target that don’t exist in the source directory.

-n
Simulate the the synchronization process.

--log-file=sync.log
Log all actions to a file.

Test run

By using the -n option you can perform a test run without actually changing anything.

Source: $HOME/Documents/
Target: /mnt/NAS/documents/

rsync --progress -vitnru --delete --log-file=sync.log $HOME/Documents/ /mnt/NAS/documents/

Bash script for folder synchronization

A script which saves my documents to a different folder (which resides on my NAS).

# !/usr/bin/env bash
# This script backs up my documents
# filename: backup-documents-to-nas.sh

# Parameters
sourcefolder=$HOME/Documents/
targetfolder=/mnt/NAS/documents/

logdirectory=$HOME/logs/backup
logfilepath="$logdirectory/documents-to-nas.log"

# Prequisites
# Create log directory if it does not exist
if [ ! -d "$logdirectory" ]; then
	mkdir -p "$logdirectory"
fi

# Start Sync
rsync -itru --delete --log-file=$logfilepath $sourcefolder $targetfolder

Screenshot of the bash script which uses rsync to backup my documents to my NAS

Parameterized bash script for folder synchronization

A generic bash script for synchronizing from a <source> to a <target>.

# !/usr/bin/env bash
# Synchronize from a source-directory to a target-directory.

# Filename:
# synchronize-source-to-target.sh

# Usage:
# synchronize-source-to-target.sh "<name>" "<source-direcotry-path>" "<target-direcotry-path>"

# Get parameters
projectname="$1"
sourcedirectory="$2"
targetdirectory="$3"

# Validate parameters
# Check if a project name has been specified
if [ -z "$projectname" ]; then
    echo "Please specify a name for the synchronization."
    exit
fi

# Check if a source directory has been specified
if [ -z "$sourcedirectory" ]; then
    echo "You must specifiy a source directory path."
    exit
fi

# Check if a target directory has been specified
if [ -z "$targetdirectory" ]; then
    echo "You must specify a target directory path."
    exit
fi

# Check if the source directory exists
if [ ! -d "$sourcedirectory" ]; then
    echo "The source directory $sourcedirecory does not exist."
    exit
fi

# Check if the target directory exists
if [ ! -d "$targetdirectory" ]; then
    echo "The target directory $targetdirectory does not exist."
    exit
fi

# Assemble log file path
logdirectory="$HOME/logs"
logfilename="sync-from-source-to-target-${projectname// /-}"
logfilepath="$logdirectory/$logfilename.log"

# Create the log directory if it does not exist
if [ ! -d "$logdirectory" ]; then
    echo "Creating the log file direcotry $logdirectory."
    mkdir -p "$logdirectory"
fi

# Start the synchronization
rsync -itru --delete --log-file="$logfilepath" $sourcedirectory $targetdirectory

Links

Tags: