提示,英文部分为原文转载,中文为翻译部分。
Termux 维基
备份 Termux
此页面显示了备份 Termux 安装的示例。此处列出的说明涵盖了归档实用程序“tar”的基本用法,并显示了应归档哪些文件。 强烈建议在复制粘贴之前了解列出的命令的作用。 误解每个步骤的目的可能会不可挽回地损坏您的数据。如果这发生在你身上 - 不要抱怨。
备份
在此示例中,将显示 home 和 sysroot 的备份。生成的存档将存储在您的共享存储 ( /sdcard
) 中并使用gzip
.
- 确保授予存储权限:
termux-setup-storage
- 备份文件:
tar -zcf /sdcard/termux-backup.tar.gz -C /data/data/com.termux/files ./home ./usr
备份应该完成,没有任何错误。除非用户滥用 root 权限,否则不应有任何权限拒绝。如果您收到有关套接字文件的警告,请忽略它们。
警告 :永远不要将备份存储在 Termux 私有目录中。它们的路径可能如下所示:
/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.
从设置中清除 Termux 数据后,这些目录也会被删除。
恢复
这里将假设您已将主目录和 usr 目录备份到同一个存档中。请注意,在此过程中,所有文件都将被覆盖。
- 确保授予存储权限:
termux-setup-storage
- 提取 home 和 usr 并覆盖所有内容。通过
--recursive-unlink
以删除任何垃圾和孤立文件。传递--preserve-permissions
以在存档中设置文件权限,忽略 umask 值。通过结合这些额外的选项,您将获得与存档完全相同的安装状态。
tar -zxf /sdcard/termux-backup.tar.gz -C /data/data/com.termux/files --recursive-unlink --preserve-permissions
现在使用通知中的“退出”按钮关闭 Termux,然后再次打开它。
使用提供的脚本
最新版本的软件包“termux-tools”包含用于备份和恢复 Termux 安装的基本脚本。它们的工作方式类似于上面提到的 tar 命令。
这些脚本备份和恢复脚本不会备份、恢复或以任何其他方式触及您的主目录。 如果有疑问,请查看<span style='color:#0080ff;'>通知</span>。Termux 开发人员不对您对文件所做的事情负责。如果您设法丢失数据,那将是您自己的问题。
使用 termux-backup
带自动压缩的简单备份:
termux-backup /sdcard/backup.tar.xz
压缩格式由文件扩展名决定,通常为 .tar.gz (gzip)、.tar.xz (xz) 或 .tar(无压缩)。
可以将备份内容流式传输到标准输出,例如使用 GnuPG 实用程序对其进行加密或发送到远程存储。将文件名设置为“-”以启用流式传输到标准输出:
termux-backup - | gpg --symmetric --output /sdcard/backup.tar.gpg
写入标准输出的内容不会被压缩。
使用 termux-restore
警告 :恢复过程将破坏以前存储在 $PREFIX 中的任何数据。脚本将完全回滚到与备份存档中完全相同的状态。
恢复备份也很简单:
termux-restore /sdcard/backup.tar.xz
完成后,重新启动 Termux 应用程序。
该实用程序termux-restore能够从标准输入读取备份数据。您可以使用它来阅读其他工具提供的内容。不得压缩提供给标准输入的备份内容。请参阅以下示例以从加密的压缩备份中恢复:
export GPG_TTY=$(tty)
gpg --decrypt /sdcard/backup.tar.gz.gpg | gunzip | termux-restore -
请注意,如果还原过程在完成之前终止,您的环境将被破坏。
Termux Wiki
Backing up Termux
This page shows an example of backing up your Termux installation. Instructions listed there cover basic usage of archiving utility "tar" as well as show which files should be archived. It is highly recommended to understand what the listed commands do before copy-pasting them. Misunderstanding the purpose of each step may irrecoverably damage your data. If that happened to you - do not complain.
Backing up
In this example, a backup of both home and sysroot will be shown. The resulting archive will be stored on your shared storage (/sdcard
) and compressed with gzip
.
- Ensure that storage permission is granted:
termux-setup-storage
- 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 will be assumed that you have backed up both home and usr directory into same archive. Please note that all files would be overwritten during the process.
- Ensure that storage permission is granted:
termux-setup-storage
- 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 archive, ignoring the umask value. By combining these extra options you will get installation state exactly as was in 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 notification and open it again.
Using supplied scripts
The latest version of package "termux-tools" contains basic scripts for backup and restore of Termux installation. They are working in a way similar to tar commands mentioned above.
These scripts backup and restore scripts will not backup, restore or in any other way touch your home directory. Check out the notice if have questions why so. Termux developers are not responsible with what you are doing with your files. If you managed to lost data, that would be your own problem.
Using termux-backup
Simple backup with auto compression:
termux-backup /sdcard/backup.tar.xz
Compression format is determined by 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 GnuPG utility or send to remote storage. Set file name as "-" to enable streaming to stdout:
termux-backup - | gpg --symmetric --output /sdcard/backup.tar.gpg
Content written to stdout is not compressed.
Using termux-restore
Warning: restore procedure will destroy any previous data stored in $PREFIX. Script will perform a complete rollback to state state exactly as in backup archive.
Restoring backup is also simple:
termux-restore /sdcard/backup.tar.xz
Once finished, restart Termux application.
The utility termux-restore
is able to read backup data from standard input. You can use this for reading content supplied by other tool. Backup contents supplied to stdin must not be compressed. See below example for restoring from encrypted, compressed backup:
export GPG_TTY=$(tty)
gpg --decrypt /sdcard/backup.tar.gz.gpg | gunzip | termux-restore -
Please note that if restore procedure will be terminated before finish, your environment will be corrupted.