Use cases and correspnd scripts (everyday life solutions)

nnCron and nnCron LITE discussion

Use cases and correspnd scripts (everyday life solutions)

Postby iScape » Mon, 16 Apr 2007, 16:43

nnCron tasks "kit" that improves several aspects of USB stick usage as a "portable" workplace (IM, browser, documents, etc).
Increases operation speed; reduces number r/w requests to USB drive and therefore (hopefully) extends drive lifetime. Takes time for sync:) A kind of simple back-up and a chance to get lost in 3 copies (home, work, USB):)
Supported use cases:
1. Start work session.
2. End work session.
3. Workstation reboot/shutdown.
4. Notification about access to temporary data location to prevent possible edit of inactive copy of data.

The script is in test for 3 days. Pls, be carefull while using it.

Code: Select all
# File/ver:         USBFlash.tab / v1.0.3
# Author:            Pavel Gorbanj
# Description:   nnCron tasks "kit" that simplifies USB stick usage as a "portable" workplace (IM, browser, documents, etc).
#                        Increases operation speed; reduces number r/w requests to USB drive and therefore ("hopefully") extends drive lifetime.
#   Use cases:      1. Start work session.
#                        2. End work session.
#                        3. Workstation reboot/shutdown.
#                        4. Notification about access to temporary data location to prevent possible edit of inactive copy of data.
# Usage:            Ctrl+Alt + M(ount)/U(nmount)/R(emount). Mount autoruns with WinLogon; Unmount with Logoff/Shutdown;

# Dependences:   nnBackup (www.nncron.ru), remount (www.uwe-sieber.de), RemoveDrive (www.uwe-sieber.de), sync (www.SysInternals.com)
# Notes:            For TODO/"Setup", pls, navigate to the end of the file.
#                        Fixes/enhancements/suggestions are Welcome.
# Warning!:         the code can't be considered as a fine tested therefore, pls, be carefull while using it for the first time (and after:)
#                        THE SCRIPT IS DISTRIBUTED "AS IS". NO WARRANTY OF ANY KIND IS EXPRESSED OR IMPLIED. YOU USE IT AT YOUR OWN RISK. THE AUTHOR WILL NOT BE LIABLE FOR DATA LOSS, DAMAGES, LOSS OF PROFITS OR ANY OTHER KIND OF LOSS WHILE USING OR MISUSING THIS SOFTWARE.
# Created:         14.04.2007
# Modified:         <date> (<description>)

#( Variables_declaration
   : sOriginalFlashDriveLetter S" K:" ; \ Drive letter that the Flash is using (for certain hard-coded links)
   : sTemporaryFlashDriveLetter S" U:" ; \ Drive letter that will be used for temporary Flash mounting while working from main source
   : sFlashFilesAtHDDPath S" D:/K" ; \ Path to Flash files at HDD

   : snnBackUpPath S" C:/Program Files/nnBackup" ;
   : snnBackUpParams S" sync -delete-before -c -dx '*PortableFirefox/profile/Cache/' -s -e -v -log D:/USBFlashSync.log" ;
   : sFlashUtilsPath S" C:/Program Files/USBFlashUtils" ; \ remount.exe, removedrive.exe, etc

   : sFlashInitProgram S" K:/Program Files/PStart/PStart.exe" ; \ Path to PStart (handles start/stop of apps IM, reminders, etc)

   VARIABLE SEM_MOUNT_UNMOUNT_IS_RUNNING \ Semaphore for syncing Mount/Unmount activities

   \ Hints-specific constants
   0x000000 CONSTANT HINT_BG_COLOR
   0xFFFFFF CONSTANT HINT_FG_COLOR
   18 CONSTANT HINT_FONT_SIZE
   : HINT_FONT_NAME S" Arial" ;
)#

