
隨著科技的發展我們日常中拍攝的圖片和影片清晰度不斷提升,但這也有一個較大的缺點那就是他們的體積也越來越大。還記得以前剛開始使用智慧型手機的時候那會一張照片只不過才2-5MB ,而現在一張照片已經達到了15-20MB ,甚至更大。

而我們手機上的儲存空間是有限的,我們怎麼把這些照片和影片備份起來,好讓手機騰出空間呢?
於是,在剛開始我是將這些資料都存放在了某相簿雲端上,雖然解決了存放這些資料的問題,但是也冒出了新的問題,例如上傳大小約束、需要一直佔後台導致耗電增加、廣告。
後面我乾脆不使用了,我自己擼了一個腳本用於備份這些數據,於是就有了這篇文章。
我使用了Node.js和adb製作了這個腳本,並命名為MIB
這個小工具是利用手機上的adb調試,透過shell命令讀取手機中的檔案資訊和複製,移動手機中的檔案實現的。
我畫了一個簡易流程圖, MIB首先會從讀取設定檔(沒有則建立配檔),根據設定檔讀取需要備份的節點路徑並進行檔案備份作業。直到節點結束。

安裝所需環境
下載adb包,用於執行各種設備操作
下載Node.js ,這個我相信兄弟們的電腦上都已經有了
安裝依賴庫
fs-extra :基於fs模組二次封裝的Node庫prompts :命令列上互動的Node函式庫winston :用來記錄腳本日誌的Node函式庫由於專案原始碼有點過多,我這裡只放主要的程式碼部分
有興趣的小夥伴可以去
github上看專案原始碼github.com/ QC2168/mib
讀取設定檔
export const getConfig = (): ConfigType => {
if (existConf()) {
return readJsonSync(CONFIG_PATH);
}
// 找不到設定檔return createDefaultConfig();
};執行腳本時,選擇需要備份的設備ID 。並指定執行adb指令時的設備
(async () => {
const device: string | boolean = await selectDevice();
if (device) MIB();
})();
export const selectDevice = async ():Promise<string|false> => {
// 取得裝置const list: devicesType[] = devices();
if (list.length === 0) {
log("目前無設備連接,請連接後執行該工具", "warn");
return false;
}
const result = list.map((i) => ({ title: i.name, value: i.name }));
const { value } = await prompts({
type: "select",
name: "value",
message: "please select your device",
choices: result,
});
currentDeviceName = value;
return currentDeviceName;
};遍歷備份節點
選擇設備之後,進入遍歷節點訊息,並執行拷貝檔案到指定路徑(設定檔中的output屬性)
const MIB = () => {
// 取得設定檔const { backups, output } = getConfig();
// 判斷備份節點是否為空if (backups.length === 0) {
log("目前備援節點為空", "warn");
log("請在設定檔中新增備份節點", "warn");
}
if (backups.length > 0) {
isPath(output);
// 解析備份路徑最後一個資料夾backups.forEach((item: SaveItemType) => {
log(`目前執行備份任務:${item.comment}`);
const arr = item.path.split("/").filter((i: string) => i !== "");
const folderName = arr.at(-1);
const backupDir = pathRepair(item.path);
// 備份目錄// 判斷節點內是否有備份目錄// 拼接匯出路徑const rootPath = pathRepair(pathRepair(output) + folderName);
const outputDir = item.output
? item.output && pathRepair(item.output)
: rootPath;
//判斷備份路徑是否存在if (!isPathAdb(backupDir)) {
log(`備份路徑:${backupDir} 不存在已跳過`, "error");
} else {
// 判斷匯出路徑isPath(outputDir);
backup(backupDir, outputDir, item.full);
}
});
}
log("程式結束");
};
// 精進需要備份的文件,進入備份佇列中const backup = (target: string, output: string, full: boolean = false) => {
if (!full) {
// 備份非備份的檔案資料// 取得手機中的檔案資訊,比較本地const { backupQueue } = initData(target, output);
// 計算體積與數量computeBackupSize(backupQueue);
// 執行備份程式move(backupQueue, output);
} else {
// 不檔案對比,直接備份moveFolder(target, output);
}
};
// 移動待備份檔案佇列中的檔案 const move = (backupQueue: FileNodeType[], outputDir: string): void => {
if (backupQueue.length === 0) {
log("無需備份");
return;
}
for (const fileN of backupQueue) {
log(`正在備份${fileN.fileName}`);
try {
const out: string = execAdb(
`pull "${fileN.filePath}" "${outputDir + fileN.fileName}"`,
);
const speed: string | null = out.match(speedReg) !== null ? out.match(speedReg)![0] : "讀取速度失敗";
log(`平均傳輸速度${speed}`);
} catch (e: any) {
log(`備份${fileN.fileName}失敗error:${e.message}`, "error");
}
}
};USB連接備份數據在終端機中輸入以下命令進行全域安裝mib 。
npm i @qc2168/mib -g
設定腳本文件
首次使用需要在使用者目錄下新建.mibrc文件,並設定對應的參數內容。
{
"backups": [
{
"path": "/sdcard/MIUI/sound_recorder/call_rec",
"comment": "通話錄音"
},
{
"path": "/sdcard/DCIM/Camera",
"comment": "本地相簿"
},
{
"path": "/sdcard/DCIM/Creative",
"comment": "我的創作"
},
{
"path": "/sdcard/Pictures/weixin",
"comment": "微信相簿"
},
{
"path": "/sdcard/tencent/qq_images",
"comment": "QQ相簿"
},
{
"path": "/sdcard/Pictures/知乎",
"comment": "知乎"
},
{
"path": "/sdcard/tieba",
"comment": "貼吧"
},
{
"path": "/sdcard/DCIM/Screenshots",
"comment": "螢幕截圖"
},
{
"path": "/sdcard/DCIM/screenrecorder",
"comment": "螢幕錄製"
},
{
"path": "/sdcard/MIUI/sound_recorder",
"comment": "錄音"
},
{
"path": "/sdcard/MIUI/sound_recorder/app_rec",
"comment": "應用錄音"
}
],
"output": "E:/backups/MI10PRO"
}執行備份
在控制台中,直接輸入mib即可觸發腳本,無需其他參數。
mib
控制台會根據設定檔並輸出對應的資訊。
2022-04-09 20:58:11 info 目前執行備份任務:螢幕錄製2022-04-09 20:58:11 info 備份數量1 2022-04-09 20:58:11 info 已取得資料24Mb 2022-04-09 20:58:11 info 備份體積24Mb 2022-04-09 20:58:11 info 正在備份Screenrecorder-2022-04-08-19-45-51-836.mp4 2022-04-09 20:58:12 info 平均傳輸速度27.7 MB/s 2022-04-09 20:58:12 info 目前執行備份任務:錄音2022-04-09 20:58:12 info 備份數量0 2022-04-09 20:58:12 info 備份體積0Mb 2022-04-09 20:58:12 info 無備份2022-04-09 20:58:13 info 程式結束
原文網址:https://juejin.cn/post/7084889987631710221
作者:_island