Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSync only if filesystem is mounted

I want to setup a cron job to rsync a remote system to a backup partition, something like:

bash -c 'rsync -avz --delete --exclude=proc --exclude=sys root@remote1:/ /mnt/remote1/'

I would like to be able to "set it and forget it" but what if /mnt/remote1 becomes unmounted? (After a reboot or something) I'd like to error out if /mnt/remote1 isn't mounted, rather than filling up the local filesystem.

Edit:
Here is what I came up with for a script, cleanup improvements appreciated (especially for the empty then ... else, I couldn't leave them empty or bash errors)

#!/bin/bash

DATA=data
ERROR="0"

if cut -d' ' -f2 /proc/mounts | grep -q "^/mnt/$1\$"; then
    ERROR=0
else
    if mount /dev/vg/$1 /mnt/$1; then
        ERROR=0
    else
        ERROR=$?
        echo "Can't backup $1, /mnt/$1 could not be mounted: $ERROR"
    fi
fi

if [ "$ERROR" = "0" ]; then
    if cut -d' ' -f2 /proc/mounts | grep -q "^/mnt/$1/$DATA\$"; then
        ERROR=0
    else
        if mount /dev/vg/$1$DATA /mnt/$1/data; then
            ERROR=0
        else
            ERROR=$?
            echo "Can't backup $1, /mnt/$1/data could not be mounted."
        fi
    fi
fi

if [ "$ERROR" = "0" ]; then
    rsync -aqz --delete --numeric-ids --exclude=proc --exclude=sys \
        root@$1.domain:/ /mnt/$1/
    RETVAL=$?
    echo "Backup of $1 completed, return value of rsync: $RETVAL"
fi
like image 652
thelsdj Avatar asked Aug 26 '08 03:08

thelsdj


People also ask

Does rsync ignore existing files?

Rsync with --ignore-existing-files: We can also skip the already existing files on the destination. This can generally be used when we are performing backups using the –link-dest option, while continuing a backup run that got interrupted. So any files that do not exist on the destination will be copied over.

Which is faster cp or rsync?

rsync is much faster than cp for this, because it will check file sizes and timestamps to see which ones need to be updated, and you can add more refinements. You can even make it do a checksum instead of the default 'quick check', although this will take longer.

Is rsync asynchronous?

Q1: Yes. Rsync uses asynchronous I/O (non-blocking). By using that option, you are forcing it to use blocking I/O mode. This would be used when the remote shell (rsh) cannot handle non-blocking mode.

Does rsync only copy changed files?

By default, rsync will only copy new or changed files from a source to a destination. The –update or -u option is used by rsync to skip files that are still new in the destination directory. Also, –dry-run or -n enables us to execute a test operation without making any changes.


3 Answers

mountpoint seems to be the best solution to this: it returns 0 if a path is a mount point:

#!/bin/bash
if [[ `mountpoint -q /path` ]]; then
    echo "filesystem mounted"
else
    echo "filesystem not mounted"
fi

Found at LinuxQuestions.

like image 80
bob esponja Avatar answered Jan 03 '23 18:01

bob esponja


if cut -d' ' -f2 /proc/mounts | grep '^/mnt/remote1$' >/dev/null; then
    rsync -avz ...
fi

Get the list of mounted partitions from /proc/mounts, only match /mnt/remote1 (and if it is mounted, send grep's output to /dev/null), then run your rsync job.

Recent greps have a -q option that you can use instead of sending the output to /dev/null.

like image 42
Ted Percival Avatar answered Jan 03 '23 17:01

Ted Percival


A quick google led me to this bash script that can check if a filesystem is mounted. It seems that grepping the output of df or mount is the way to go:

if df |grep -q '/mnt/mountpoint$'
    then
        echo "Found mount point, running task"
        # Do some stuff
    else
        echo "Aborted because the disk is not mounted"
        # Do some error correcting stuff
        exit -1
fi
like image 31
Harley Holcombe Avatar answered Jan 03 '23 16:01

Harley Holcombe