Skip to main content

Thread: Run script when specific e-SATA hdd is mounted


hi all,
can not find solution, how run script, when mount e-sata harddrive. found solution, when use disk connected via usb, nothing consider sata devices. use disk backuping. answers.

your best bet udev in case.
hade same issue , used udev rule run script whenever partition added system, volume label matched pattern.
in case chose store actual backup-script on external drive well.
there 2 parts in case, script on external hdd.
example:
code:
#!/bin/bash  source_dir=".../"  backup_dir=".../"  rdiff-backup --print-statistics "$source_dir" "$pwd/$backup_dir"
the second part script called udev, when rule matches, mounts partition, runs backup-script, if exists, umounts drive , sends report via e-mail.
code:
#!/bin/bash  function canonical () {  	if pushd -- "$(dirname -- "$1")" >/dev/null;  		echo "$pwd/$(basename "$1")"  		popd >/dev/null  	fi  	exit $?  }    rules_name="/etc/udev/rules.d/hdd-backup.rules"    case "$1" in  --enable)  	exec udevadm control --property=hdd_backup_disable=no  	;;  --disable)  	exec udevadm control --property=hdd_backup_disable=yes  	;;  --install)  	email="${2:-root}"  	backup_script="${3:-backup-script}"  	label_pattern="${4:-backup_*}"  	script_name="$(canonical "$0")" ||*exit $?  	exec cat >"$rules_name" <<-eof  		subsystem=="block", action=="add", env{hdd_backup_disable}!="yes", env{id_fs_label}=="$label_pattern", run+="$script_name --run-backup '$email' '$backup_script' &"  	eof  	;;  --uninstall)  	exec rm "$rules_name"  	;;  --run-backup)  	email="$2"  	backup_script="$3"  	[ -n "$email" ] && [ -n "$devname" ] && [ -n "$backup_script" ] || exit 1  	;;  *)  	cat <<-eof  		options:  		--enable  		    enable run-backup.  		--disable  		    temporarily disable run-backup.  		--install [email] [pattern] [script]  		    install udev rule.  		    if drive partition label matches  		    pattern (default: backup_*) inserted , contains  		    executable file named script (default: backup-script),  		    executed , result mailed  		    email (default: root).  		    afterwards drive unmounted again.  		--uninstall  		    remove udev rule.  	eof  	exit 1  	;;  esac    temp="$(mktemp -d /tmp/run-backup.xxx 2>&1)" || {  	err="$?"  	mail -s "backup failed!" "$email" <<-eof  		mktemp: $temp  	eof  	exit $err  }    function mountandrun () {  	mountpoint="$1"  	result=0  	if mkdir -p "$mountpoint";  		if mount "$devname" "$mountpoint";  			if [ -x "$mountpoint/$backup_script" ];  				if pushd "$mountpoint" >/dev/null;  					if "./$backup_script";  						sync  					else  						result="$?"  					fi  					popd >/dev/null  				else  					result="$?"  				fi  			else  				echo "$backup_script not found or not executable." >&2  				result=1  			fi  			umount "$mountpoint"  			[ "$result" == "0" ] && hdparm -y "$devname" >/dev/null  		else  			result="$?"  		fi  		rmdir "$mountpoint"  	else  		result="$?"  	fi  	  	return "$result"  }    if mountandrun "$temp/mnt" >"$temp/output" 2>&1;  	subject="backup successful."  else  	subject="backup failed!"  fi    mail -s "hdd-backup: $subject" "$email" < "$temp/output"    rm "$temp/output" && rmdir "$temp"
disclaimer:
use script @ own risk.
seems work on server, on yours might delete important files , play rebecca black on endless loop.


Forum The Ubuntu Forum Community Ubuntu Official Flavours Support Hardware [ubuntu] Run script when specific e-SATA hdd is mounted


Ubuntu

Comments

Popular posts from this blog

Joomla 3.3 Installation Error message - Joomla! Forum - community, help and support

Multilanguage infinite redirect loop error. - Joomla! Forum - community, help and support

trim media limit reached