sasbro97 Posted August 16, 2023 Share Posted August 16, 2023 In Anlehnung an mein Thema frage ich mich, ob und wenn ja, wie es geht, dass ich einen angeschlossenen USB-Stick automatisch bspw. jeden 01. des Monats um 02:00 mounten lasse und dann eben ein Backup Skript laufen lasse und wieder unmounte. Wäre das dann ein Device Script und wie würde es aussehen? Hab noch keinen Beitrag mit diesen Anforderungen gesehen. Quote Link to comment
dlandon Posted August 16, 2023 Share Posted August 16, 2023 The script that runs when the device is plugged in is the device script. Plug the device in and then click the Edit Device Settings icon (three gears). Then click the 'Default' script button and fill in your commands in the appropriate event section. Once you are happy with the script, switch on the 'Auto Mount' switch and then when you plug in the USB device the script will run. You can unmout the disk in the script when it has completed the operations. Quote Link to comment
DataCollector Posted August 16, 2023 Share Posted August 16, 2023 1 hour ago, dlandon said: The script that runs when the device is plugged in is the device script. Plug the device in and then click the Edit Device Settings icon (three gears). Then click the 'Default' script button and fill in your commands in the appropriate event section. Once you are happy with the script, switch on the 'Auto Mount' switch and then when you plug in the USB device the script will run. You can unmout the disk in the script when it has completed the operations. I understand his question in another way: He wants to keep the Stick in the pc permanently. But in Windows you can eject the Stick and the OS does not see it, even if it is connected. He wants this state in unraid also: So he wants to let the os detect the stick triggered with a script/timer, than it should run a backup and after that the device should be ejected again, without really disconnecting. Quote Link to comment
alturismo Posted August 16, 2023 Share Posted August 16, 2023 3 hours ago, sasbro97 said: Hab noch keinen Beitrag mit diesen Anforderungen gesehen. gib dem Gerät mal einen Namen in UAD, bitte ohne Leer oder Sonderzeichen, Bsp. "USBHDD" dann teste mal im Unraid Terminal wie folgt und beobachte ob die disk unmounted, mounted wird ... /usr/local/sbin/rc.unassigned unmount name=USBHDD /usr/local/sbin/rc.unassigned mount name=USBHDD wenn ja, wäre der Rest jetzt ein script zu erstellen was dann die Aufgabe übernimmt in user scripts, oder mal testen was passiert mit einem UAD script wenn die Platte per Befehl mounted wird ... dann wäre es ja nur der mount Befehl in user scripts. Quote Link to comment
dlandon Posted August 16, 2023 Share Posted August 16, 2023 What you want to do is to install the "User Scripts" plugin and set up a script to run at a set time. I use this script to mount a disk, do a backup, and unmount the disk when it's done. #!/bin/bash #description=Backup Music Folder. #backgroundOnly=true #arrayStarted=true #clearLog=true # LOGFILE="/tmp/unassigned.devices/logs/Music_Backup.log" # Mount the backup disk /usr/local/sbin/rc.unassigned mount name=MusicBk # Backup the Music folder to the backup disk rsync -av --delete --exclude .Recycle.Bin '/mnt/user/Music' '/mnt/disks/Music Backup/' 2>&1 >> $LOGFILE # Unmount the disk /usr/local/sbin/rc.unassigned umount name=MusicBk You can have the disk detach by loading the default script in UD and uncomment the line to detach it: 'REMOVE' ) # do your stuff here # Spin down disk - uncomment this if you want the disk to be spun down after the disk is unmounted /usr/local/sbin/rc.unassigned spindown $DEVICE # Detach the disk - uncomment this if you want the USB disk to be detached after it is unmounted /usr/local/sbin/rc.unassigned detach $DEVICE /usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Unassigned Devices" -d "Device unmounted" -i "normal" ;; 1 2 Quote Link to comment
sasbro97 Posted August 17, 2023 Author Share Posted August 17, 2023 20 hours ago, DataCollector said: He wants to keep the Stick in the pc permanently. Yes. 19 hours ago, alturismo said: dann teste mal im Unraid Terminal wie folgt und beobachte ob die disk unmounted, mounted wird Klappt. Command ist nur wie in Linux umount und nicht unmount. 13 hours ago, dlandon said: I use this script to mount a disk, do a backup, and unmount the disk when it's done. This is perfect! 1 Quote Link to comment
sasbro97 Posted August 17, 2023 Author Share Posted August 17, 2023 (edited) 14 hours ago, dlandon said: 'REMOVE' ) # do your stuff here # Spin down disk - uncomment this if you want the disk to be spun down after the disk is unmounted /usr/local/sbin/rc.unassigned spindown $DEVICE # Detach the disk - uncomment this if you want the USB disk to be detached after it is unmounted /usr/local/sbin/rc.unassigned detach $DEVICE /usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Unassigned Devices" -d "Device unmounted" -i "normal" ;; This doesn't work for me. Am I doing it right? It shows the following error logs: /tmp/unassigned.devices/scripts/SanDisk_Ultra.sh: line 1: syntax error near unexpected token `)' /tmp/unassigned.devices/scripts/SanDisk_Ultra.sh: line 1: ` 'REMOVE' )' Okay got it working with the template now. The device completely disappeared from Unassigened Devices. Should this happen? It's now a historial device. I cannot mount it anymore. Command show true but nothing is mounted. Edited August 17, 2023 by sasbro97 Quote Link to comment
dlandon Posted August 17, 2023 Share Posted August 17, 2023 35 minutes ago, sasbro97 said: Okay got it working with the template now. The device completely disappeared from Unassigened Devices. Should this happen? It's now a historial device. I cannot mount it anymore. Command show true but nothing is mounted. You need to use the complete 'Default' script and change the 'REMOVE' section. The script you have is not complete. You asked for the device to be detached. Click on the three gears on the Historical device and click the "Attach" button and the device will then show up in UD. Quote Link to comment
sasbro97 Posted August 17, 2023 Author Share Posted August 17, 2023 35 minutes ago, dlandon said: You need to use the complete 'Default' script and change the 'REMOVE' section. The script you have is not complete. You asked for the device to be detached. Click on the three gears on the Historical device and click the "Attach" button and the device will then show up in UD. Can I attach it via a script again then? My script looks like this: #!/bin/bash PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin ## Available variables: # ACTION - if mounting, ADD; if unmounting, UNMOUNT; if unmounted, REMOVE; if error, ERROR_MOUNT, ERROR_UNMOUNT # DEVICE - partition device, e.g. /dev/sda1 # UD_DEVICE - unassigned devX designation # SERIAL - disk serial number # LABEL - partition label # LUKS - if the device is encrypted, this is the partition device, e.g. /dev/sda1 # FSTYPE - partition filesystem # MOUNTPOINT - where the partition is mounted # OWNER - "udev" if executed by UDEV, otherwise "user" # PROG_NAME - program name of this script # LOGFILE - log file for this script case $ACTION in 'REMOVE' ) # do your stuff here # Spin down disk - uncomment this if you want the disk to be spun down after the disk is unmounted /usr/local/sbin/rc.unassigned spindown $DEVICE # Detach the disk - uncomment this if you want the USB disk to be detached after it is unmounted /usr/local/sbin/rc.unassigned detach $DEVICE /usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Unassigned Devices" -d "Device unmounted" -i "normal" ;; esac Quote Link to comment
dlandon Posted August 17, 2023 Share Posted August 17, 2023 55 minutes ago, sasbro97 said: Can I attach it via a script again then? Unfortunately, no. I just realized you can with this command: /usr/local/sbin/rc.unassigned attach 'deviceserialnumber' Quote Link to comment
Solution hawihoney Posted August 17, 2023 Solution Share Posted August 17, 2023 (edited) On 8/16/2023 at 3:37 AM, sasbro97 said: frage ich mich, ob und wenn ja, wie es geht, dass ich einen angeschlossenen USB-Stick automatisch bspw. jeden 01. des Monats um 02:00 mounten lasse und dann eben ein Backup Skript laufen lasse und wieder unmounte. Wäre das dann ein Device Script und wie würde es aussehen? Für diese Anforderung machst Du das mit dem User Script Plugin und nicht mit dem Device Script. Die Kommandos hierzu findest Du oben sowie in der Hilfe der Unassigned Devices Seite: Du musst das in etwa so machen: #!/bin/bash #backgroundOnly=true #clearLog=true # Mounten /usr/local/sbin/rc.unassigned mount name=diskname ### Hier machst Du was immer Du machen willst ### rsync -avPX --delete-during /mnt/user/<Deine Quelle>/ /mnt/disks/<Dein USB Stick>/ # Und wieder unmounten /usr/local/sbin/rc.unassigned umount name=diskname Danach noch Cron Einträge für dieses User Script setzen. Die Hilfe hierzu ist auf der User Scripts Seite verlinkt. Deine Anforderung läuft auf "Custom: 0 2 1 * *" hinaus. Einzige Bedingung: Stick bleibt immer stecken wie von Dir angegeben. Ich hänge dazu mal ein Bild vom meinen User Scripts an. Edited August 17, 2023 by hawihoney 3 Quote Link to comment
sasbro97 Posted August 17, 2023 Author Share Posted August 17, 2023 Thank you for your help @dlandon! Und natürlich vielen Dank @hawihoney. Dachte das muss per Device Script sein. So per User Script ist mir das auch lieber Quote Link to comment
Greyberry Posted August 19, 2023 Share Posted August 19, 2023 Kann man das User Script irgendwie über das Device Script aufrufen? Quote Link to comment
hawihoney Posted August 19, 2023 Share Posted August 19, 2023 (edited) 1 hour ago, Greyberry said: Kann man das User Script irgendwie über das Device Script aufrufen? Keine Ahnung, ich denke nicht. Das sind meinem Verständnis nach zunächst einmal zwei Paar Schuhe: 1.) Ein Stick/Device, wie hier in diesem Thread gefordert, steckt immer drin und wird 1x pro Monat automatisch gemounted, beschrieben und wieder abgemeldet --> User Script 2.) Du willst einen Stick einstecken, dann das Skript automatisch laufen lassen, ... --> Device Script Ein Device Skript sieht schon inhaltlich ganz anders aus als ein User Skript. Guck Dir das mal an. Du könntest vielleicht den eigentlichen Job in ein User Script packen und diesen aus dem Device Script aufrufen. Aber so weit bin ich noch nie gegangen. Bei mir rennen manuell/automatisch dutzende Skripte - jeden Tag, jede Woche, jeden Monat, jedes Jahr, beim Start des Arrays, etc. etc. etc. das reicht mir. Edited August 19, 2023 by hawihoney Quote Link to comment
dlandon Posted August 19, 2023 Share Posted August 19, 2023 Cut and paste the User Script text into the proper action area of the Device Script. Quote Link to comment
hawihoney Posted August 19, 2023 Share Posted August 19, 2023 (edited) 18 minutes ago, dlandon said: Cut and paste the User Script text into the proper action area of the Device Script. Ahem, don't I need the ACTION ADD, etc. stuff then? I simply put a Python script to rsync something from A to B into that Device Script field? And that's all? Who does mount then? If this is true, I never got the help text on that page ... Edited August 19, 2023 by hawihoney Quote Link to comment
dlandon Posted August 19, 2023 Share Posted August 19, 2023 Try this procedure: Set up your device script to do what you want when the device is plugged in. Use the default script and don't remove any events. They don't do anything unless you add code, and your code will need to use the correct event. Call your python script in the UD device script. Set the device to auto mount. Your UD device script will look like this: #!/bin/bash # # Available variables: # # ACTION - if mounting, ADD; if unmounting, UNMOUNT; if unmounted, REMOVE; if error, ERROR_MOUNT, ERROR_UNMOUNT # DEVICE - partition device, e.g. /dev/sda1 # UD_DEVICE - unassigned devX designation # SERIAL - disk serial number # LABEL - partition label # LUKS - if the device is encrypted, this is the partition device, e.g. /dev/sda1 # FSTYPE - partition filesystem # MOUNTPOINT - where the partition is mounted # OWNER - "udev" if executed by UDEV, otherwise "user" # PROG_NAME - program name of this script # LOGFILE - log file for this script case $ACTION in 'ADD' ) /usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Unassigned Devices" -d "Device mounted" -i "normal" if [ "$OWNER" = "udev" ]; then # put your python script execution here execute python script <====== your script else # do your user initiated stuff here sleep 1 fi ;; 'UNMOUNT' ) # do your stuff here /usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Unassigned Devices" -d "Device unmounting" -i "normal" ;; 'REMOVE' ) # do your stuff here # Spin down disk - uncomment this if you want the disk to be spun down after the disk is unmounted /usr/local/sbin/rc.unassigned spindown $DEVICE # Detach the disk - uncomment this if you want the USB disk to be detached after it is unmounted /usr/local/sbin/rc.unassigned detach $DEVICE /usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Unassigned Devices" -d "Device unmounted" -i "normal" ;; 'ERROR_MOUNT' ) # do your stuff here /usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Unassigned Devices" -d "Error mounting device" -i "alert" ;; 'ERROR_UNMOUNT' ) # do your stuff here /usr/local/emhttp/webGui/scripts/notify -e "Unraid Server Notice" -s "Unassigned Devices" -d "Error unmounting device" -i "alert" ;; esac Now when you plug in the device the script will execute and perform your python stuff. Notice the 'REMOVE' event will spin down and detach your device. Leave the device installed and detached. Once you have it running, set up a User Script on a cron to attach the device. The 'Attach' creates a udev hot plug event. UD will then mount the device (if auto mount is on) and run the device script. When it's finished the device will be spun down and detached again. 2 1 Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.