Automated, robust, and encrypted USB pendrive backup system designed for Android (Termux) with multi-part chunking (~500MB per file), self-healing Syncthing synchronization, and cross-platform restoration.
On your Android phone in Termux, run this single command to download and set up everything automatically:
pkg upgrade -y && pkg install curl -y && bash -c "$(curl -fsSL https://raw.githubusercontent.com/mahdi4261/backup-script/main/setup-backup.sh)"(Alternatively, via Git clone:)
pkg upgrade -y && pkg install git -y && git clone https://github.com/mahdi4261/backup-script.git ~/backup-script && bash ~/backup-script/setup-backup.sh- Dedicated Folders per Drive: Each pendrive is backed up into its own folder:
~/storage/shared/.backups/<UUID>/. - ~500MB Multi-Part Splitting: Archives are chunked into 500MB parts (
part-001,part-002, ...), overcoming the 4GB FAT32 Android file limit. - In-Memory Streaming: Zero multi-gigabyte temporary files on disk; compression, encryption, and splitting stream directly through memory pipelines.
- Hybrid RSA + AES Encryption (Optional): Secures pendrive data using public-key hybrid encryption before storing or syncing.
- Syncthing Integration:
- Automatically installs and starts Syncthing in the background (
--no-browser). - Per-Part Incremental Syncing: Triggers sync after each 500MB part completes (and instantly for
key.enc), so files transfer over the network while subsequent parts are still being processed. - Watchdog in
backup.shensures Syncthing stays running.
- Automatically installs and starts Syncthing in the background (
- Android CPU Wake Lock: Holds
termux-wake-lockto keep backups and Syncthing alive when the screen is locked. - Safe Atomic Operations: Parts are streamed to temporary chunks (
part-*.tmp) ignored by Syncthing until fully written, with automatic recovery if an interrupted backup is detected. - Cross-Platform Restore Utility: Includes
restore.shto decrypt and extract backup parts back into original directory structures.
| File | Description |
|---|---|
setup-backup.sh |
Automated 1-step installer for dependencies, permissions, Syncthing, and scripts |
backup.sh |
Main scanner and backup engine (detects pendrives, compresses, splits, encrypts) |
restore.sh |
Restore & extraction utility (works on Android Termux, Linux, macOS, and WSL) |
backup_public_key.pem |
Public key certificate used for encrypting backups |
run_tests.sh |
Automated local test suite verifying multi-part backups, restore, and rotation |
Plug in your USB pendrive via OTG and run:
bash ~/backup.shThe setup script automatically registers the backup job and starts the crond daemon to scan every 1 minute in the background.
To inspect or edit your scheduled cron schedule:
crontab -l(The configured entry runs every 1 minute: * * * * * ... ~/backup.sh)
To extract original files from a backup folder:
# For unencrypted backups:
./restore.sh ~/storage/shared/.backups/<UUID> [destination_folder]
# For encrypted backups (with your private key):
./restore.sh ~/storage/shared/.backups/<UUID> ~/backup_private_key.pem [destination_folder]- Open your phone's browser to
http://127.0.0.1:8384. - Click Actions
$\to$ Show ID to view your phone's Syncthing Device ID. - On your remote PC / Server, install Syncthing and click Add Remote Device, then paste your phone's Device ID.
- On your phone, add folder
~/storage/shared/.backups(or/storage/emulated/0/.backups), check the box for your remote PC under Sharing, and set Folder Type to Send Only. - Accept the folder on your PC as Receive Only to automatically mirror all pendrive backups!
Important
Keep your backup_private_key.pem safe and never commit it to public repositories. Only the public key (backup_public_key.pem) is stored on the phone to encrypt data.