Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Node.js and adb to develop a mobile phone backup gadget

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article "how to use Node.js and adb to develop a mobile phone backup gadget" most people do not understand, so the editor summed up the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "how to use Node.js and adb to develop a mobile phone backup gadget" article.

With the development of science and technology, the sharpness of pictures and videos we take every day continues to improve, but there is also a big disadvantage that they are also getting bigger and bigger. I still remember that when I first started using smartphones, a photo was only 2-5MB, but now a photo has reached 15-20MB or even bigger.

The storage space on our phones is limited, so how can we back up these photos and videos to make room for our phones?

So, at the beginning, I stored all this data on the cloud of a photo album. Although I solved the problem of storing these data, new problems emerged, such as upload size constraints, the need to occupy the background all the time leading to increased power consumption, advertising.

Principle

This gadget is realized by using adb debugging on the mobile phone, reading the file information in the mobile phone and copying the files in the mobile phone through the shell command.

Execution process

I drew a simple flowchart. MIB will first read the configuration file (if not, create a configuration file), read the node path that needs to be backed up according to the configuration file, and perform file backup operations. Until the node ends.

Development process

Environment required for installation

Download the adb package to perform various device operations

Download Node.js, which I believe is already available on the brothers' computers.

Install dependent libraries

Fs-extra: Node Library based on the Secondary Encapsulation of fs Module

Prompts: an interactive Node library on the command line

Winston: Node library for recording script logs

Because the project source code is a little too much, I only put the main part of the code here.

Interested partners can go to github to see the project source code github.com/QC2168/mib

Read configuration file

Export const getConfig = (): ConfigType = > {if (existConf ()) {return readJsonSync (CONFIG_PATH);} / cannot find the configuration file return createDefaultConfig ();}

When executing the script, select the device ID that needs to be backed up. And specify the device when executing the adb command

(async () = > {const device: string | boolean = await selectDevice (); if (device) MIB ();}) (); export const selectDevice = async (): Promise = > {/ / get device const list: devicesType [] = devices (); if (list.length = 0) {log ("currently no device connection, 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;}

Traversing backup nodes

After selecting the device, go to traverse the node information and copy the 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 ("current backup node is empty", "warn"); log ("Please add backup node in configuration file", "warn");} if (backups.length > 0) {isPath (output) / / parse the last folder of the backup path backups.forEach ((item: SaveItemType) = > {log (`currently executed 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 / / splice 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 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, enter the backup queue const backup = (target: string, output: string, full: boolean = false) = > {if (! full) {/ / backup non-backup file data / / obtain file information in the phone, compare with local const {backupQueue} = initData (target, output); / / calculate volume and quantity computeBackupSize (backupQueue) / / execute backup program move (backupQueue, output);} else {/ / backup moveFolder (target, output) directly without file comparison;}}; / / move files in the queue of files to be backed up const move = (backupQueue: FileNodeType [], outputDir: string): void = > {if (backupQueue.length = 0) {log ("No backup"); 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]: "read speed failed"; log (`aPCge transfer speed ${speed} `);} catch (e: any) {log (`backup ${fileN.fileName} failed error:$ {e.message}`, "error");}; scripting function

USB connection backup data

Wireless connection backup data

Multi-device backup selection

Full backup on a single node

Use

Enter the following command in the terminal to install mib globally.

Npm I @ qc2168/mib-g

Configure script Fil

For the first time, you need to create a new .mibrc file in the user directory and set the corresponding parameter contents.

{"backups": [{"path": "/ sdcard/MIUI/sound_recorder/call_rec", "comment": "call recording"}, {"path": "/ sdcard/DCIM/Camera", "comment": "Local album"} {"path": "/ sdcard/DCIM/Creative", "comment": "my creation"}, {"path": "/ sdcard/Pictures/weixin", "comment": "Wechat album"}, {"path": "/ sdcard/tencent/qq_images" "comment": "QQ album"}, {"path": "/ sdcard/Pictures/ Zhihu", "comment": "Zhihu"}, {"path": "/ sdcard/tieba", "comment": "Post Bar"} {"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": "apply recording"}], "output": "E:/backups/MI10PRO"}

Perform a backup

In the console, type mib directly to trigger the script without additional parameters.

Mib

The console will output the corresponding information according to the configuration file.

2022-04-09 20:58:11 info current backup task: screen recording 2022-04-09 20:58:11 info backup number 12022-04-09 20:58:11 info acquired data 24Mb2022-04-09 20:58:11 info backup volume 24Mb2022-04-09 20:58:11 info is backing up Screenrecorder-2022-04-08-19-45-51-836.mp42022-04-09 20:58:12 info average transfer speed 27. 7 MB/s2022-04-09 20:58:12 info current backup task: recording 2022-04-09 20:58:12 info backup number 02022-04-09 20:58:12 info backup volume 0Mb2022-04-09 20:58:12 info no backup 2022-04-09 20:58:13 info program ends above is about "how to use Node.js and adb to develop a mobile phone backup gadget" this article I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report