├── LICENSE ├── README.md ├── rewind └── rewind.png /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 phoenix 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rewind - A termux backup/restore tool 2 | 3 | 4 | Introducing rewind - the ultimate solution for all your data backup and restore needs on the popular Android terminal emulator, Termux. Say goodbye to the hassle of manual data transfer and hello to seamless backups with just a few simple commands. With rewind, you can easily backup your important files, and even better, create a list of all the packages you've installed so they can be reinstalled with ease during a restore. Experience the power and convenience of rewind, the ultimate backup and restore tool for Termux. 5 | 6 | ![rewind](rewind.png) 7 | 8 | 9 | ### created by [Laraib07](https://github.com/laraib07) 10 | 11 | ## Installation 12 | 13 | Just copy paste this in your termux. 14 | 15 | 16 | ```bash 17 | wget https://raw.githubusercontent.com/laraib07/TermuxBackupTools/master/rewind && chmod u+x rewind && mv rewind $PREFIX/bin/ 18 | ``` 19 | 20 | or 21 | 22 | ```bash 23 | curl -O https://raw.githubusercontent.com/laraib07/TermuxBackupTools/master/rewind && chmod u+x rewind && mv rewind $PREFIX/bin/ 24 | ``` 25 | 26 | ## Usage 27 | 28 | Usage : **rewind** [-hv] [-b|-r [home|pkgs]] 29 | 30 | option | Description 31 | :---------------:|:---------------------------: 32 | -h | print this usage 33 | -v | print version 34 | -b [home\|pkgs] | backup home and/or packages 35 | -r [home\|pkgs] | restore home and/or packages 36 | 37 | -------------------------------------------------------------------------------- /rewind: -------------------------------------------------------------------------------- 1 | #!/data/data/com.termux/files/usr/bin/bash 2 | 3 | #********************************************# 4 | # # 5 | # Rewind v6.0 # 6 | # A CLI tool to backup and restore termux # 7 | # Author : https://github.com/laraib07 # 8 | # # 9 | #********************************************# 10 | 11 | 12 | #variables 13 | readonly SOURCE="$HOME" 14 | readonly DEST='/storage/emulated/0/Termux/Backup' 15 | readonly PKGS_SRC="$PREFIX/var/log/apt/history.log" 16 | readonly program_name=$(basename "$0") 17 | 18 | #colors 19 | R='\e[1;31m' # red 20 | G='\e[1;32m' # green 21 | W='\e[1;37m' # white bold 22 | off='\e[0m' # turn off color 23 | t=' ' # tab 24 | OK=" $W[$G - $W]$off" 25 | EXC=" $W[$R ! $W]$off" 26 | 27 | 28 | # Banner 29 | read -r -d '' BANNER << EOF 30 | $W■──────────────────────────────────■ 31 | │$R █▀▀█ █▀▀ █ █ ▀ █▀▀▄ █▀▀▄ $W │ 32 | │$R █▄▄▀ █▀▀ █▄█▄█ ▀█▀ █ █ █ █ $W │ 33 | │$R ▀ ▀▀ ▀▀▀ ▀ ▀ ▀▀▀ ▀ ▀ ▀▀▀ $W │ 34 | ■──────────────────────────────────■$off 35 | EOF 36 | 37 | 38 | # FUNCTIONS 39 | function show_help() { 40 | printf "${off}Usage: ${W}rewind${off} [-hv] [-b|-r [home|pkgs]]\n 41 | -h print this usage 42 | -v print version 43 | -b [home|pkgs] backup home and/or packages 44 | -r [home|pkgs] restore home and/or packages\n" 45 | } 46 | 47 | 48 | function show_version() { 49 | grep "#$" "$0" 50 | } 51 | 52 | 53 | function error() { 54 | printf "${program_name}: $*\n" >&2 55 | } 56 | 57 | 58 | function check_internet() { 59 | if ping -q -w 1 -c 1 8.8.8.8 &> /dev/null;then 60 | return 0 61 | fi 62 | return 1 63 | } 64 | 65 | 66 | function backup_home() { 67 | printf "${EXC} Backing up home\n" 68 | printf "${t}Be patient...\n\n" 69 | 70 | if tar -I pigz -cf ${DEST}/backup.tmp -C ${SOURCE} . ; then 71 | mv ${DEST}/backup.tmp ${DEST}/home.bak 72 | printf "${OK} home backed up and can be found in\n" 73 | printf "${t}${DEST}/home.bak\n\n" 74 | 75 | else 76 | error "backup failed." 77 | fi 78 | } 79 | 80 | 81 | function backup_pkgs(){ 82 | printf "${EXC} Backing up packages\n\n" 83 | 84 | # Grep pkgs and create two arrays 85 | # one of installed pkgs and one of removed pkgs 86 | while IFS= read -r line; do 87 | line=${line/-y/} 88 | if [[ "$line" =~ ' install ' ]]; then 89 | installed+=(${line##* install }) 90 | else 91 | removed+=(${line##* remove }) 92 | removed+=(${line##* purge }) 93 | fi 94 | done < <(grep -E ' install | remove | purge ' ${PKGS_SRC}) 95 | 96 | 97 | # For every removed pkg, if it's also in installed pkgs 98 | # remove it from installed pkgs 99 | for pkg in "${removed[@]}";do 100 | for i in "${!installed[@]}";do 101 | if [[ "${installed[$i]}" == "$pkg" ]]; then 102 | unset installed[$i] 103 | break 104 | fi 105 | done 106 | done 107 | 108 | 109 | # Remove any duplicates 110 | # And put installed pkgs in output 111 | declare -A tmp_array 112 | 113 | for i in "${installed[@]}"; do 114 | [[ $i ]] && IFS=" " tmp_array["${i:- }"]=1 115 | done 116 | 117 | printf "${t}%s\n" "${!tmp_array[@]}" | tee ${DEST}/pkgs.bak 118 | 119 | printf "\n${OK} Packages backed up and can be found in\n" 120 | printf "${t}${DEST}/pkgs.bak\n\n" 121 | } 122 | 123 | 124 | function backup() { 125 | if [[ ! -d "${DEST}" ]]; then 126 | mkdir -p "${DEST}" 127 | fi 128 | 129 | printf "${BANNER}\n\n" 130 | 131 | case "$1" in 132 | home ) 133 | backup_home 134 | ;; 135 | pkgs ) 136 | backup_pkgs 137 | ;; 138 | '' ) 139 | backup_home 140 | backup_pkgs 141 | ;; 142 | * ) 143 | error "invalid argument" 144 | show_help 145 | exit 1 146 | ;; 147 | esac 148 | } 149 | 150 | 151 | function restore_home() { 152 | if [[ -f "${DEST}"/home.bak ]]; then 153 | printf "${EXC} Restoring home\n" 154 | printf "${t}Be patient...\n\n" 155 | 156 | if tar -I pigz -xf ${DEST}/home.bak -C ${SOURCE} ; then 157 | printf "${OK} home restored\n" 158 | printf "${t}start a new session.\n\n" 159 | fi 160 | 161 | else 162 | error "home backup not found in ${DEST}" 163 | exit 1 164 | fi 165 | } 166 | 167 | 168 | function restore_pkgs() { 169 | if ! check_internet; then 170 | error 'no internet connection.' 171 | exit 1 172 | fi 173 | 174 | if [[ -f "${DEST}/pkgs.bak" ]]; then 175 | printf "${EXC} Restoring packages\n" 176 | printf "${t}Be patient...\n\n" 177 | 178 | if apt-get install -y $(awk '{print $1}' ${DEST}/pkgs.bak ) ; then 179 | 180 | printf "${OK} packages restored\n" 181 | printf "${t}start a new session.\n\n" 182 | fi 183 | 184 | else 185 | error "packages backup not found in ${DEST}" 186 | exit 1 187 | fi 188 | } 189 | 190 | 191 | function restore() { 192 | printf "${BANNER}\n\n" 193 | 194 | case "$1" in 195 | home ) 196 | restore_home 197 | ;; 198 | pkgs ) 199 | restore_pkgs 200 | ;; 201 | '' ) 202 | restore_home 203 | restore_pkgs 204 | ;; 205 | * ) 206 | error "invalid argument" 207 | show_help 208 | exit 1 209 | ;; 210 | esac 211 | } 212 | 213 | 214 | function install_dependencies() 215 | { 216 | # checking net connection 217 | if check_internet ; then 218 | printf "${EXC} Installing Dependencies...\n" 219 | 220 | apt-get update && apt-get upgrade -y 221 | apt-get install -y tar pigz 222 | 223 | if [[ $(type -P tar) && $(type -P pigz) ]];then 224 | printf "${OK} Dependencies Installed...\n" 225 | fi 226 | 227 | else 228 | error "network connection failed" 229 | exit 1 230 | fi 231 | } 232 | 233 | 234 | function getopts_get_optional_argument() { 235 | eval next_token=\${$OPTIND} 236 | if [[ -n $next_token && $next_token != -* ]]; then 237 | OPTIND=$((OPTIND + 1)) 238 | OPTARG=$next_token 239 | else 240 | OPTARG="" 241 | fi 242 | } 243 | 244 | 245 | # remove temporary files in SOURCE if any signal received. 246 | function cleanup() { 247 | rm -f "${DEST}/backup.tmp" 248 | error "\nprogramme terminated unexpectedly\n" 249 | exit 1 250 | } 251 | 252 | # trapping signal 253 | trap cleanup 1 2 3 15 20 254 | 255 | # installing required programmes 256 | if ! [[ $(type -P tar) && $(type -P pigz) ]];then 257 | install_dependencies 258 | fi 259 | 260 | 261 | # checking storage permission 262 | while ! [ -w /storage/emulated/0/ ];do 263 | printf "${EXC} Grant storage Permission${off}\n" 264 | termux-setup-storage 265 | sleep 5 266 | done 267 | 268 | 269 | # Driver Code 270 | while getopts ":hvbr" arg ; do 271 | case "${arg}" in 272 | h ) show_help ;; 273 | v ) show_version ;; 274 | b ) 275 | getopts_get_optional_argument $@ 276 | backup "${OPTARG}" 277 | ;; 278 | r ) 279 | getopts_get_optional_argument $@ 280 | restore "${OPTARG}" 281 | ;; 282 | \?) error "unknown option\n"; exit 1 ;; 283 | : ) error "no arguments"; show_help; exit 1 ;; 284 | esac 285 | done 286 | 287 | 288 | if [[ -z "$1" ]]; then 289 | error "no options provided" 290 | printf "try 'rewind -h' for help.\n" 291 | exit 1 292 | fi 293 | 294 | 295 | exit 0 296 | -------------------------------------------------------------------------------- /rewind.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laraib07/TermuxBackupTools/5576aeee95d215b86a0f062051d49598576ae330/rewind.png --------------------------------------------------------------------------------