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

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

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

<p><strong>Edit:</strong><br />
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)</p>

<pre><code>#!/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
</code></pre>

[{'answer_id': 27370, 'author': 'Harley Holcombe', 'author_id': 1057, 'author_profile': 'https://Stackoverflow.com/users/1057', 'pm_score': 2, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>if df |grep -q \'/mnt/mountpoint$\'\n    then\n        echo "Found mount point, running task"\n        # Do some stuff\n    else\n        echo "Aborted because the disk is not mounted"\n        # Do some error correcting stuff\n        exit -1\nfi\n</code></pre>\n'}, {'answer_id': 27371, 'author': 'T Percival', 'author_id': 954, 'author_profile': 'https://Stackoverflow.com/users/954', 'pm_score': 4, 'selected': True, 'text': "<pre><code>if cut -d' ' -f2 /proc/mounts | grep '^/mnt/remote1$' &gt;/dev/null; then\n    rsync -avz ...\nfi\n</code></pre>\n\n<p>Get the list of mounted partitions from <code>/proc/mounts</code>, only match <code>/mnt/remote1</code> (and if it is mounted, send grep's output to <code>/dev/null</code>), then run your <code>rsync</code> job.</p>\n\n<p>Recent <code>grep</code>s have a <code>-q</code> option that you can use instead of sending the output to <code>/dev/null</code>.</p>\n"}, {'answer_id': 3576205, 'author': 'bob esponja', 'author_id': 19927, 'author_profile': 'https://Stackoverflow.com/users/19927', 'pm_score': 3, 'selected': False, 'text': '<p><code>mountpoint</code> seems to be the best solution to this: it returns 0 if a path is a mount point:</p>\n\n<pre><code>#!/bin/bash\nif [[ `mountpoint -q /path` ]]; then\n    echo "filesystem mounted"\nelse\n    echo "filesystem not mounted"\nfi\n</code></pre>\n\n<p>Found at <a href="http://www.linuxquestions.org/questions/programming-9/bash-check-if-fs-is-already-mounted-on-directory-137282/#post4061230" rel="noreferrer">LinuxQuestions</a>.</p>\n'}, {'answer_id': 24021504, 'author': 'Mike Q', 'author_id': 1618630, 'author_profile': 'https://Stackoverflow.com/users/1618630', 'pm_score': 0, 'selected': False, 'text': '<p>I am skimming This but I would think you would rather rsync -e ssh   and setup the keys to accept the account.</p>\n'}, {'answer_id': 28752314, 'author': 'miu', 'author_id': 2524925, 'author_profile': 'https://Stackoverflow.com/users/2524925', 'pm_score': 1, 'selected': False, 'text': '<ol>\n<li>Copy and paste the script below to a file (e.g. backup.sh).</li>\n<li>Make the script executable (e.g. <code>chmod +x backup.sh</code>)</li>\n<li>Call the script as root with the format <code>backup.sh [username (for rsync)] [backup source device] [backup source location] [backup target device] [backup target location]</code></li>\n</ol>\n\n<p>!!!ATTENTION!!! Don\'t execute the script as root user without understanding the code!</p>\n\n<p>I think there\'s nothing to explain. The code is straightforward and well documented.</p>\n\n<pre><code>#!/bin/bash\n\n##\n## COMMAND USAGE: backup.sh [username] [backup source device] [backup source location] [backup target device] [backup target location]\n##\n## for example: sudo /home/manu/bin/backup.sh "manu" "/media/disk1" "/media/disk1/." "/media/disk2" "/media/disk2"\n##\n\n##\n## VARIABLES\n##\n\n# execute as user\nUSER="$1"\n\n# Set source location\nBACKUP_SOURCE_DEV="$2"\nBACKUP_SOURCE="$3"\n\n# Set target location\nBACKUP_TARGET_DEV="$4"\nBACKUP_TARGET="$5"\n\n# Log file\nLOG_FILE="/var/log/backup_script.log"\n\n##\n## SCRIPT\n##\n\nfunction end() {\n    echo -e "###########################################################################\\\n#########################################################################\\n\\n" &gt;&gt; "$LOG_FILE"\n    exit $1\n}\n\n# Check that the log file exists\nif [ ! -e "$LOG_FILE" ]; then\n        touch "$LOG_FILE"\n    chown $USER "$LOG_FILE"\nfi\n\n# Check if backup source device is mounted\nif ! mountpoint "$BACKUP_SOURCE_DEV"; then\n        echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Backup source device is not mounted!" &gt;&gt; "$LOG_FILE"\n    end 1\nfi\n\n# Check that source dir exists and is readable.\nif [ ! -r  "$BACKUP_SOURCE" ]; then\n        echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to read source dir." &gt;&gt; "$LOG_FILE"\n        echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." &gt;&gt; "$LOG_FILE"\n    end 1\nfi\n\n# Check that target dir exists and is writable.\nif [ ! -w  "$BACKUP_TARGET" ]; then\n        echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to write to target dir." &gt;&gt; "$LOG_FILE"\n        echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." &gt;&gt; "$LOG_FILE"\n    end 1\nfi\n\n# Check if the drive is mounted\nif ! mountpoint "$BACKUP_TARGET_DEV"; then\n        echo "$(date "+%Y-%m-%d %k:%M:%S") - WARNING: Backup device needs mounting!" &gt;&gt; "$LOG_FILE"\n\n        # If not, mount the drive\n        if mount "$BACKUP_TARGET_DEV" &gt; /dev/null 2&gt;&amp;1 || /bin/false; then\n                echo "$(date "+%Y-%m-%d %k:%M:%S") - Backup device mounted." &gt;&gt; "$LOG_FILE"\n        else\n                echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to mount backup device." &gt;&gt; "$LOG_FILE"\n                echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." &gt;&gt; "$LOG_FILE"\n        end 1\n        fi\nfi\n\n# Start entry in the log\necho "$(date "+%Y-%m-%d %k:%M:%S") - Sync started." &gt;&gt; "$LOG_FILE"\n\n# Start sync\nsu -c "rsync -ayhEAX --progress --delete-after --inplace --compress-level=0 --log-file=\\"$LOG_FILE\\" \\"$BACKUP_SOURCE\\" \\"$BACKUP_TARGET\\"" $USER\necho "" &gt;&gt; "$LOG_FILE"\n\n# Unmount the drive so it does not accidentally get damaged or wiped\nif umount "$BACKUP_TARGET_DEV" &gt; /dev/null 2&gt;&amp;1 || /bin/false; then\n    echo "$(date "+%Y-%m-%d %k:%M:%S") - Backup device unmounted." &gt;&gt; "$LOG_FILE"\nelse\n    echo "$(date "+%Y-%m-%d %k:%M:%S") - WARNING: Backup device could not be unmounted." &gt;&gt; "$LOG_FILE"\nfi\n\n# Exit successfully\nend 0\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/27359', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/163/']