2021-09-25 13:26:10 +02:00
|
|
|
#!@TERMUX_PREFIX@/bin/bash
|
2021-10-28 11:46:36 +02:00
|
|
|
##
|
|
|
|
## Termux script for restoring $PREFIX (package installation dir).
|
|
|
|
## For backup script, see "termux-backup".
|
|
|
|
##
|
|
|
|
## Why it doesn't implement support for restoring $HOME:
|
|
|
|
##
|
|
|
|
## * It can't selectively restore files due to tar option
|
|
|
|
## --recursive-unlink, that will erase file tree before extracting.
|
|
|
|
##
|
|
|
|
## * It has to maintain support of piped input. Therefore we can't
|
|
|
|
## allow optional backup of home by termux-backup. Here is why:
|
|
|
|
## with piped input it is not possible to check contents of backup
|
|
|
|
## file (you can't seek piped content twice). We must be sure that
|
|
|
|
## all directories selected for restore are present inside archive
|
|
|
|
## and this is possible only with hardcoded configuration
|
|
|
|
## (e.g. backup only $PREFIX). Otherwise, restore procedure may
|
|
|
|
## fail or even worse, erase all directories (potentially user
|
|
|
|
## data) that are not present in backup.
|
|
|
|
##
|
|
|
|
## Considering the mentioned 2 major issues, should be obvious that
|
|
|
|
## we have literally 2 choices: either backup/restore only $PREFIX
|
|
|
|
## or backup/restore both $HOME and $PREFIX unconditionally, to
|
|
|
|
## ensure consistency of data passed to termux-restore.
|
|
|
|
##
|
|
|
|
## However the current choice is to stick to backing up only $PREFIX,
|
|
|
|
## to leave responsibility of managing files in $HOME to user (as said
|
|
|
|
## above, termux-restore script ERASES DATA before restore).
|
|
|
|
##
|
|
|
|
## On side note, why data should be erased before restore:
|
|
|
|
##
|
|
|
|
## * Perform a complete rollback to state exactly as in archive.
|
|
|
|
##
|
|
|
|
## * Get rid of files that are not tracked by package manager.
|
|
|
|
##
|
|
|
|
|
2021-09-25 13:26:10 +02:00
|
|
|
set -e -u
|
|
|
|
|
|
|
|
export PREFIX=@TERMUX_PREFIX@
|
|
|
|
|
|
|
|
msg() {
|
2021-10-08 21:05:52 +02:00
|
|
|
echo "$*" >&2
|
2021-09-25 13:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
show_usage() {
|
|
|
|
msg
|
|
|
|
msg "Usage: termux-restore [input file]"
|
|
|
|
msg
|
|
|
|
msg "Script for restoring Termux installation directory (\$PREFIX)"
|
|
|
|
msg "from the given TAR archive."
|
|
|
|
msg
|
|
|
|
msg "It is expected that backup file was made by 'termux-backup'."
|
|
|
|
msg "Restoring procedure will erase all files in \$PREFIX directory"
|
|
|
|
msg "that are not present in the given backup file. Be careful."
|
|
|
|
msg
|
|
|
|
msg "Backup contents may be supplied via stdin by specifying input"
|
|
|
|
msg "file as '-'. Note that piped TAR archive must be uncompressed."
|
|
|
|
msg
|
|
|
|
}
|
|
|
|
|
2021-10-28 11:46:36 +02:00
|
|
|
if [ "$(id -u)" = "0" ]; then
|
|
|
|
msg "This script should not be used as root."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-09-25 13:26:10 +02:00
|
|
|
if [ $# -lt 1 ]; then
|
|
|
|
msg
|
|
|
|
msg "[!] Input file path is not specified."
|
|
|
|
show_usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ $# -gt 1 ]; then
|
|
|
|
shift 1
|
|
|
|
msg
|
2021-10-08 21:05:52 +02:00
|
|
|
msg "[!] Got extra arguments: $*"
|
2021-09-25 13:26:10 +02:00
|
|
|
show_usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
case "$1" in
|
2021-10-08 21:05:52 +02:00
|
|
|
-\?|-h|--help|--usage) show_usage; exit 0;;
|
2021-09-25 13:26:10 +02:00
|
|
|
*) BACKUP_FILE_PATH=$1;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if [ "$BACKUP_FILE_PATH" != "-" ] && [ ! -e "$BACKUP_FILE_PATH" ]; then
|
|
|
|
msg
|
|
|
|
msg "[!] File '$BACKUP_FILE_PATH' does not exist."
|
|
|
|
msg
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
if [ "$BACKUP_FILE_PATH" != "-" ] && [ ! -f "$BACKUP_FILE_PATH" ]; then
|
|
|
|
msg
|
|
|
|
msg "[!] Path '$BACKUP_FILE_PATH' is not a regular file."
|
|
|
|
msg
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2021-10-08 21:01:46 +02:00
|
|
|
# Ensure that prefix doesn't contain read-only files.
|
2021-10-28 11:46:36 +02:00
|
|
|
msg "Fixing read-write access to files where necessary..."
|
2021-10-08 21:01:46 +02:00
|
|
|
find "@TERMUX_PREFIX@" -type d -print0 | xargs -0 -r chmod u+rwx
|
|
|
|
find "@TERMUX_PREFIX@" -type f -print0 | xargs -0 -r chmod u+rw
|
|
|
|
|
2021-09-25 13:26:10 +02:00
|
|
|
# --recursive-unlink is added intentionally to delete all orphan/extra files
|
|
|
|
# in $PREFIX. It must be restored to a clean state as in backup tarball.
|
2021-10-28 11:46:36 +02:00
|
|
|
msg "Erasing current \$PREFIX and restoring one from archive..."
|
2021-09-25 13:26:10 +02:00
|
|
|
tar -x -C "@TERMUX_BASE_DIR@" -f "$BACKUP_FILE_PATH" \
|
|
|
|
--recursive-unlink --preserve-permissions ./usr
|