Hierarchical backup wanted.



  • Ok, NPP has great backup feature that saves all your edits to a special dir. But it would be a bit difficult to browse there though same-named copies originated from various sources (many days later). For example I could edit different websites and style.css files on each of them.

    It would be extremely nice to have hierarchical backup system, when if I edit (it’s WinScp) C:\Users\User\AppData\Local\Temp\scp18702\var\www\lala\data\www\example.com\actions\channel.php
    and backup copy to be saved to
    %Backup-dir%/Drive-c/Users\User\AppData\Local\Temp\scp18702\var\www\lala\data\www\example.com\actions\channel.php

    Seems there’s no such a plugin?



  • @teleslon2

    To my knowledge, there’s no plugin to do such a thing.

    But, with the PythonScript plugin (or another scripting plugin) you can do pretty much anything you want. You would set up a “notification” on the save event and then fire off some script code to make a copy of your file.

    If you’d be willing to use the PythonScript plugin, I’ll volunteer to put together a demo for you (it’s really not difficult).



  • So it could be as simple as a script like what follows.

    Note that this script only considers files that are mapped to a local C: through Z: drive.
    Other (network) paths are possible, but for the point of illustration, that’s just an unnecessary complication.

    Also note that much more error checking and results reporting (presumably upon failure) could be done.

    Typically the lines of the script would be added to the user’s startup.py file, so that it is transparently activated and sitting there waiting for user saves to happen, after Notepad++ initializes.

    Anyway, the script:

    # -*- coding: utf-8 -*-
    
    from Npp import notepad, NOTIFICATION
    import os
    import shutil
    import re
    
    def callback_notepad_FILESAVED(args):
        curr_buffer_id = notepad.getCurrentBufferID()
        notepad.activateBufferID(args['bufferID'])
        p = notepad.getCurrentFilename()
        m = re.match('([C-Z]):', p, re.I)
        if m:
            top_level_backup_dir = r'c:\temp\mybackups'
            backup_path = top_level_backup_dir + os.sep + 'Drive-{}'.format(m.group(1)) + p[2:]
            backup_dir = backup_path.rsplit(os.sep, 1)[0]
            if not os.path.isdir(backup_dir): os.makedirs(backup_dir)
            if os.path.isdir(backup_dir): shutil.copy2(p, backup_path)
    
    notepad.callback(callback_notepad_FILESAVED, [NOTIFICATION.FILESAVED])
    


  • Sadly, it doesn’t work for me. Folder c:\temp\mybackups exists. I’ve tried files on Z (mapped c:\xampp…), on С - nothing appeared in mybackups folder.
    NPP 7.8 Windows 8.1
    Is it possible to print debug info there? My primary language is PHP, so I’m not so smart here…



  • @teleslon2

    Did you restart npp after creating your user startup.py?
    A print(m) before the if m will print to the python script console.
    Can be opened via the menu.
    Just make sure you use the same indentation (tabs or spaces but not mixed) as the lines before and after.



  • @teleslon2

    Did you notice this line in the demo script?:

    top_level_backup_dir = r'c:\temp\mybackups'

    Obviously you’d want to replace the path I used with the path you desire.

    Otherwise I suggest the “print” route that @Ekopalypse recommended.

    Saying “it doesn’t work” is fine but it doesn’t leave much room for help from someone not sitting right at your computer.



  • @teleslon2 said in Hierarchical backup wanted.:

    Folder c:\temp\mybackups exists.

    Oh, you did say this, so my comment about that doesn’t apply. Sorry.



  • That’s strange. I surely restarted it and not once. Ok, now it’s working (after inserting “print”, hmm…).
    Next, I’d like to shorten some paths but my regex didn’t match, could you please help?

    print (backup_path)
    backup_path = re.sub(r"<c\:\\temp\\mybackups\\Drive\-C\\Users\\User\\AppData\\Local\\Temp\\scp\d+>", 'WinScp', backup_path)
    

    Prints
    c:\temp\mybackups\Drive-C\Users\User\AppData\Local\Temp\scp26772\home\admin\web
    before replacing.



  • Opps, there should by “c:\temp\mybackups\WinScp” as replacement string. But anyway, it prints the same after replacement.



  • Thanks, Alan!
    And a guy helped me out there, so below is my final variant. I add time to file names and shorten paths derived from WinScp editing of remote files.

    from Npp import notepad, NOTIFICATION
    import os
    import shutil
    import re
    from datetime import datetime
    
    def callback_notepad_FILESAVED(args):
        curr_buffer_id = notepad.getCurrentBufferID()
        notepad.activateBufferID(args['bufferID'])
        p = notepad.getCurrentFilename()
        m = re.match('([C-Z]):', p, re.I)
        time_now = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        if m:
            top_level_backup_dir = r'c:\temp\mybackups'
            backup_path = top_level_backup_dir + os.sep + 'Drive-{}'.format(m.group(1)) + p[2:] + '.' + time_now
            backup_path = re.sub(r"c\:\\temp\\mybackups\\Drive\-C\\Users\\User\\AppData\\Local\\Temp\\scp\d+", r'c:\\temp\\mybackups\\WinScp', backup_path)
            backup_dir = backup_path.rsplit(os.sep, 1)[0]
            if not os.path.isdir(backup_dir): os.makedirs(backup_dir)
            if os.path.isdir(backup_dir): shutil.copy2(p, backup_path)
    
    notepad.callback(callback_notepad_FILESAVED, [NOTIFICATION.FILESAVED])
    

Log in to reply