MyTree AutoBackUp script

Hello everybody,

I just want share my userscript here - it’s too litttle thing for I open a github repo.
Any comments are welcome. :blush:

Features:

  • use a fix BACKUP folder for everything (location is changeable)
  • the BACKUP folder is local folder or a mapped virtual folder ( eg. Google Drive with subst or SUBSTittue or whatever)
  • works everything: local or remote files or KOMOD files eg. macros
  • the deafult trigger is a presave (changeable)
  • use a structured folder tree, based by file url eg. you edit C:\Myfolder\readme.txt, the backup will be:
- BACKUP
  - file
    - C 
      - Myfolder
        - readme.txt.1601215837161 (where is the last number is a timestamp)
  • If you edit a remote file via scp /var/www/html/index.php the tree will be:
- BACKUP
  - scp
    - servername (based by connection panel) 
     - var
      - www
       - html
         - index.php.1601215887161

Here is the script: - (save as TreeAutoBackUp.ktf )

/*
komodo tool: TreeAutoBackUp
===========================
async: 1
icon: chrome://fugue/skin/icons/lifebuoy.png
is_clean: true
language: JavaScript
rank: 100
trigger: trigger_postsave
trigger_enabled: 1
type: macro
version: 1.1.6
===========================*/
/* 
 * TREE AUTOBACKUP for Komodo IDE 
 * batlabor
*/
var ko_TreeAutoBackUp = function(){

	var win,I,dir,utime,view,fileName,newDirs,pattern,newFile,content;
	// CHECK CURRENT OS
	win = Services.koInfo.isWindows;
	
	// set directory separators
	if (win){
		I = "\\";
	}else{
		I= "/";
	}
	//////////////////////////////////////////////////////
	// BACKUP DIRECTORY
	/////////////////////////////////////////////////////
	// definied backup directory
	// you can use KOMODO dirs in path - > console.log(Services.koDirs);
	// note: backup directory will be a local directory
	// (or locally mapped virtual directory via subst, SUBSTitue... whatever)
	// and always use trailing slahes!
	// on windows escape drive backslahes!
	// eg. 'H:\\KOMODO' + I + BACKUPS
	
	dir		= Services.koDirs.installDir + I + "BACKUPS" + I;
	
	// timestamp
	utime				= +new Date() + "";
	// current file
	view			= require("ko/views").current();
	content			= view.koDoc.encodedText;
	fileName		= view.basename;

	// if file not saved - aka new file - the basename is false
	
	if (fileName){
	
		// we create directory structure by url
		pattern = new RegExp("/" + fileName  + '$' + '|:', 'mg');
		newDirs = view.url.replace(pattern,"");
		newDirs = dir + newDirs.replace(/\/\/\/|\/\/|\/|\/$/mg, I);

		require("ko/file").mkpath(newDirs); 
	
		//create new file
		newFile = newDirs + I + fileName + '.' + utime;
		require("ko/file").create(newFile);
		backupFile = require("ko/file").open(newFile, "wb");
		if (!backupFile.closed) {
			backupFile.write( content );
			backupFile.close();
		}

	}else{
		return;
	}

};
ko_TreeAutoBackUp();

1 Like