# Runs
#      1. Automatically when USB Flash is detected by OS
#      2. Manually by user request
#( USB_Flash_Mount
   WatchDriveInsert: "%sOriginalFlashDriveLetter%"
   WatchHotKey: "^@M" \ ctrl + alt + "M"

   Rule: QUERY: "Mount?"

   SingleInstance

   Action:
      SEM_MOUNT_UNMOUNT_IS_RUNNING GET

      \ Disable task that watches for changes of temporary locations. (bug) 2
      \ TODO
      \ USB_Flash_Handle_Requests_to_Flash_location_at_HDD CF-ACTIVE CLR-CRON-FLAG
      \ USB_Flash_Handle_Requests_to_Flash_Temporary_Drive CF-ACTIVE CLR-CRON-FLAG

      \ Configure hints
      \ HINT_BG_COLOR HINT_FG_COLOR HINT-COLOR
      \ S" %HINT_FONT_NAME%" HINT_FONT_SIZE HINT-FONT

      \ Flush buffers for removable drive
      S" Flush buffers StartIn: %sFlashUtilsPath%" CRON-LOG
      StartIn: "%sFlashUtilsPath%"
      S" sync.exe -r %sOriginalFlashDriveLetter%" CRON-LOG
      START-APPW: sync.exe -r %sOriginalFlashDriveLetter%

      \ Sync files from Flash to Local folder
      \ HINT: "Syncronizing from Flash to HDD..."
      BALLOON: "%CUR-TASK-NAME%" "Syncronizing from Flash to HDD..."
      S" nnBackup: %snnBackUpPath%" CRON-LOG
      StartIn: "%snnBackUpPath%"
      S" nnbackup.exe %snnBackUpParams% -i %sOriginalFlashDriveLetter% -o %sFlashFilesAtHDDPath%" CRON-LOG
      START-APPW: nnbackup.exe %snnBackUpParams% -i %sOriginalFlashDriveLetter% -o %sFlashFilesAtHDDPath%
      \ HINT-OFF

      \ "Shift" real USB drive to another drive letter U:
      \ HINT: "Shifting USB drive to reserved drive letter..."
      BALLOON: "%CUR-TASK-NAME%" "Shifting USB drive to reserved drive letter..."
      S" Remount StartIn: %sFlashUtilsPath%" CRON-LOG
      StartIn: "%sFlashUtilsPath%"
      S" remount.exe %sOriginalFlashDriveLetter% %sTemporaryFlashDriveLetter%" CRON-LOG
      START-APPW: remount.exe %sOriginalFlashDriveLetter% %sTemporaryFlashDriveLetter%
      \ HINT-OFF

      \ Map Local folder to K:
      \ HINT: "Mapping HDD location to regular USB drive letter..."
      BALLOON: "%CUR-TASK-NAME%" "Mapping HDD location to regular USB drive letter..."
      S" subst %sOriginalFlashDriveLetter% %sFlashFilesAtHDDPath%" CRON-LOG
      START-APPW: subst %sOriginalFlashDriveLetter% %sFlashFilesAtHDDPath%
      \ HINT-OFF

      \ Run PStart from K:
      \ HINT: "Initializing USB drive manager..."
      BALLOON: "%CUR-TASK-NAME%" "Initializing USB drive manager..."
      S" PStart StartIn: %sFlashInitProgram ONLYDIR%" CRON-LOG
      StartIn: "%sFlashInitProgram ONLYDIR%"
      S" PStart exe: %sFlashInitProgram ONLYNAME%" CRON-LOG
      START-APP: "%sFlashInitProgram ONLYNAME%"
      \ HINT-OFF

      \ Enable task that watches for changes of temporary locations. (bug) 2
      \ TODO
      \ USB_Flash_Handle_Requests_to_Flash_location_at_HDD @ CF-ACTIVE SET-CRON-FLAG
      \ USB_Flash_Handle_Requests_to_Flash_Temporary_Drive @ CF-ACTIVE SET-CRON-FLAG

      \ Establish periodical backups. (enh) 6
      \ TODO

      \ THINT: "Mount task is complete" 10
      BALLOON: "%CUR-TASK-NAME%" "Mount task is complete"

      SEM_MOUNT_UNMOUNT_IS_RUNNING RELEASE
)#

