Advice on best way to auto-duplicate a DT group into an external folder? ( ...cant have to many backups..;-) )

Hi all

In the spirit of “there cant be to many backups” TM :slight_smile: i want to automatically duplicate a group (full of Markdown files) within a Devonthink database into an external dropbox AND/OR iCloud (as i said cant have to many backups :laughing: )

Whats the way one should approach this? should i use a smart rule? Applescript, external automation (a-la Keyboard Maestro ?)?

i want to this to happen automatically after a few minutes when i add a new Markdown files into the Devonthink group.

PS i don’t want these files to be indexed in DT at all, just have them as a backup

thx in advance, any tips would be greatly appreciated!

Z

Whilst I’m not sure of the value of this backup (you are not insured against changes to files, only against file deletion; unless, that is, your backup destination has some kind of versioning), but I think I would go about it with a script run by a smart rule. I know of no trigger which would run a number of minutes after you add a file, other than one which simply runs every 5 minutes.

The requisite script would be something along the lines of getting the (POSIX) path to the record, telling Finder to duplicate that file to a specified location (either replacing or not replacing files with the same name in the destination) and repeating for each record. You could perform the duplicate action to a folder named (automatically) based on the date, or the day of the month, which would give you a degree of file history.

Thanks so much @Blanc, greatly appreciated!

yeah as i said i plan on using dropbox as the destination so it will have rudimentary versioning. i also will consider perhaps putting that MD folder in a git folder and manually push changes

I think I would go about it with a script run by a smart rule

yeah every X minutes will work great!

Problem is i don’t know how to code :laughing: … i can however try to follow Applescript/BASH examples (…sometimes :wink: … ) and slightly modify them to suit my needs. Do you know if there are script examples for such an export to a destination? either by the community or developers? i googles but couldn’t find something along theses lines

thx again!

Z

This should do what you are asking after:

property targetFolder : "/Users/yourUserName/Downloads/DTbackup" -- use a path such as "/Users/yourUserName/Documents/aFolder"; if you open a location in Finder, you can copy the path by right-clicking the destination in the path bar (the path bar can be toggled on/off in the View menu); paste it here, putting it in quotes
property replaceFiles : true -- use true or false to determine whether files with the same name in the destination should be overwritten (true) or not (false).

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			show progress indicator "Duplicating files to Backup" cancel button 1 steps count of theRecords
			do shell script "mkdir -p " & quoted form of targetFolder
			repeat with theRecord in theRecords
				if cancelled progress then error number -128
				step progress indicator (name of theRecord) as string
				set theFile to path of theRecord as text
				if replaceFiles is true then
					tell application "Finder" to duplicate (theFile as POSIX file) to folder (targetFolder as POSIX file) with replacing
				else
					tell application "Finder" to duplicate (theFile as POSIX file) to folder (targetFolder as POSIX file) without replacing
				end if
			end repeat
		on error error_message number error_number
			hide progress indicator
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		end try
		hide progress indicator
	end tell
end performSmartRule

Note that you need to specify the folder you want the files duplicated to in the first line; the comment explains how to do so. Also, you need to specify whether files should be overwritten or not. Note that the destination you specify will be created if it does not exist.

The script will not create any other form of structure, i.e. the records provided by your smart rule will be duplicated to the destination and no additional folder tree will be created. It would probably be possible to expand the script to replicate the structure in DT (i.e. Database/Group/Subgroup) if necessary.

As always: please try this on test files first and determine for yourself whether it does what you require it to do.

Wow just Wow @Blanc !!!

this is beyond perfect.

thank you so much :pray::pray::pray::pray::pray:

you just made my weekend, have a great Sunday

best regards

Z

1 Like

You’re most welcome.

This adapted version optionally saves the files to your target folder including a folder tree based on the name of the database and the location of the file within that database, so to yourdestination/database/group/subgroup/subgroup/file. Just in case that is helpful to you or others passing by here.

property targetFolder : "/Users/Michael/Downloads/DTdest" -- use a path such as "/Users/yourUserName/Documents/aFolder"; if you open a location in Finder, you can copy the path by right-clicking the destination in the path bar (the path bar can be toggled on/off in the View menu); paste it here, putting it in quotes
property replaceFiles : true -- use true or false to determine whether files with the same name in the destination should be overwritten (true) or not (false).
property includePath : true -- use true or false to determine whether the destination path includes the group structure (true) or not (false)

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			show progress indicator "Duplicating files to Backup" cancel button 1 steps count of theRecords
			repeat with theRecord in theRecords
				if cancelled progress then error number -128
				step progress indicator (name of theRecord) as string
				set theFile to path of theRecord as text
				if includePath is true then
					set destFolder to targetFolder & "/" & name of database of theRecord & location of theRecord
				else
					set destFolder to targetFolder
				end if
				do shell script "mkdir -p " & quoted form of destFolder
				if replaceFiles is true then
					tell application "Finder" to duplicate (theFile as POSIX file) to folder (destFolder as POSIX file) with replacing
				else
					tell application "Finder" to duplicate (theFile as POSIX file) to folder (destFolder as POSIX file) without replacing
				end if
			end repeat
		on error error_message number error_number
			hide progress indicator
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		end try
		hide progress indicator
	end tell
end performSmartRule

Edit: Oh, and DT has an AppleScript function called export which looks like it would do much the same thing - so it would be possible to avoid calling “Finder”, although it’s not obvious to me that we could then determine whether or not files should be overwritten. All roads lead to Rome.