一種文檔同步的方案

 

一開始將不少筆記放在雲筆記本上面,後來發現筆記太零碎不少沒有去整理,並且筆記本上面雖然能夠寫文檔,可是沒有word上面的直觀正式,決定整理一些筆記歸檔到word上面,這樣容易對整個知識體系有所整理,打通一些靜脈。想了一些思路,最終使用雲網盤的方法。固然了雲網盤有一些隱私泄露的風險,要自行斟酌。app

 

使用word記錄,能夠在左邊點擊想要跳轉的章節,而且導出pdf也有目錄能夠看,比較清晰。oop

 

這裏選擇了不少人推崇雲同步軟件(這裏不提到)。它能夠在多個電腦之間同步文件,本着不相信雲的觀念,怕他一不當心將個人改動弄沒了,也就是我添加了A,而後又添加了B,最後雲出了bug,把個人文檔同步到A的狀態,這樣我就得從新寫了,以前出現過這種狀況無論是本身的緣由仍是雲的緣由,因此額外的爲它加了一個保險,本身寫了一個本地備份的程序。spa

 

 

import datetime
import os
import os.path
import shutil
import filecmp
import time
import wx
import thread
from datetime import datetime 

TRAY_TOOLTIP = 'Backup Main'
TRAY_ICON = 'icon.jpg'

NUT_DRIVE_DIRECTORY = 'C:\\Users\\tanhangbo\\Desktop\\Share\\DOC'
BACKUP_DIRECTORY = 'D:\\Sync.bak'


def write_log(log_str):
    f = open(os.path.join(BACKUP_DIRECTORY, 'log.txt'), 'a');
    f.write("\n\n[---------->New Event]");
    f.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    f.write(log_str);
    f.close;



##################################


def create_menu_item(menu, label, func):
    item = wx.MenuItem(menu, -1, label)
    menu.Bind(wx.EVT_MENU, func, id=item.GetId())
    menu.AppendItem(item)
    return item


class TaskBarIcon(wx.TaskBarIcon):
    def __init__(self):
        super(TaskBarIcon, self).__init__()
        self.set_icon(TRAY_ICON)
        self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)


    def CreatePopupMenu(self):
        menu = wx.Menu()
        create_menu_item(menu, 'Say Hello', self.on_print_backup)
        menu.AppendSeparator()
        create_menu_item(menu, 'Exit', self.on_exit)
        return menu

    def set_icon(self, path):
        icon = wx.IconFromBitmap(wx.Bitmap(path))
        self.SetIcon(icon, TRAY_TOOLTIP)

    def on_left_down(self, event):
        print 'Tray icon was left-clicked.'

    def on_print_backup(self, event):
        print BACKUP_DIRECTORY

    def on_exit(self, event):
        wx.CallAfter(self.Destroy)


def icon_main():
    app = wx.PySimpleApp()
    TaskBarIcon()
    app.MainLoop()




#################################

def copy_file(sourceDir,targetDir):
    shutil.copy2(sourceDir,targetDir) 


def move_and_add_time(file_path):
    cur_date = datetime.now().strftime('%Y%m%d%H%M%S');
    new_file_path = file_path + "." + cur_date;
    shutil.move(file_path, new_file_path) 


def judge_size_and_backup(org_file_path, new_file_path):
    if not os.path.exists(new_file_path):
        write_log("\nnew file found " + org_file_path + "\nneed to copy to " + new_file_path)
        copy_file(org_file_path, new_file_path);
    else:

        backed_size = os.path.getsize(new_file_path);
        org_size = os.path.getsize(org_file_path);
        #if file changed, (backup) and (backup the backup)
        
        if org_size > backed_size:
            write_log("\nfile get larger " + org_file_path)
            move_and_add_time(new_file_path);
            copy_file(org_file_path, new_file_path);
        elif org_size< backed_size:
            write_log("\nfile get smaller " + org_file_path)
            move_and_add_time(new_file_path);
            copy_file(org_file_path, new_file_path);



def do_backup(base_dir, backup_dir):
    for root, dirs, files in os.walk(base_dir):
        for name in files:
            org_file_path = os.path.join(root, name);
            new_file_path = os.path.join(backup_dir, name);
            judge_size_and_backup(org_file_path, new_file_path);


def get_backup_directory(base_dir):
    backup_dir = base_dir + '.bak'
    if not os.path.exists(backup_dir):
        write_log("create new dir " + backup_dir);
        os.makedirs(backup_dir);
    return backup_dir

def perform_backup(base_dir):
    backup_dir = BACKUP_DIRECTORY;
    do_backup(base_dir, backup_dir)

def backup_main(threadName):
    while True:
        perform_backup(NUT_DRIVE_DIRECTORY)
        time.sleep(60)

#################################
        
def main():
    thread.start_new_thread( backup_main, ("Thread-1", ) )
    icon_main();



if __name__ == '__main__':
    main()

 

另外要準備一個icon文件(icon.jpg),放在同一個目錄下,它跑起來的時候會在任務欄顯示。3d

若是文件有改動,他會自行拷貝到備份文件夾。將他重命名爲.pyw文件,能夠後臺運行。code

 

 

這是實際的備份效果:orm

若是文件被同步壞了,能夠從裏面找,總能找獲得的。blog

相關文章
相關標籤/搜索