📥 Backing Up
In this example, we'll back up both the home and sysroot directories. The resulting archive will be stored on your shared storage (`/sdcard`) and compressed with `gzip`.
1. Ensure that storage permission is granted:
`termux-setup-storage`
2. Backing up files:
`tar -zcf /sdcard/termux-backup.tar.gz -C /data/data/com.termux/files ./home ./usr`
Backup should be finished without any error. There shouldn't be any permission denials unless the user abused root permissions. If you got some warnings about socket files, ignore them.
⚠️ Warning: never store your backups in Termux private directories. Their paths may look like:
- `/data/data/com.termux` - private Termux directory on internal storage
- `/sdcard/Android/data/com.termux` - private Termux directory on shared storage
- `/storage/XXXX-XXXX/Android/data/com.termux` - private Termux directory on external storage, XXXX-XXXX is the UUID of your micro-sd card.
- `${HOME}/storage/external-1` - alias for Termux private directory on your micro-sd.
Once you clear Termux data from settings, these directories are erased too.
⤴️ Restoring
Here, we assume that you have backed up both the home and usr directories into the same archive. Please note that all files will be overwritten during the process.
1. Ensure that storage permission is granted:
`termux-setup-storage`
2. Extract home and usr with overwriting everything. Pass `--recursive-unlink` to remove any junk and orphaned files. Pass `--preserve-permissions` to set file permissions as in the archive, ignoring the umask value. By combining these extra options, you will get the installation state exactly as it was in the archive.
`tar -zxf /sdcard/termux-backup.tar.gz -C /data/data/com.termux/files --recursive-unlink --preserve-permissions`
Now close Termux with the "exit" button from the notification and open it again.
📜 Using supplied scripts
The latest version of the "termux-tools" package contains basic scripts for backup and restore of Termux installation. They work in a way similar to the tar commands mentioned above.
🚫 **These scripts backup and restore scripts will not back up, restore, or in any other way touch your home directory.**
Check out the [notice] if you have questions about why this is the case. Termux developers are not responsible for what you do with your files. If you manage to lose data, that's your own problem.
💾 Using termux-backup
Simple backup with auto compression:
`termux-backup /sdcard/backup.tar.xz`
The compression format is determined by the file extension, which is typically `.tar.gz` (gzip), `.tar.xz` (xz), or `.tar` (no compression).
It is possible to stream backup contents to standard output, for example, to encrypt it with the GnuPG utility or send it to remote storage. Set the file name as "-" to enable streaming to stdout:
`termux-backup - | gpg --symmetric --output /sdcard/backup.tar.gpg`
Content written to stdout is not compressed.