# Run (manually) if you want to remove USB Flash from the system
#( USB_Flash_Unmount
   WatchHotKey: "^@U" \ ctrl + alt + "U"

   Rule: FILE-EXIST: "%sTemporaryFlashDriveLetter%" FILE-EXIST: "%sOriginalFlashDriveLetter%" AND

   SingleInstance

   Action:
      QUERY: "Unmount?" NOT
      IF EXIT THEN

      SEM_MOUNT_UNMOUNT_IS_RUNNING GET

      \ Disable task that watches for changes of temporary locations. (bug) 2
      \ TODO
      \ USB_Flash_Handle_Requests_to_Flash_location_at_HDD CF-ACTIVE CLR-CRON-FLAG
      \ USB_Flash_Handle_Requests_to_Flash_Temporary_Drive CF-ACTIVE CLR-CRON-FLAG

      \ Configure hints
      \ HINT_BG_COLOR HINT_FG_COLOR HINT-COLOR
      \ S" %HINT_FONT_NAME%" HINT_FONT_SIZE HINT-FONT

      \ Stop PStart
      \ HINT: "Stopping USB drive manager..."
      BALLOON: "%CUR-TASK-NAME%" "Stopping USB drive manager..."
      KILL: "%sFlashInitProgram ONLYNAME%"
      \ HINT-OFF

      \ Flush buffers for D: drive
      S" Flush buffers StartIn: %sFlashUtilsPath%" CRON-LOG
      StartIn: "%sFlashUtilsPath%"
      S" sync.exe -r %sFlashFilesAtHDDPath%" CRON-LOG
      START-APPW: sync.exe -r %sFlashFilesAtHDDPath%

      \ UnMap Local folder from K:
      \ TODO
      \ HINT: "Unmapping HDD location..."
      BALLOON: "%CUR-TASK-NAME%" "Unmapping HDD location..."
      S" subst %sOriginalFlashDriveLetter% /D" CRON-LOG
      START-APPW: subst %sOriginalFlashDriveLetter% /D
      \ HINT-OFF

      \ Flush buffers for removable drive
      S" Flush buffers StartIn: %sFlashUtilsPath%" CRON-LOG
      StartIn: "%sFlashUtilsPath%"
      S" sync.exe -r %sTemporaryFlashDriveLetter%" CRON-LOG
      START-APPW: sync.exe -r %sTemporaryFlashDriveLetter%

      \ "Shift" real USB drive back to regular drive letter K:
      \ HINT: "Shifting USB drive back to it's regular drive letter..."
      BALLOON: "%CUR-TASK-NAME%" "Shifting USB drive back to it's regular drive letter..."
      StartIn: "%sFlashUtilsPath%"
      S" remount.exe %sTemporaryFlashDriveLetter% %sOriginalFlashDriveLetter%" CRON-LOG
      START-APPW: remount.exe %sTemporaryFlashDriveLetter% %sOriginalFlashDriveLetter%
      \ HINT-OFF

      \ Sync files from Local folder to Flash
      \ HINT: "Syncronizing from HDD to Flash..."
      BALLOON: "%CUR-TASK-NAME%" "Syncronizing from HDD to Flash..."
      StartIn: "%snnBackUpPath%"
      S" nnbackup.exe %snnBackUpParams% -i %sFlashFilesAtHDDPath% -o %sOriginalFlashDriveLetter%" CRON-LOG
      START-APPW: nnbackup.exe %snnBackUpParams% -i %sFlashFilesAtHDDPath% -o %sOriginalFlashDriveLetter%
      \ HINT-OFF

      \ Flush buffers for removable drive
      S" Flush buffers StartIn: %sFlashUtilsPath%" CRON-LOG
      StartIn: "%sFlashUtilsPath%"
      S" sync.exe -r %sFlashFilesAtHDDPath%" CRON-LOG
      START-APPW: sync.exe -r %sFlashFilesAtHDDPath%

      \ Perform unmount. (enh) 4
      \ TODO

      \ Cancel periodical backups. (enh) 6
      \ TODO

      \ Enable task that watches for changes of temporary locations. (bug) 2
      \ TODO
      \ USB_Flash_Handle_Requests_to_Flash_location_at_HDD @ CF-ACTIVE SET-CRON-FLAG
      \ USB_Flash_Handle_Requests_to_Flash_Temporary_Drive @ CF-ACTIVE SET-CRON-FLAG

      \ THINT: "Unmount task is complete" 10
      BALLOON: "%CUR-TASK-NAME%" "Unmount task is complete"

      SEM_MOUNT_UNMOUNT_IS_RUNNING RELEASE
)#

# Used after workstation restart if real USB Flash was not removed (still in U)
#( USB_Flash_Remount
   WatchHotKey: "^@R" \ alt + ctrl + "R"

   Rule: QUERY: "Remount?"

   SingleInstance

   Action:
      \ Configure hints
      \ HINT_BG_COLOR HINT_FG_COLOR HINT-COLOR
      \ S" %HINT_FONT_NAME%" HINT_FONT_SIZE HINT-FONT

      \ Map local folder to K:
      \ HINT: "Mapping HDD location to regular USB drive letter..."
      BALLOON: "%CUR-TASK-NAME%" "Mapping HDD location to regular USB drive letter..."
      S" subst %sOriginalFlashDriveLetter% %sFlashFilesAtHDDPath%" CRON-LOG
      START-APPW: subst %sOriginalFlashDriveLetter% %sFlashFilesAtHDDPath%
      \ HINT-OFF

      \ Run PStart from K:
      \ HINT: "Initializing USB drive manager..."
      BALLOON: "%CUR-TASK-NAME%" "Initializing USB drive manager..."
      S" PStart StartIn: %sFlashInitProgram ONLYDIR%" CRON-LOG
      StartIn: "%sFlashInitProgram ONLYDIR%"
      S" PStart exe: %sFlashInitProgram ONLYNAME%" CRON-LOG
      START-APP: "%sFlashInitProgram ONLYNAME%"
      \ HINT-OFF

      \ Establish periodical backups. (enh) 6
      \ TODO

      \ THINT: "Remount task is complete" 10
      BALLOON: "%CUR-TASK-NAME%" "Remount task is complete"
)#

