# [FreeNAS] (auto) mount, rsync, dismount script



## bigalnz (Jan 1, 2011)

Hi All, 

I have got a box here running FreeNAS and have started to hack at someone else's script to make it do what I want. I know this is not the Freenas forums but i have not had much luck there and its FreeBSD based.....

In particular his script was; 

1) encrypting the USB attached device - which I don't need. 
2) repeated beeping when finished (one long beep would be fine) 
3) I dont know what the A16P echoing is all about 

I am not quite there yet, and hope someone here can help. What I want 
to do it: 

1. Automount USB disk when plugged in 
2. Rysnc certain directories 
3. Dismount when finished 
4. Send email with some sort of details (ie failure of 1,2,3 or 4, and 
also upon success of 2 (rsync)). 

So far this is what I have ended up with, some of the folders I have 
hard coded. 


```
#!/bin/bash -x 
key_file=/temp/ukey.key 
device=da${1//[a-z]/} 
origin=/mnt/DATA/ 
target=/mnt/secret 
echo ${device} 
echo ${target} 
_start() { 
    if [ ! -f /var/run/${0##*/}.pid ];then 
        echo $$ > /var/run/${0##*/}.pid 
    else 
        /usr/bin/logger "Error syncing $origin on $device"; 
        echo "A16P16A16P16A16P16CP8CP8CP8A16P16A16P16A16" > /dev/ 
speaker 
        _finish; 
    fi 
} 

_finish() { 
    if [ -f /var/run/${0##*/}.pid ];then 
        rm /var/run/${0##*/}.pid; 
    fi 
    exit 
} 

_mount() { 
    /sbin/geli attach -p -k ${key_file} /dev/$device 
    /bin/mkdir -p ${target} && 
    /sbin/mount /dev/${device}.eli ${target}/ && 
    /sbin/fsck_ufs -pB ${target}; 
} 

_umount() { 
    /sbin/umount /dev/${device}.eli; 
    /bin/rm -r ${target}; 
    /bin/sync /dev/${device}.eli 
    /sbin/geli detach /dev/${device}; 
    /bin/sync /dev/${device} 
} 

_sync_func() { 
    /usr/local/bin/rsync -a --log-file=${origin}/backup.log ${origin}/ 
${target}/ 
    /usr/bin/logger "Finish syncing $origin on $device"; 
} 

_start; _mount; _sync_func; _umount; 
while [ -e /dev/da0 ];do echo "A8P8A8P8A8" > /dev/speaker;sleep 
10;done 
_finish;
```

Thanks in advance, 
-Al


----------



## DutchDaemon (Jan 1, 2011)

Note: Topics about PC-BSD / FreeNAS / DesktopBSD / m0N0WALL / pfSense
Watch out when applying any information based on FreeBSD. FreeNAS may be based on, but is absolutely not the same as, FreeBSD.


----------



## bigalnz (Jan 1, 2011)

DutchDaemon said:
			
		

> Note: Topics about PC-BSD / FreeNAS / DesktopBSD / m0N0WALL / pfSense
> Watch out when applying any information based on FreeBSD. FreeNAS may be based on, but is absolutely not the same as, FreeBSD.



Absolutely understood. I only come here after trying their forums, attempting it myself and plenty of googling.

-Al


----------



## Gruelius (Feb 18, 2012)

*I* have the same requirement except it*'*s a freebsd FreeBSD server. *I'*ts for a USB drive based syncing of a 12 TB array between a freebsd FreeBSD server, a FreeNAS box, and a Linux based off-the-shelf NAS.

*O*n Server B:

```
cd /mnt/nas
find . -type f -print > /mnt/tferdisk/nas.files
```

*O*n Server A:

```
cd /ztank
find . -type f -print > /mnt/tferdisk/mtank.files
cd /mnt/tferdisk/
sort mtank.files nas.files | uniq -u > tocopy.list
rsync -d --files-from=tocopy.list /ztank /mnt/usbdisk/tferdir
rm /mnt/tferdisk/tocopy.list
```

*O*n Server B:

```
mv /mnt/tferdisk/tferdir/* /mnt/nas/
cd /mnt/nas
find . -type f -print > /mnt/tferdisk/nas.files
```


----------

