With the development of technology, the clarity of the pictures and videos we take in our daily life continues to improve, but this also has a major disadvantage, that is, their size is also getting larger and larger. I still remember when I first started using smartphones, a photo was only 2-5MB
, but now a photo has reached 15-20MB
, or even larger.
The storage space on our mobile phones is limited. How do we back up these photos and videos to free up space on our mobile phones?
So, at the beginning, I stored all these data in a photo album cloud. Although the problem of storing this data was solved, new problems also emerged, such as upload size constraints and the need to occupy the background all the time, which led to increased power consumption. ,advertise.
Later, I simply stopped using it and wrote a script to back up the data, so this article came out.
I used Node.js
and adb
to make this script, and named it MIB
This small tool is implemented by using adb
debugging on the mobile phone, reading file information and copying, and moving files on the mobile phone through shell
commands. .
I drew a simple flow MIB
until the end of the node.
install the required environment
and download adb
package, which is used to perform various device operations.
Download Node.js
I believe that all brothers already have the installation dependency library fs-extra on their computers
:
fs-extra
Node
library based on the secondary encapsulation of the fs
module.prompts
: Node
library for interaction on the command linewinston
: Node
library for recording script logs.Since the project source code is a bit too much, I only put the main code part here.
Interested friends can go to
github
to see the project source code github.com/ QC2168/mib
reads the configuration file
export const getConfig = (): ConfigType => { if (existConf()) { return readJsonSync(CONFIG_PATH); } // The configuration file cannot be found return createDefaultConfig(); };
When executing the script, select the device ID
that needs to be backed up. And specify the device when executing adb
command
(async () => { const device: string | boolean = await selectDevice(); if (device) MIB(); })(); export const selectDevice = async ():Promise<string|false> => { // Get devices const list: devicesType[] = devices(); if (list.length === 0) { log("No device is currently connected, please connect before executing the tool", "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; };After
traversing the backup node
and selecting the device, enter the traversal node information and execute copy file to the specified path ( output
attribute in the configuration file)
const MIB = () => { // Get the configuration file const { backups, output } = getConfig(); // Determine whether the backup node is empty if (backups.length === 0) { log("The current backup node is empty", "warn"); log("Please add backup nodes in the configuration file", "warn"); } if (backups. length > 0) { isPath(output); // Parse the last folder of the backup path backups.forEach((item: SaveItemType) => { log(`Currently executing backup task:${item.comment}`); const arr = item.path.split("/").filter((i: string) => i !== ""); const folderName = arr.at(-1); const backupDir = pathRepair(item.path); // Backup directory // Determine whether there is a backup directory in the node // Splice the export path const rootPath = pathRepair(pathRepair(output) + folderName); const outputDir = item.output ? item.output && pathRepair(item.output) : rootPath; // Determine whether the backup path exists if (!isPathAdb(backupDir)) { log(`Backup path:${backupDir} does not exist and has been skipped`, "error"); } else { // Determine the export path isPath(outputDir); backup(backupDir, outputDir, item.full); } }); } log("Program ends"); }; // Refine the files that need to be backed up and enter them in the backup queue const backup = (target: string, output: string, full: boolean = false) => { if (!full) { // Back up non-backup file data // Get the file information in the mobile phone and compare it with the local const { backupQueue } = initData(target, output); // Calculate the volume and quantity computeBackupSize(backupQueue); //Execute the backup program move(backupQueue, output); } else { // No file comparison, direct backup moveFolder(target, output); } }; //Move files in the file queue to be backed up const move = (backupQueue: FileNodeType[], outputDir: string): void => { if (backupQueue.length === 0) { log("No backup required"); return; } for (const fileN of backupQueue) { log(`Backing up ${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] : "Failed to read speed"; log(`Average transmission speed${speed}`); } catch (e: any) { log(`Backup ${fileN.fileName} failed error:${e.message}`, "error"); } } };
USB
connection to backup dataenter the following command in the terminal to install mib
globally.
the npm i @qc2168/mib -g
configuration script file
for the first time, you need to create a new .mibrc
file in the user directory and set the corresponding parameter content.
{ "backups": [ { "path": "/sdcard/MIUI/sound_recorder/call_rec", "comment": "Call recording" }, { "path": "/sdcard/DCIM/Camera", "comment": "Local photo album" }, { "path": "/sdcard/DCIM/Creative", "comment": "My creation" }, { "path": "/sdcard/Pictures/weixin", "comment": "WeChat photo album" }, { "path": "/sdcard/tencent/qq_images", "comment": "QQ photo album" }, { "path": "/sdcard/Pictures/zhihu", "comment": "Zhihu" }, { "path": "/sdcard/tieba", "comment": "Tieba" }, { "path": "/sdcard/DCIM/Screenshots", "comment": "Screenshot" }, { "path": "/sdcard/DCIM/screenrecorder", "comment": "Screen recording" }, { "path": "/sdcard/MIUI/sound_recorder", "comment": "recording" }, { "path": "/sdcard/MIUI/sound_recorder/app_rec", "comment": "App Recording" } ], "output": "E:/backups/MI10PRO" }
To perform backup
in the console, directly enter mib
to trigger the script without other parameters.
mib
console will output corresponding information based on the configuration file.
2022-04-09 20:58:11 info Current backup task: Screen recording2022-04-09 20:58:11 info Backup quantity 1 2022-04-09 20:58:11 info 24Mb of data obtained 2022-04-09 20:58:11 info backup size 24Mb 2022-04-09 20:58:11 info Backing up Screenrecorder-2022-04-08-19-45-51-836.mp4 2022-04-09 20:58:12 info average transfer speed 27.7 MB/s 2022-04-09 20:58:12 info Current backup task: recording 2022-04-09 20:58:12 info Backup quantity 0 2022-04-09 20:58:12 info backup size 0Mb 2022-04-09 20:58:12 info No backup required 2022-04-09 20:58:13 info Program ends
Original address: https://juejin.cn/post/7084889987631710221
Author: _island