# Used after workstation restart if USB Flash was not Unmounted before restart (still in U)
#( USB_Flash_WinLogon_Handler
   NoActive \ Bug 8
   WatchLogon: "*"

   \ Check if there is a need for remount (U: exists and K: does not exists) and ask user for working mode
   Rule: FILE-EXIST: "%sTemporaryFlashDriveLetter%" FILE-EXIST: "%sOriginalFlashDriveLetter%" NOT AND

   Action:
      USB_Flash_Remount LAUNCH
)#

# Propose user to Unmount prior to shut down/restart
#( USB_Flash_Handle_Workstation_Shutdown
   NoActive \ Bug 8
   WatchQueryShutdown

   Action:
      INTERCEPT-SHUTDOWN
      USB_Flash_Unmount RUN
      CONTINUE-SHUTDOWN
)#

# Notify user about modification that happened at "wrong" location (U: instead of K:)
#( USB_Flash_Handle_Requests_to_Flash_Temporary_Drive
   WatchDir: %sTemporaryFlashDriveLetter% WatchSubtree

   SingleInstance

   Action:
      2 BalloonIcon !
      BALLOON: "Warning from %CUR-TASK-NAME%" "%sTemporaryFlashDriveLetter% is being modified by some process. Pls, ensure it is not damaging synced folders."
      S" USB_Flash_Handle_Requests_to_Flash_Temporary_Drive empty statement" CRON-LOG
)#

# Notify user about modification that happened at "wrong" location (D:/K instead of K:)
#( USB_Flash_Handle_Requests_to_Flash_location_at_HDD
   WatchDir: %sFlashFilesAtHDDPath% WatchSubtree

   \ If flash was not remounted to U and smb tries to access Flash files at HDD
   Rule: FILE-EXIST: "%sTemporaryFlashDriveLetter%" NOT

   SingleInstance

   Action:
      2 BalloonIcon !
      BALLOON: "Warning from %CUR-TASK-NAME%" "%sFlashFilesAtHDDPath% is being modified by some process. Pls, ensure it is not damaging synced folders."
      S" USB_Flash_Handle_Requests_to_Flash_Temporary_Drive empty statement" CRON-LOG
)#

# TODO:
#      (bug, 2A) 8. FILE-EXIST: "K:" returns FALSE for mounted D:/K so it fails USB_Flash_WinLogon_Handler and may fail USB_Flash_Unmount
#      (bug, 2A) 1. "subst K: /d" is not working while Unmounting (sometimes works, sometimes not).
#      (bug, 2A) 2. enable/disable USB_Flash_Handle_Requests_to_Flash_location_at_HDD etc when Mounting/Unmounting.
#      (bug, 2A) 3. decide how to avoid "conflict" between USB_Flash_Handle_Requests_to_Flash_location_at_HDD etc and virus scanners.
#      (bug, 2B) 4. sometimes hints remain open despite of HINT-OFF; THINT doesn't help either.
#      (bug, 3) 5. disable Mount start as a result of Unmount task execution.
#      (bug, 3) 6. garbage hint files are left in the nnCron home dir after USB_Flash_Handle_Workstation_Shutdown with Unmount (looks like it is a result of # last bug id: bug 4).
#      (bug, 3) 7. configure nnBackup to ignore FireFox cache while syncing.
#      (enh, 2A) 1. add buffers "flush" before sync/mount/unmount operations.
#      (enh, 2A) 2. provide/log extended information about files modified at wrong location (USB_Flash_Handle_Requests_to_Flash_location_at_HDD, etc).
#      (enh, 2A) 3. review possible "exceptions" that can happen (unexpected reboots) and their handling.
#      (enh, 2A) 4. perform physical Flash unmount using RemoveDrive.
#      (enh, 2A) 5. review possible use cases.
#      (enh, 3) 6. consider if periodical backup is needed for different working modes.
#      (enh, 3) 7. implement different modes of everyday usage (FromFlash, noMount, FromHDD).
#      (impr, 2B) 1. debugging code.
# last bug id: 7
# last enh id: 7
# last impr id: 1
#
# Setup:
#      1. Backup your USB Flash :)
#      2. Ensure that you have all the necessary utils for running the script.
#      3. Configure variables (Variables_declaration) according to your environment.
#      4. Configure hotkeys.
#      5. Customize if you don't use PStart, etc.
#      6. Disable all the "dangerous" code like nnbackup.exe etc and check log output first.
#      7. Register in nnCron.
#      8. Enable "dangerous" code.
iScape
 
Posts: 3
Joined: Mon, 16 Apr 2007, 16:21

Return to nnCron forum (English)

Who is online

Users browsing this forum: No registered users and 2 guests

cron