Windows下自动云备份思源笔记到Gitee

前言

思源笔记是一款本地笔记为主的软件,其目前提供了148元/year的付费同步功能,但对于21世纪中国难民而言还是太贵啦。

条件允许的同学还是使用官方的同步,支持下作者。

Windows下自动云备份思源笔记到Gitee

所以,就在思考有没有白嫖的一种方法,能同步且不要付费呢。

网上有些使用云盘自动同步,但可能导致数据丢失。

这里提供方法:

笔记通过小软件的方式进行自动同步到git。

这个[siyuansyntogit]小软件基于python开发,我只在win10上进行过测试,下方有源码及exe提供,诸君自取。

https://gitee.com/kingstacker/siyuansyntogit

Windows下自动云备份思源笔记到Gitee

笔者对python语言并不熟悉,这是第一个demo,当前对于我已然够用,当然你可以自行进行更改。

软件及环境:

win10、思源笔记v2.4.7、siyuansyntogit、git、gitee网站

流程

(1)软件支持功能:

目录正确性判断

网络连接线判断,打开软件后,检测到电脑联网后会自动拉取远程文件同步

打开思源笔记软件同步后会自动最大化思源笔记窗口并自动重建索引(仅测试1080p,根据主题的不同,缩放的不同需要进行更改)

检测思源笔记开关状态

思源笔记软件关闭则自动提交git同步

小软件运行后默认最小化 窗口运行

Windows下自动云备份思源笔记到Gitee

(2)确保你使用过Git,Git使用不再此文说明。软件放置路径跟思源笔记的data路径在同一层级。git工程也在这一层级。

Windows下自动云备份思源笔记到Gitee

.gitignore文件内容参考:避免其他文件夹同步,这里只会同步data文件夹。思源笔记的笔记内容是存放在data文件夹中的。

conf/
history/
temp/
*exe

(3)确保已经进行了初次git提交,确保环境一切正常。enjoy it。

(4)你也可以把小软件开机自启:

参考:https://zhuanlan.zhihu.com/p/446167633

(4.1)创建小软件的快捷方式。

Windows下自动云备份思源笔记到Gitee
(4.2)快捷方式拖入:win+R打开打开对话框输入shell:startup,进入启动文件夹

Windows下自动云备份思源笔记到Gitee

(5)小软件运行界面图示:小软件打开默认最小化运行。

Windows下自动云备份思源笔记到Gitee
检测到思源笔记软件关闭,则自动提交同步。
Windows下自动云备份思源笔记到Gitee

这里使用gitee作为远程仓库,可以看到版本已经提交。

Windows下自动云备份思源笔记到Gitee

(7)源码参考:你可以进行任意更改,希望我的工作对你有所帮助。

import os
import sys
import ctypes
import time
import psutil
import pyautogui

from subprocess import call

home_dir = os.getcwd()               #获得当前路径

choice_list = ['上传','下载']

def git_update():
    os.chdir(home_dir)
    git_add_cmd    = "git add ."
    git_commit_cmd = "git commit -m {}".format(gitdate)
    git_push_cmd   = "git push origin master"
    call(
       git_add_cmd + "&&" +
       git_commit_cmd + "&&" +
       git_push_cmd,
       shell=True
    )

def git_get():
    os.chdir(home_dir)
    git_pull_cmd = "git pull origin master"
    call(
        git_pull_cmd,
        shell=True
    )

def is_process_running(process_name):
    pl = psutil.pids()
    for pid in pl:
        if psutil.Process(pid).name() == process_name:
            return True
    else:
        return False

if __name__ == "__main__":

    ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 6)
    print("---------------------------------------------------")
    print("欢迎使用 SiYuan Auto Backup,poweredBy kingstacker!")
    print("---------------------------------------------------")
    print("程序当前运行路径:")
    print(home_dir)
    time.sleep(0.5)
    print("----------------")
    print("检查程序执行路径是否正确中...")
    dir_status = int(os.path.exists(home_dir+'\data'))
    if dir_status == 1:
        print("程序执行路径正确.")
        pass
    else:
        print("未发现当前路径存在data文件夹,请确认!")
        os.system("pause")
        sys.exit()
    eth_exit_code = 1
    print("----------------")
    print('检查网络连接状态中,请等待...')
    status_befor = 0;
    auto_pull_status = 0;
    while True:
        if eth_exit_code == 1:
            # eth_exit_code = int(os.system('ping www.baidu.com > /dev/null'))
            eth_exit_code = int(os.system('ping www.baidu.com'))
        else:
            pass
        if eth_exit_code == 1:
            print('没联网,确认你的网络连接状态.')
        time.sleep(2)
        while True:
            gitdate = time.strftime("%Y-%m-%d/%H-%M-%S/%A", time.localtime())
            if eth_exit_code:
                break
            if auto_pull_status == 0:
                print("----------------")
                print("拉取远程文件中...")
                git_get()
                print("拉取远程文件完成!")
                print("----------------")
            time.sleep(2)

            try:
                siyuan_program_status = int(is_process_running("SiYuan.exe"))  # 查看思源软件是否打开
            except:
                pass
            else:
                pass
            if status_befor == 0 and siyuan_program_status == 1:
                print("----------------")
                print("发现思源软件已打开,等待软件关闭...")
                time.sleep(4)
                user32 = ctypes.WinDLL('user32')
                SW_MAXIMISE = 3
                hWnd = user32.GetForegroundWindow()
                user32.ShowWindow(hWnd, SW_MAXIMISE)

                time.sleep(1)
                print("自动重建索引中...")
                pyautogui.click(255, 43, button='left')
                time.sleep(0.5)
                pyautogui.click(309, 84, button='left')
                time.sleep(0.2)
                print("索引重建完成!")
            if status_befor == 1 and siyuan_program_status == 0:
                print("----------------")
                print("发现思源软件已关闭,备份笔记到云端中...")
                print("请等待...")
                print("拉取远程文件更新确认中...")
                git_get()
                print("正在提交笔记...")
                git_update()
                print("提交备份已完成!")
                print("当前备份时间点:",gitdate)
                print("----------------")
            status_befor = int(siyuan_program_status)
            auto_pull_status = 1;

以上。

Original: https://www.cnblogs.com/kingstacker/p/16842012.html
Author: 小翁同学
Title: Windows下自动云备份思源笔记到Gitee

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/799801/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球