You are not logged in.
Hi,
First bash-script here - the tldp.org-guide has gotten me somewhere already, but the final condition 'if rsync...' seems to fail and I don't know why. The script checks if the location to backup the files to is a mountpoint. If not, the script should mount it or die. If it is a mountpoint, rsync should be run.
#!/bin/bash
##
## VARIABLES
##
# Set location to backup from
BACKUP_FROM="/srv/media/"
# Set location to backup to
BACKUP_TO="/media/backup/media/"
BACKUP_DEV="e3434573-ad6f-4c44-8168-391292ba5ec5"
BACKUP_MNT="/media/backup"
# Log file
LOG_FILE="/var/log/script_sync_media.log"
##
## SCRIPT
##
# Check if the log file exists
if [ ! -e $LOG_FILE ]; then
touch $LOG_FILE
fi
# Check if the drive is mounted
if [[ `! mountpoint -q $BACKUP_MNT` ]]; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - Backup device needed mounting!" >> $LOG_FILE
# If not, mount the drive
if [[ `mount -U $BACKUP_DEV $BACKUP_MNT` ]]; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - Backup device mounted." >> $LOG_FILE
else
echo "$(date "+%Y-%m-%d %k:%M:%S") - Unable to mount backup device." >> $LOG_FILE
exit
fi
fi
# Start entry in the log
echo "$(date "+%Y-%m-%d %k:%M:%S") - Sync started." >> $LOG_FILE
# Start sync
if [[ `rsync -a --delete $BACKUP_FROM $BACKUP_TO` ]]; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - Sync completed succesfully." >> $LOG_FILE
else
echo "$(date "+%Y-%m-%d %k:%M:%S") - Sync failed." >> $LOG_FILE
fi
# End entry in the log
echo "" >> $LOG_FILE
exit
It is probably a trivial problem for a bash-professional? The log states 'Sync started.' and immediately 'Sync failed.'...
Thx.
Vincent
Last edited by zenlord (2011-03-05 15:29:41)
Offline
SOLVED:
#!/bin/bash
##
## VARIABLES
##
# Set source location
BACKUP_FROM="/srv/media/"
# Set desination location
BACKUP_TO="/media/backup/media/"
BACKUP_DEV="e3434573-ad6f-4c44-8168-391292ba5ec5"
BACKUP_MNT="/media/backup"
# Log file
LOG_FILE="/var/log/script_sync_media.log"
##
## SCRIPT
##
# Check that the log file exists
if [ ! -e $LOG_FILE ]; then
touch $LOG_FILE
fi
# Check that source dir exists and is readable.
if [ ! -r $BACKUP_FROM ]; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to read source dir." >> $LOG_FILE
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." >> $LOG_FILE
echo "" >> $LOG_FILE
exit
fi
# Check if the drive is mounted
if ! mount | grep $BACKUP_MNT >/dev/null; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - WARNING: Backup device needs mounting!" >> $LOG_FILE
# If not, mount the drive
if mount -U $BACKUP_DEV $BACKUP_MNT; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - Backup device mounted." >> $LOG_FILE
else
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to mount backup device." >> $LOG_FILE
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." >> $LOG_FILE
echo "" >> $LOG_FILE
exit
fi
fi
# Start entry in the log
echo "$(date "+%Y-%m-%d %k:%M:%S") - Sync started." >> $LOG_FILE
# Start sync
if rsync -a -v --delete $BACKUP_FROM $BACKUP_TO &>> $LOG_FILE; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - Sync completed succesfully." >> $LOG_FILE
else
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Sync failed." >> $LOG_FILE
fi
# End entry in the log
echo "" >> $LOG_FILE
exit
The bug was somewhere in my usage of [[``]] in the conditional.
//EDIT: I didn't c/p the whole script - Fixed
Last edited by zenlord (2011-03-05 17:15:01)
Offline
grep has -q as well, but parsing the output of mount usually isn't the way to go. The easiest solution is to check for the existance of a file on the mounted volume, generally a dotfile that you make for this purpose. Also, be sure to quote every variable you're expanding. This will have issues with spaces in your variables.
Offline
There is also the `mountpoint` command which existence is purely to test if a directory path is mounted or not
# Check if the drive is mounted
if ! mountpoint "$BACKUP_MNT" ; then
....
Last edited by fukawi2 (2011-03-07 22:36:46)
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
OK, a little bit later than I should have replied, but I have incorporated the tips provided above, and a few other changes:
* added an extra check for the target dir
* applied general exit codes
* added unmount after running the script (which makes the earlier warning if the device nees to be mounted a little superfluous)
Here goes:
#!/bin/bash
##
## VARIABLES
##
# Set source location
BACKUP_FROM="/srv/media/"
# Set target location
BACKUP_TO="/media/backup/media/"
BACKUP_DEV="xxxxxxx-xxxxx-xxxxxxxxxxxxxxx" #UUID of the disk
BACKUP_MNT="/media/backup"
# Log file
LOG_FILE="/var/log/script_sync_media.log"
##
## SCRIPT
##
# Check that the log file exists
if [ ! -e "$LOG_FILE" ]; then
touch "$LOG_FILE"
fi
# Check that source dir exists and is readable.
if [ ! -r "$BACKUP_FROM" ]; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to read source dir." >> "$LOG_FILE"
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
exit 1
fi
# Check that target dir exists and is writable.
if [ ! -w "$BACKUP_TO" ]; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to write to target dir." >> "$LOG_FILE"
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
exit 1
fi
# Check if the drive is mounted
if ! mountpoint "$BACKUP_MNT"; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - WARNING: Backup device needs mounting!" >> "$LOG_FILE"
# If not, mount the drive
if [ mount -U "$BACKUP_DEV" "$BACKUP_MNT" ]; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - Backup device mounted." >> "$LOG_FILE"
else
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to mount backup device." >> "$LOG_FILE"
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
exit 1
fi
fi
# Start entry in the log
echo "$(date "+%Y-%m-%d %k:%M:%S") - Sync started." >> "$LOG_FILE"
# Start sync
if rsync -a -v --delete "$BACKUP_FROM" "$BACKUP_TO" &>> "$LOG_FILE"; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - Sync completed succesfully." >> "$LOG_FILE"
else
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: rsync-command failed." >> "$LOG_FILE"
echo "$(date "+%Y-%m-%d %k:%M:%S") - ERROR: Unable to sync." >> "$LOG_FILE"
echo "" >> "$LOG_FILE"
exit 1
fi
# Unmount the drive so it does not accidentally get damaged or wiped
if [ umount "$BACKUP_MNT" ]; then
echo "$(date "+%Y-%m-%d %k:%M:%S") - Backup device unmounted." >> "$LOG_FILE"
else
echo "$(date "+%Y-%m-%d %k:%M:%S") - WARNING: Backup device could not be unmounted." >> "$LOG_FILE"
fi
# End entry in the log
echo "" >> "$LOG_FILE"
exit 0
Usage:
1. c/p to a .sh-file
2. Make the file executable (chmod +x <file>.sh)
3. set an entry in crontab to run this script daily/weekly/monthly
Maybe this little script helps other bash newbies...
Last edited by zenlord (2012-09-23 21:08:32)
Offline