qownnotes / scripts

QOwnNotes script repository
http://www.qownnotes.org
GNU General Public License v3.0
102 stars 78 forks source link

[SUPPORT] Script to create local backups for the notes #202

Closed ElfQrin closed 10 months ago

ElfQrin commented 11 months ago

I'm writing a qml script to create local backups every time a note is updated and written on disk.

I'm aware that this code, should it work, would overwrite a backup note with a same name, because there aren't subdirectories in the backup directory, but this is just experimental, for now.

However, I have two questions:

`import QtQml 2.0

/*

auto_bak_updated_notes.zip

pbek commented 11 months ago

Is there a way to detect the system (Linux or Windows) and set the command "cp" or "copy" accordingly?

https://www.qownnotes.org/scripting/methods-and-objects.html#check-whether-platform-is-linux-os-x-or-windows

script.startDetachedProcess(copyCmd , "\"" + fullNotePath + "\"" + " " + "\"" + bakNotesPath + note.fileName + ".bak"+ "\"");

You are not passing the parameters correctly, please see: https://www.qownnotes.org/scripting/methods-and-objects.html#starting-an-external-program-in-the-background

ElfQrin commented 11 months ago

I've read all the examples, and I can't make the copy command work with startDetachedProcess.

I've tried to pass parameters as an array of strings (comma separated): script.startDetachedProcess(copyCmd , ["\"" + fullNotePath + "\"" , "\"" + bakNotesPath + note.fileName + ".bak" + "\""] , "callback-cp" , 1);

and as a single string (that is, passing both parameters as a single parameter separated by a space character, as in the cp/copy command): script.startDetachedProcess(copyCmd , ["\"" + fullNotePath + "\"" + " " + "\"" + bakNotesPath + note.fileName + ".bak" + "\""] , "callback-cp" , 1);

I'm also using the callback to check if I can get anything wrong from there, but it looks fine.

Code: ` import QtQml 2.0

/*

QtObject { property string copyCmd; property string bakNotesPath; property string fullNotePath; property variant settingsVariables: [ { "identifier": "bakNotesPath", "name": "Directory path to notes backup", "description": "", "type": "directory", "default": "C:/D/bak/QOwnNotes/bak_notes", } ];

function onNoteStored(note) {
   if (script.platformIsWindows()) {
       copyCmd="copy";
   } else {
       copyCmd="cp";
   }

   bakNotesPath=script.toNativeDirSeparators(bakNotesPath + "/");
   fullNotePath=script.toNativeDirSeparators(note.fullNoteFilePath);
   // fullBakNotesPath = bakNotesPath + note.fileName + ".bak";

   script.log("Backing up " + "(" + copyCmd + ") " + "\"" + fullNotePath + "\"" + " to " + "\"" + bakNotesPath + note.fileName + ".bak" + "\"");
   script.startDetachedProcess(copyCmd , ["\"" + fullNotePath + "\"" , "\"" + bakNotesPath + note.fileName + ".bak" + "\""] , "callback-cp" , 1);
}

function onDetachedProcessCallback(callbackIdentifier, resultSet, cmd, thread) {
     if (callbackIdentifier == "callback-cp") {
         script.log(`${cmd[0]} ${cmd[1]}`);
     }
}

} `

auto_bak_updated_notes.zip

ElfQrin commented 11 months ago

By the way, is there any built in file copy funcion in qml? Like File.copy(source, dest)?

pbek commented 11 months ago

Best look how others have done it: https://github.com/search?q=repo%3Aqownnotes%2Fscripts%20script.startDetachedProcess&type=code

By the way, is there any built in file copy funcion in qml? Like File.copy(source, dest)?

No, there isn't. 😉

ElfQrin commented 11 months ago

Best look how others have done it: https://github.com/search?q=repo%3Aqownnotes%2Fscripts%20script.startDetachedProcess&type=code

I already looked, but to no avail because the format of the command I used was correct.

I found more useful to write a script that would write to a file the arguments it had received, so that I realized that qml automatically adds quotes to an argument if it contains spaces in it.

Another problem is that the "copy" command on Windows (I haven't tried "cp" on Linux yet) does not seem to accept the arguments passed by qml. Instead if I call a batch file that then passes the same arguments to copy, everything works as expected.

cp.bat @ echo off copy %1 %2 %3 auto_bak_updated_notes.zip cp.zip

pbek commented 11 months ago

Yes, you might need a shell to copy files. copy is no application to launch. You may need to run your command with e.g. cmd.exe, see https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmd

github-actions[bot] commented 10 months ago

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] commented 10 months ago

This issue was closed because it has been inactive for 14 days since being marked as stale.