DEV Community

Stephano Kambeta
Stephano Kambeta

Posted on

Build a Termux-based Phone Contact Backup Tool

If you keep contacts only on your phone you will lose them one day. Phones break. SIMs get lost. Accounts are hacked. A simple Termux script can export your contacts to a file. Then you can copy that file to cloud storage or to a PC. This post shows a practical, beginner friendly way to make a contact backup tool using Termux. No fluff. Just steps that work.

Why use Termux for this

Termux runs on Android. It gives you a lightweight Linux shell on your phone. You can run scripts and use standard tools. That makes automation simple. You can export contacts, encrypt the file, and upload it. If you already read quick Termux projects you will find this easy. See some ideas in Quick Termux Projects You Can Do.

Termux also lets you combine tools. For example you can use termux-contact-list to get contacts, jq to parse JSON, and rclone to upload backups to cloud drives. If you want secure uploads, pair this with a VPN like the one in my Surfshark VPN review.

What this tool will do

  • Read contacts from the phone using Termux API.
  • Convert contacts to a standard format such as vCard (.vcf) and CSV.
  • Optionally encrypt the backup with GPG.
  • Optionally upload the backup to cloud storage with rclone or save it to an SD card.

Prerequisites

Install Termux from a trusted source. Then open Termux and install packages you will need.

pkg update && pkg upgrade -y
pkg install termux-api jq bash coreutils gnupg rclone

Install the Termux:API app from the Play Store or F-Droid. Grant it the contacts permission. Without that app the contact commands will not work.

A simple script to export contacts as JSON

Termux provides termux-contact-list. It prints all contacts as JSON. Save that JSON first so you can inspect it.

termux-contact-list > contacts.json

Open contacts.json with less or cat to confirm the content. You will see objects with fields like name, phone, and email.

Convert JSON to vCard (.vcf)

Many phone and desktop contact apps accept vCard files. Use a small bash script to convert contacts.json to a vcf file.

#!/data/data/com.termux/files/usr/bin/bash
# save as contacts_to_vcf.sh and chmod +x it

if [ ! -f contacts.json ]; then
  echo "Run termux-contact-list first"
  exit 1
fi

out="contacts_backup_$(date +%Y%m%d_%H%M%S).vcf"
> "$out"

cat contacts.json | jq -c '.[]' | while read -r c; do
  name=$(echo "$c" | jq -r '.name // ""')
  phones=$(echo "$c" | jq -r '.phone[]? // empty' | tr '\n' '|' | sed 's/|$//')
  emails=$(echo "$c" | jq -r '.email[]? // empty' | tr '\n' '|' | sed 's/|$//')
  echo "BEGIN:VCARD" >> "$out"
  echo "VERSION:3.0" >> "$out"
  echo "FN:$name" >> "$out"
  if [ -n "$phones" ]; then
    IFS='|' read -ra PARR <<< "$phones"
    for p in "${PARR[@]}"; do
      echo "TEL;TYPE=CELL:$p" >> "$out"
    done
  fi
  if [ -n "$emails" ]; then
    IFS='|' read -ra EARR <<< "$emails"
    for e in "${EARR[@]}"; do
      echo "EMAIL;TYPE=INTERNET:$e" >> "$out"
    done
  fi
  echo "END:VCARD" >> "$out"
done

echo "Saved to $out"

This script handles basic names, phones, and emails. It is easy to expand. Add addresses, organization, and notes if needed. If you want CSV output instead, use a similar approach and print comma separated fields.

Make the script run automatically

Put the script in a directory like $HOME/bin. Make sure it is executable. You can run it manually or schedule it with Termux:Tasker or cron-like tools. Termux supports the termux-notification command. Use it to notify you when backup finishes.

mkdir -p $HOME/bin
mv contacts_to_vcf.sh $HOME/bin/
chmod +x $HOME/bin/contacts_to_vcf.sh

# run it once
$HOME/bin/contacts_to_vcf.sh

Optional: Encrypt the backup

If the backup file will leave your device, encrypt it. GPG works well. Create a symmetric encrypted file so you do not need key management. Use a passphrase you remember.

gpg --symmetric --cipher-algo AES256 "$out"
# produces contacts_backup_....vcf.gpg

Store the passphrase in a secure password manager. Do not store plain passphrases on the phone. For more on security basics and incident planning see my posts on Cyber Security for Small Companies and Cyber Security Plan for Small Business.

Optional: Upload to cloud with rclone

Rclone supports many cloud providers. Configure rclone with rclone config. Then upload the encrypted file with a single command.

rclone copy /path/to/contacts_backup_....vcf.gpg remote:backups/contacts

If you want a secure network when uploading, use a VPN. See my Surfshark VPN review and the post about VPNs to use when using Termux. Using a VPN reduces risks when you are on public WiFi.

Restore process

Restoring is simple. Transfer the vCard file to the device. Open the Contacts app and import the vcf. Alternatively, for bulk restores, use a script to parse vcf lines and create new contacts with termux-contact-create if available. Test restores before you need them.

Security and privacy considerations

Contacts are personal data. Treat backups like sensitive files. Encrypt them. Limit where you upload them. If your phone is used for security work or research, isolate backups from your test environment. For guidance on operational security and threats, see Operational Security Simple Guide and Understanding Information Security. If you manage a small business, consider the network security tips and incident response resources linked in this post.

If you use other Termux tools on the same device, keep separation between projects. For example, if you experiment with phishing tools like MaxPhisher in Termux, do not mix those environments with your backup files. Keep your device clean and organized. See MaxPhisher in Termux for a reminder on why separation matters.

Troubleshooting

  • If termux-contact-list returns an empty array, check that Termux:API has contacts permission. Grant access from Android settings.
  • If your vCard file imports with missing fields, inspect contacts.json to see how fields map. Add extraction logic to the script.
  • If rclone upload fails, test the cloud remote with rclone ls remote: to confirm the config.
  • If you cannot decrypt a GPG file, you likely used the wrong passphrase. Keep passphrases in a trusted manager.

Advanced ideas

Here are options to improve the tool over time.

  • Versioned backups. Keep the last N backups and delete older files automatically.
  • Automatic daily backups. Trigger the script on a schedule with Tasker integration.
  • Two factor for cloud accounts. Protect any account you use with rclone with 2FA.
  • Audit logs. Keep a small log file that records when backups ran and where they were uploaded.

Where this fits into a bigger security plan

Backups are part of a larger resilience plan. If you run a small business, contact backups are a tiny but crucial piece. Pair this tool with broader measures like network security and incident response. Read more in Network Security Tips for Small Business, Cyber Security Plan for Small Business, and Best Cyber Incident Response Companies. Those posts explain steps to protect data, and what to do if something goes wrong.

Keep learning and keep safe

This project is a practical starter. It is simple to build and tweak. If you like Termux projects, check Quick Termux Projects You Can Do for more ideas. If you worry about attackers or privacy, read about threat intelligence and NIST guidance in posts like What Is Cyber Threat Intelligence and How NISTIR 8286 Connects Cybersecurity and Business Risk.

If you want, I can expand the script to include photos and contact groups, or add an option to export only new or changed contacts. I can also provide a ready to paste script that you can run as is. Tell me which features you want and I will add them in the same clear, step by step style.

Links you may find useful:

That is all. Build this small tool and you will never lose contacts again. Keep backups encrypted. Keep the automation simple. If you want the full ready-to-run script with vCard, CSV, GPG and rclone upload included, say which options you want and I will write it in plain, copy-pasteable form.

Top comments (0)