What to do to mount/unmount drive before and after backup?

I backup data to a local drive in Windows. However, instead of keeping the drive always online, I’d like to take it offline outside of scheduled backup times, and bring it back online before backup happens. Windows has API to mark a disk online/offline (similar to mount and umount in Linux).

Unfortunately, it seems the before and after snapshot root actions happen only when actual upload or restore are performed for a snapshot entry. If the drive is offline when I run snapshot create, it just errs with “cannot access storage path”. Ideally, I want to run an action before any attempt to access the storage, and an action after the whole snapshot operation is done (like “setup” and “teardown” scripts). Is there such option in the current Kopia?

kopia supports such concept as actions. Here is documentation regarding this: Actions | Kopia

The only things I would add, is that there no any benefit to put disk offline. Either physically disconnect it or simply let it idle.

kopia supports such concept as actions.

I understand that. I think there is no such action to do the setup and teardown sort of task, based on my understanding of the source code.

there no any benefit to put disk offline

I have my reason to do it.

I think there is no such action to do the setup and teardown sort of task

How putting a drive to offline mode is related to kopia ?
It is a job of underlying operation system. The path, - the only argument that need to make a backup, kopia isn’t a program to manage operation system and its components.

I have my reason to do it.

Then do it on the level of operation system, create a script that first set backup disk to online mode:

@echo off
echo select disk X > diskmode.txt
echo online disk >> diskmode.txt
type diskmode.txt | diskpart

then run kopia to create snapshot

and run another script to put disk in offline mode

@echo off
echo select disk X > diskmode.txt
echo offline disk >> diskmode.txt
type diskmode.txt | diskpart

or combine all above in a single script and run it from scheduler.
Just make sure to use correct disk identifier in place of X :wink:

I’m not asking kopia to directly support bringing disk online or offline. I’m asking if kopia has action before any operation happens and after all operations done. And if such actions are not available, maybe we could discuss the possibility of introducing such feature. Online/offline disk is just my use case to help understand the incentive. There could be many other uses for setup/teardown.

Either I don’t understand what you mean under term “action before any operation happens and after all operations done” or you didn’t read the link to documentation that I provided for you in my first post here. The feature you asking is already implemented.

$ kopia policy set /some/dir --before-folder-action /path/to/command
$ kopia policy set /some/dir --after-folder-action /path/to/command
$ kopia policy set /some/dir --before-snapshot-root-action /path/to/command
$ kopia policy set /some/dir --after-snapshot-root-action /path/to/command

If it isn’t what you looking for, then give more details, what exactly means: “setup and teardown” that can’t be executed by existing hooks

Yes, but folder actions either before or after only apply to the snapshot, they’re bound to. Kopia has no notion of multiple snapshots per “job”. So if your different snapshots target the same drive, but different repos on it, or even the same, you’d never know, when your “round” of snapshots has finished and thus where to hook your action to.

If you want a more sophisticated job handling, then you’ll have to write some scripts which will handle that for you and call kopia from the scripts directly. This is currently the only way of handling this. In these scriptrs you can check the availability of the drive and choose not to call kopia snapshot…

Hello,

I wrote this very, very, very simple cmd script to do some Kopia jobs, both local and offline. This is very simple example, how one can mount/unmount usb drivers for Kopia backup purposes.

echo off
cls

:main
echo --------------------
Echo Kopia Backup
echo --------------------               

set /p answer=1-Local Backup 1,  2-Local Backup 2, 3-USB Backup 1, 4-USB Backup 2,  X-Exit - 1/2/3/4 or X?
   
   if /i "%answer:~,1%" EQU "1" goto hdc
   if /i "%answer:~,1%" EQU "2" goto hdd
   if /i "%answer:~,1%" EQU "3" goto usb1
   if /i "%answer:~,1%" EQU "4" goto usb2   
   if /i "%answer:~,1%" EQU "X" goto exits
   
   echo Please type: 1/2/3/4 or  X -?
   
   goto main

:exits

exit /b


:hdc

rem - path to backup config

SET dest=repository-xxxxxx.config
SET name=Local Backup 1

rem also, passwords can be assigned to argument:
rem SET password=xxxxx

goto mainhd

:hdd

SET dest=repository-xxxxx.config
SET name=Local Backup 2

goto mainhd


:usb1

rem Path to usb backup.
rem Warring! As we connect it, Kopia always assign new default "respiratory.config" file, overwriting existing one. So, do not use default "respiratory.config" for local backups.

SET dest=g:\kopia\usb1
SET name=USB-1

goto mainusb

:usb2

SET dest= h:\kopia\usb2
SET name=USB-2

goto mainusb

:mainhd

echo ----------------
Echo Kopia - %name%
echo ----------------               

set /p answer=C-Backup. M-Mount. V-Verify. E-Exit - C/M/V or E?
   if /i "%answer:~,1%" EQU "C" goto backuphd
   if /i "%answer:~,1%" EQU "V" goto verifyhd   
   if /i "%answer:~,1%" EQU "M" goto mounthd
   if /i "%answer:~,1%" EQU "E" goto exitshd
   
   echo Please type C,M,V or E?
   
   goto mainhd

:mounthd

Echo "Mount..."

kopia --config-file=%dest% mount 

goto mainhd

:exitshd

goto main

:backuphd

Echo "Snapshotting..."

kopia --config-file=repository-%dest%  snapshot create --all  --password=xxxx

goto mainhd

:verifyhd

Echo "Verifying..."

kopia --config-file=%dest% snapshot verify --verify-files-percent=3
kopia --config-file=%dest% cache clear

goto mainhd

:mainusb

if exist %dest% ( 
echo "Connecting..."

kopia repository connect filesystem --path=%dest% --password=xxxxx 

echo "Connected!"

goto againusb

) else (

echo Please connect %name%-Drive!
Pause
goto main

)

:againusb

echo ----------------
Echo Kopia - %name%
echo ----------------        


set /p answer=C-Backup. M-Mount. V-Verify. E-Disconnect - C/M/V or E?
   if /i "%answer:~,1%" EQU "C" goto backupusb
   if /i "%answer:~,1%" EQU "V" goto verifyusb   
   if /i "%answer:~,1%" EQU "M" goto mountusb
   if /i "%answer:~,1%" EQU "E" goto exitsusb
   
   echo Please type C,M,V or E?
  
   goto againusb

:mountusb

Echo "Mount..."

kopia mount 

goto againusb

:exitsusb

Echo "Disconnecting..."

kopia repository disconnect

goto main

:backupusb

Echo "Snapshotting..."

kopia snapshot create --all 

goto againusb

:verifyusb

Echo "Verifying..."

kopia snapshot verify --verify-files-percent=0.1 

goto againusb