Tech, business, and useful tricks.
Vheüel Insight Network

How to Back Up and Restore Termux

•6 min read

Back up and restore your Termux scripts, color themes, fonts, and installed packages to Android internal storage without root using simple shell commands.

Vheüel

Vheüel

Author

Uninstalling Termux or clearing its app data immediately wipes the entire /data/data/com.termux/files directory. Without a proper backup, your custom scripts, terminal color schemes, SSH keys, and dozens of installed packages vanish permanently.

Because Android isolates private app directories from shared user storage, you need to copy critical Termux data to /sdcard on a regular basis. This guide covers selective and full backups, restoring your environment on a new device, and automating the process with a single shell script.


1. Grant Internal Storage Permissions

Before copying any files out of the Termux sandbox, grant the app permission to read and write to Android's shared internal storage.

  1. Run the storage setup command: Open Termux, execute the command below, and tap Allow on the Android permission prompt:
    termux-setup-storage
  2. Create a dedicated backup directory: Set up a centralized folder in your phone's main storage so backup files stay organized:
    mkdir -p /sdcard/Backup_Termux

    Confirm that no Permission denied error appears and that the Backup_Termux folder is visible in your Android File Manager.


2. Back Up Specific Scripts and Project Folders

This method works best when you only need to safeguard individual code files or specific project directories so you can open or transfer them directly via your Android File Manager.

Use the cp command for single files, or add the -r (recursive) flag to copy an entire directory along with its subdirectories:

# Copy a single script file
cp my_script.py /sdcard/Backup_Termux/

# Copy an entire project folder
cp -r my_project /sdcard/Backup_Termux/

Verify the transfer by running ls /sdcard/Backup_Termux and confirming your files appear in the output.


3. Back Up Color Schemes, Fonts, and Shell Configurations

Visual customizations—including your color palette (colors.properties), typeface (font.ttf), and extra key row layout (termux.properties)—live inside the hidden ~/.termux directory. Command aliases and environment variables reside in shell dotfiles such as .bashrc or .zshrc.

Copy these settings to internal storage while removing the leading dot from the directory name so Android file managers do not hide it:

mkdir -p /sdcard/Backup_Termux/settings
cp -r ~/.termux /sdcard/Backup_Termux/settings/termux_ui
cp ~/.bashrc ~/.zshrc ~/.profile /sdcard/Backup_Termux/settings/ 2>/dev/null

Appending 2>/dev/null suppresses error messages if one of the shell configuration files (such as .zshrc) does not exist on your system.


4. Export a List of Installed Packages and Modules

Saving a plain-text list of installed packages takes almost zero storage space and avoids compatibility issues if you migrate to a phone with a different CPU architecture.

  1. Export main Termux packages (pkg / apt): Running apt-mark showmanual records only the packages you explicitly installed, leaving out hundreds of automatic base dependencies:
    apt-mark showmanual > /sdcard/Backup_Termux/package_list.txt
  2. Export Python and Node.js modules (Optional): If you install global libraries via pip or npm, save them to separate text files:
    pip freeze > /sdcard/Backup_Termux/pip_packages.txt 2>/dev/null
    npm list -g --depth=0 > /sdcard/Backup_Termux/npm_packages.txt 2>/dev/null

5. Create a Full System and Configuration Archive (.tar.gz)

If you want to avoid redownloading packages individually when setting up Termux again, bundle both the home and usr directories into a single compressed archive.

Important Safeguards Before Archiving:

  • Exclude the storage folder: Including storage causes tar to traverse symlinks into your phone's entire internal memory, creating an infinite loop until storage fills up.
  • Check CPU architecture: Backups containing the usr directory can only be restored cleanly on devices sharing the same processor architecture (for example, aarch64 to aarch64).

Choose either option below depending on your needs:

# Option A: Back up the Home directory only (lightweight and cross-device safe)
tar --exclude='./storage' -czvf /sdcard/Backup_Termux/home_backup_$(date +%Y%m%d).tar.gz -C ~ .

# Option B: Full backup of Home + all installed binaries in usr
tar --exclude='./home/storage' -czvf /sdcard/Backup_Termux/termux_full_$(date +%Y%m%d).tar.gz -C /data/data/com.termux/files ./home ./usr

Using $(date +%Y%m%d) automatically stamps the filename with the current date (such as termux_full_20261129.tar.gz) so newer backups never overwrite older snapshots.


6. Restore Your Backup on a Fresh Termux Installation

When reinstalling Termux or setting up a new phone, run termux-setup-storage first so Termux can read /sdcard/Backup_Termux.

A. Restore Settings and Reinstall Packages (From Sections 3 & 4)

Copy your configuration directory back to its original path, reload Termux UI settings, and batch-install your saved package list:

cp -r /sdcard/Backup_Termux/settings/termux_ui ~/.termux
cp /sdcard/Backup_Termux/settings/.bashrc /sdcard/Backup_Termux/settings/.zshrc ~/ 2>/dev/null
termux-reload-settings

# Reinstall all core packages
pkg update && pkg install -y $(cat /sdcard/Backup_Termux/package_list.txt)

If you backed up Python dependencies, restore them with pip install -r /sdcard/Backup_Termux/pip_packages.txt.

B. Restore a Full Home and Usr Archive (From Section 5, Option B)

Extract the archive directly into the Termux root filesystem while preserving executable file permissions:

tar -xzvf /sdcard/Backup_Termux/termux_full_YYYYMMDD.tar.gz -C /data/data/com.termux/files --recursive-unlink --preserve-permissions

(Replace YYYYMMDD with the date stamp on your backup file.)

Once extraction finishes, type exit, navigate to Android Settings > Apps > Termux, and tap Force Stop. Relaunch Termux to load the restored binaries and theme cleanly.


7. Automate Backups with a Single Shell Script (backup.sh)

Typing multiple commands every week quickly becomes tedious. Combine your selective backup steps into one executable script so you can back up everything with a single command.

  1. Create a new script file:
    nano ~/backup.sh
  2. Paste the following code:
    #!/data/data/com.termux/files/usr/bin/bash
    
    TARGET="/sdcard/Backup_Termux/auto_$(date +%Y%m%d)"
    mkdir -p "$TARGET"
    
    echo "[1/3] Backing up UI and shell configurations..."
    cp -r ~/.termux "$TARGET/termux_ui" 2>/dev/null
    cp ~/.bashrc ~/.zshrc ~/.profile "$TARGET/" 2>/dev/null
    
    echo "[2/3] Exporting installed package lists..."
    apt-mark showmanual > "$TARGET/package_list.txt"
    pip freeze > "$TARGET/pip_packages.txt" 2>/dev/null
    
    echo "[3/3] Archiving Home directory..."
    tar --exclude='./storage' -czf "$TARGET/home_files.tar.gz" -C ~ .
    
    echo "Backup complete. Files saved to: $TARGET"
  3. Save and make the script executable: Press Ctrl + O then Enter to save, followed by Ctrl + X to exit nano. Grant execution permissions:
    chmod +x ~/backup.sh

Whenever you write a new script or tweak your terminal theme, run ~/backup.sh to snapshot your environment to internal storage immediately.


Adding Off-Device Redundancy

Storing backups in /sdcard protects your work if you reinstall Termux, but local files will still be lost if your phone suffers hardware failure or theft. For active coding projects, push your repositories to a private GitHub or GitLab remote using git push. For full configuration snapshots, upload your dated .tar.gz archives to cloud storage such as Google Drive—either manually or directly from the terminal using rclone.

Join Bybit