Advertisement
rc-chuah

TermuxBackup

Oct 5th, 2020 (edited)
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.05 KB | None | 0 0
  1. #!/bin/bash
  2. # Created And Scripted by Zongou & RC Chuah-(RaynerSec)
  3. # https://wiki.termux.com/wiki/Backing_up_Termux
  4.  
  5. # Define Presets
  6. backupDir="/storage/emulated/0/termux/backups"
  7. termuxRoot="/data/data/com.termux"
  8.  
  9. # Check Session Mode
  10. function session_mode() {
  11.     session="NULL"
  12.     if command -v termux-info > /dev/null 2>&1; then
  13.         echo "[NORMAL SESSION]"
  14.         session="NORMAL"
  15.     else
  16.         echo "[FAILSAFE SESSION]"
  17.         session="FAILSAFE"
  18.     fi
  19. }
  20.  
  21. # Check Storage Permission
  22. function checkStoragePermission() {
  23.     if [[ ! -w /storage/emulated/0 ]]; then
  24.         echo "Setting Up Termux Storage..."
  25.         termux-setup-storage
  26.         sleep 5
  27.         echo "Setting Up Termux Storage Completed..."
  28.         sleep 5
  29.     fi
  30. }
  31.  
  32. # Check If Backup Directory Exists
  33. function checkBackupDir() {
  34.     if [[ ! -d $backupDir ]]; then
  35.         echo "Backup Directory:"
  36.         echo $backupDir
  37.         echo "Backup Directory Does Not Exists"
  38.         # Ask If To Create Backup Directory
  39.         echo ""
  40.         echo -n "Create Backup Directory? [y/n] "
  41.         read answer
  42.         if [[ "$answer"x = "y"x ]]; then
  43.             echo "Creating Backup Directory..."
  44.             mkdir -p $backupDir
  45.             sleep 5
  46.             echo "Creating Backup Directory Completed..."
  47.             sleep 5
  48.             if [[ $? -ne 0 ]]; then
  49.                 echo "Create Backup Directory Failed!"
  50.                 exit 1
  51.             fi
  52.         else
  53.             echo "Exiting..." && exit 1
  54.         fi
  55.     fi
  56. }
  57.  
  58. # Backup Function
  59. function backup() {
  60.     # Check If In NORMAL SESSION
  61.     # Backup Will Fail When Other Linux System Is Installed, Force Running In NORMAL SESSION
  62.     if [[ "$session"x = "NORMAL"x ]]; then
  63.         echo "Type The File Name For Backup"
  64.         echo "If Empty Will Use termux.tar.gz"
  65.         echo ""
  66.         echo -n "Enter File Name For Backup: "
  67.         read name
  68.         if [[ -z $name ]]; then
  69.             name="termux.tar.gz"
  70.         fi
  71.         echo "Will Create "$name
  72.         # cleanHistory
  73.         echo "Backing System Up..."
  74.         sleep 5
  75.         cd $termuxRoot/files
  76.         tar -czvf $backupDir/$name ./home ./usr
  77.         if [[ $? -ne 0 ]]; then
  78.             echo "Make Sure Running In Termux Default Environment."
  79.             exit 1
  80.         fi
  81.         echo -e "\033[0;32m Backing Up Finished! \033[0m"
  82.     else
  83.         echo "Backup Is Not Supported In [FAILSAFE SESSION], Exiting..."
  84.     fi
  85. }
  86.  
  87. # Restore Function
  88. function restore() {
  89.     # Check If Backup Directory Is Empty
  90.     if [[ $(ls -l $backupDir | grep "^-" | wc -l) -eq 0 ]]; then
  91.         # Empty
  92.         echo "Backup Directory Is Empty! Exiting..." && exit 1
  93.     else
  94.         echo "Note: Make Sure No Background Program Running."
  95.         echo "Listing Backup File..."
  96.         echo ""
  97.         ls $backupDir
  98.     fi
  99.     echo ""
  100.     echo -n "Choose A Backup File: "
  101.     read file
  102.     while [[ ! -f $backupDir/$file ]]
  103.     do
  104.         clear
  105.         echo "No Match, Try Again!"
  106.         sleep 5
  107.         clear
  108.         echo "Note: Make Sure No Background Program Running."
  109.         echo "Listing Backup File..."
  110.         echo ""
  111.         ls $backupDir
  112.         echo ""
  113.         echo -n "Choose A Backup File: "
  114.         read file
  115.     done
  116.     echo "Start Restoring!"
  117.     sleep 5
  118.     # Use Seperated Steps Is More Compatible For Lower Version Of Toolbox
  119.     if [[ "$session"x = "FAILSAFE"x ]]; then
  120.         rm -rf $termuxRoot/files/*
  121.         gzip -d -c $backupDir/$file | tar -xvf - -C $termuxRoot/files
  122.     fi
  123.     if [[ "$session"x = "NORMAL"x ]]; then
  124.     #   cleanAllButKeepCoreFunctions
  125.         tar -xzvf $backupDir/$file -C $termuxRoot/files --recursive-unlink --preserve-permissions
  126.     fi
  127.     echo -e "\033[0;32m Restoring Finished! \033[0m"
  128. }
  129.  
  130. # Clean History Function
  131. function cleanHistory() {
  132.     echo "Start Cleaning"
  133.     termuxBashHistory=$termuxRoot"/files/home/.bash_history"
  134.     if [[ -f $termuxBashHistory ]]; then
  135.         echo "Clean Termux Bash History"
  136.         rm $termuxBashHistory
  137.     fi
  138.     debianBashHistory=$termuxRoot"/files/home/debian-fs/root/.bash_history"
  139.     if [[ -f $debianBashHistory ]]; then
  140.         echo "Clean Debian Bash History"
  141.         rm $debianBashHistory
  142.     fi
  143. }
  144.  
  145. # Clean All But Keep Core Functions, Get 'rm' Alike Effect
  146. function cleanAllButKeepCoreFunctions() {
  147.     # Clean Files Directory
  148.     cd $termuxRoot/files
  149.     find * -maxdepth 0 | grep -vw 'usr' | xargs rm -rf
  150.     # Clean $PREFIX Directory
  151.     cd $termuxRoot/files/usr
  152.     find * -maxdepth 0 | grep -vw '\(bin\|lib\)' | xargs rm -rf
  153.     # Clean Bin Directory
  154.     cd $termuxRoot/files/usr/bin
  155.     find * -maxdepth 0 | grep -vw '\(coreutils\|rm\|xargs\|find\|grep\|tar\|gzip\)' | xargs rm -rf
  156.     # Clean Lib Directory
  157.     cd $termuxRoot/files/usr/lib
  158.     find * -maxdepth 0 | grep -vw '\(libandroid-glob.so\|libtermux-exec.so\|libiconv.so\|libandroid-support.so\|libgmp.so\)' | xargs rm -rf
  159.     # Clean None Exact Utils, Aggressively
  160.     cd $termuxRoot/files/usr/bin
  161.     rm coreutils grep xargs find rm ../lib/libgmp.so ../lib/libandroid-support.so
  162.  
  163.     # Dependencies:
  164.     # ls libandroid-support.so libgmp.so
  165.     # rm libgmp.so
  166.     # tar libandroid-glob libtermux-exec.so libiconv.so
  167. }
  168.  
  169. # Press Enter To Continue Function
  170. function press_enter() {
  171.     echo ""
  172.     echo -n "Press Enter To Continue..."
  173.     read
  174.     clear
  175. }
  176.  
  177. # Incorrect Selection Function
  178. function incorrect_selection() {
  179.     echo "Incorrect Selection! Try Again."
  180. }
  181.  
  182. # Usage Menu
  183. until [[ "$option" = "3" ]]; do
  184.      clear
  185.      checkStoragePermission
  186.      clear
  187.      checkBackupDir
  188.      clear
  189.      session_mode
  190.      echo "This Script Will Backup/Restore Termux"
  191.      echo "Choose To Backup, Restore Or Exit Menu"
  192.      echo "1 To Backup, 2 To Restore, 3 To Exit Menu"
  193.      echo ""
  194.      echo -n "Enter Option: "
  195.      read option
  196.      case $option in
  197.           1 ) clear ; backup ; press_enter ;;
  198.           2 ) clear ; restore ; press_enter ;;
  199.           3 ) clear ; exit ;;
  200.           * ) clear ; incorrect_selection ; press_enter ;;
  201.      esac
  202. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement