一种文档同步的方案
一开始将很多笔记放在云笔记本上面,后来发现笔记太零碎很多没有去整理,而且笔记本上面虽然可以写文档,但是没有word上面的直观正式,决定整理一些笔记归档到word上面,这样容易对整个知识体系有所整理,打通一些静脉。想了一些思路,最终使用云网盘的方法。当然了云网盘有一些隐私泄露的风险,要自行斟酌。
使用word记录,可以在左边点击想要跳转的章节,并且导出pdf也有目录可以看,比较清晰。
这里选择了很多人推崇云同步软件(这里不提到)。它可以在多个电脑之间同步文件,本着不相信云的观念,怕他一不小心将我的改动弄没了,也就是我添加了A,然后又添加了B,最后云出了bug,把我的文档同步到A的状态,这样我就得重新写了,之前出现过这种情况不管是自己的原因还是云的原因,所以额外的为它加了一个保险,自己写了一个本地备份的程序。
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),放在同一个目录下,它跑起来的时候会在任务栏显示。
如果文件有改动,他会自行拷贝到备份文件夹。将他重命名为.pyw文件,可以后台运行。
这是实际的备份效果:
如果文件被同步坏了,可以从里面找,总能找得到的。