#!/data/data/com.termux/files/usr/bin/bash # TERBR - Termux Backup/Restore Tool # Copyright (C) 2020 MrAlpha786 # # This program is free software: you can redistribute it # and/ or modify it under the terms of the GNU General Public # License as published by the Free Software Foundation either # version 3 of the License, or (at your option) any later # version. # # This program is distributed in the hope that it will be # useful, but WITHOUT ANY WARRANTY; without even the implied # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR # PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General # Public License along with this program. If not, see # . # # Author: MrAlpha786 (github/MrAlpha786) # #colors and other variable R='\033[1;31m' B='\033[1;34m' C='\033[0;36m' G='\033[1;32m' Y='\033[1;33m' N='\033[0m' TERBR_COMMAND='tar' TERBR_DIRS='home' TERBR_BACKUP_FILE="/storage/emulated/0/termux_backup$(date +%m%d%Y_%H%M%S).bak" readonly BACKUP_SRC='/data/data/com.termux/files' readonly TERBR_HISTFILE=$PREFIX/var/log/apt/history.log readonly TERBR_PKGS=~/pkglist.out function banner () { echo echo -e "$B ┌───────────────────────┐" echo -e "$B │$C ▀█▀ █▀▀ █▀▀▄ █▀▄ █▀▀▄ $B│" echo -e "$B │$C ─█─ █▀▀ █▐█▀ █▀█ █▐█▀ $B│" echo -e "$B │$C ─▀─ ▀▀▀ ▀─▀▀ ▀▀─ ▀─▀▀ $B│" echo -e "$B └───────────────────────┘" echo -e "$G -> Author: MrAlpha786 $N" echo } function list_packages () { # Grep 'histfile' and create two arrays # one of installed pkgs and one of removed pkgs while IFS= read -r line; do line=${line/-y/} if [[ "$line" =~ ' install ' ]]; then installed+=(${line##* install }) else removed+=(${line##* remove }) removed+=(${line##* purge }) fi done < <(grep -E ' install | remove | purge ' ${TERBR_HISTFILE}) # For every removed pkg, if it's also in installed pkgs # remove it from installed pkgs for pkg in "${removed[@]}";do for i in "${!installed[@]}";do if [[ "${installed[$i]}" == "$pkg" ]]; then unset installed[$i] break fi done done # Remove any duplicates # And put them in TERBR_PKGS declare -A tmp_array for i in "${installed[@]}"; do [[ $i ]] && IFS=" " tmp_array["${i:- }"]=1 done echo -e "${G} Saving installed packages to '${TERBR_PKGS}'\n" printf '%s\n' "${!tmp_array[@]}" > $TERBR_PKGS if [ -n "${TERBR_VERBOSE}" ];then echo -e "$G [*] Installed Packages $N\n" nl "${TERBR_PKGS}" fi } function backup () { cd $BACKUP_SRC list_packages || error_exit echo -e "${G} Saving Backup to '${TERBR_BACKUP_FILE}'\n" echo -e "${Y} [*] Backing Up Files.............." echo -e "${R} [!] It may take a while. Please wait...$N\n" ${TERBR_COMMAND} "${TERBR_BACKUP_FILE}" ${TERBR_DIRS} | nl || error_exit echo -e "${G} [!] Backup Done! " } function restore () { cd $BACKUP_SRC echo -e "${G} Restoring from '${TERBR_RESTORE_FILE}'\n" echo -e "${Y} [*] Restoring Files..............." echo -e "${R} [!] It may take a while. Please wait...\n" ${TERBR_COMMAND} "${TERBR_RESTORE_FILE}" ${TERBR_DIRS} | nl || error_exit echo -e "${G} [!] Restore Done! " } function usage () { echo -e "Usage: $(basename $0) -[hvVbr] " echo -e "Create Termux Backup and Restore it.\n" echo -e " -b Take backup" echo -e " -r [FILE] Restore backup" echo -e " -p List Installed Packages" echo -e " -v Verbose output" echo -e " -h Print usage" echo -e " -V Print version" } function version () { echo "Terbr - Termux Backup and Restore Tool" echo "A tool to Backup and Restore Termux" echo "Copyright (C) 2020 MrAlpha786" echo "This program is free software: you can redistribute it and/or modify it under the terms of the GNU GPL as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version." echo echo "This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." echo echo "Author: MrAlpha786 (github.com/MrAlpha786)" } #Print message to standard error function print_error () { echo "$(basename $0): $1" >&2 } # Cleanup on unexpected error function error_exit () { SIGNAL=$? rm -f "${TERBR_BACKUP_FILE}" exit ${SIGNAL} } # Trap signals trap 'error_exit' 1 2 3 15 20 while getopts ':hvVpbr:' arg; do case $arg in b) unset TERBR_RESTORE; TERBR_BACKUP=1;; r) unset TERBR_BACKUP; TERBR_RESTORE=1; TERBR_RESTORE_FILE="${OPTARG}";; p) TERBR_PKG_LIST=1;; v) TERBR_VERBOSE=1;; h) usage; exit;; V) version; exit;; :) print_error "'${OPTARG}' option requires an argument."; usage >&2; exit 1;; ?) print_error "unknown option '${OPTARG}'."; usage >&2; exit 1;; esac done # Show usage if no option is provided if [ ${OPTIND} -eq 1 ]; then usage >&2; exit 1 fi # Check Storage Permission if [ ! -w /storage/emulated/0/ ]; then termux-setup-storage fi # Check if pigz is installed if type -p pigz &> /dev/null; then TERBR_COMMAND+=' -I pigz' else TERBR_COMMAND+=' -z' fi # Enable verbose if [ -n "${TERBR_VERBOSE}" ]; then TERBR_COMMAND+=' -v' fi # display terbr banner banner # list installed packages if [ -n "${TERBR_PKG_LIST}" ]; then list_packages fi # Backup if [ -n "${TERBR_BACKUP}" ]; then TERBR_COMMAND+=' -cf' backup fi # Restore if [ -n "${TERBR_RESTORE}" ]; then # Check if file to restore exists. if [ ! -f "${TERBR_RESTORE_FILE}" ]; then print_error "${TERBR_RESTORE_FILE}: No such file or directory exists." exit 1 fi TERBR_COMMAND+=' -xf' restore fi exit 0