Disable/Enable Samba Share with cronjob

    • OMV 4.x
    • Resolved

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Disable/Enable Samba Share with cronjob

      Hello everbody,

      I'm using openmediavault as a storage system for backups. I created multiple samba shares for the backups. Every computer has a own share. I want do disable/enable the shares at specific times with a cronjob. I'm able to disable/enable it manually with the control panel but don't found a solution to enable/disable it with a command. Is there anny command or plugin for this?

      Best regards
      NorthernRoot
    • One simple possibility could be to use cron to run a script that change the access rights for the shared folder. Using chmod and/or chown.

      However I think it would be better to share the rights of a subfolder one level down. So that the share is still fully functional, but it will look empty or be inaccessible. That way OMV will function as normal without errors.

      I haven't tested it, but perhaps something like this:

      /sharedfolders/backup_computer_a/storage_a/
      /sharedfolders/backup_computer_b/storage_b/

      The actual shares are backup_computer_a and backup_computer_b.

      To make the contents of the shares appear or disappear, change the access rights to storage_a or storage_b using chmod in a script. You will have to experiment to see if you also need to change ownership using chown. Possibly using the recursive switch?

      It is possible that shares are "cached" so that the changes don't appear at once. Then there may be errors until the share is refreshed. I'm not sure. You'll have to test it. One extreme option could be to reboot the OMV server. Especially if it is run as a VM.
      OMV 4, 7 x ODROID HC2, 1 x ODROID HC1, 5 x 12TB, 1 x 8TB, 1 x 2TB SSHD, 1 x 2TB SSD, GbE, WiFi mesh
    • Hello @Adoby and @subzero79,

      thank you both for the great ideas and the help. I found a solution and want to present it for everbody if they want to do it as well:

      Shell-Script: smbtimer

      1. #!/bin/sh
      2. set -e
      3. . /etc/default/openmediavault
      4. . /usr/share/openmediavault/scripts/helper-functions
      5. index=$(omv_config_get_count "//services/smb/shares/share")
      6. while [ ${index} -gt 0 ]; do
      7. # Get the UUID of the current share.
      8. uuid=$(omv_config_get "//services/smb/shares/share[position()=${index}]/uuid")
      9. # Get shared folder name.
      10. enabled=$(omv_config_get "//services/smb/shares/share[uuid='${uuid}']/enable")
      11. sfref=$(omv_config_get "//services/smb/shares/share[uuid='${uuid}']/sharedfolderref")
      12. name=$(omv_get_sharedfolder_name "${sfref}")
      13. if [ "$1" = "enable" ]; then
      14. if [ "${enabled}" = "0" ]; then
      15. if [ "${name}" = "BackupTEST" ]; then
      16. omv_config_update "//services/smb/shares/share[uuid='${uuid}']/enable" 1
      17. omv-mkconf samba
      18. fi
      19. fi
      20. elif [ "$1" = "disable" ]; then
      21. if [ "${enabled}" = "1" ]; then
      22. if [ "${name}" = "BackupTEST" ]; then
      23. omv_config_update "//services/smb/shares/share[uuid='${uuid}']/enable" 0
      24. omv-mkconf samba
      25. service smbd restart
      26. fi
      27. fi
      28. fi
      29. index=$(( ${index} - 1 ))
      30. done
      Display All
      ./smbtimer enable #Enables the share BackupTEST
      ./smbtimer disable #Disable the share BackupTEST

      The entries inside the crontab is self explaining.
      I used this post as a reference: Samba 'access based share enum' workaround for workgroups (Hide shares that users don't have access to)

      A small Note to the command 'service smbd restart': Windows reacts a bit wired when I disable the shared Folder. I'm still able to connect to it and use it but the share is not inside the /etc/samba/smb.conf. To make 100% shure the share is not available to the System anymore I restart the samba service. I was worried that current connections cut off during the restart of the service but it seems that they survive it. There is also a Killall command to tell Samba to reload the smb.conf file again but windows still was able to access the shared folder.

      The post was edited 1 time, last by NorthernRoot: Formatting ().