From ohcanada, 2 Weeks ago, written in Bash.
  1. #!/bin/bash
  2.  
  3. # CLONE - clone a debian install
  4.  
  5. ######################################################################
  6. # At the end, all packages will be identical to the list in :
  7. #    /usr/local/backup/ALL (if it exists)
  8. #
  9. # All missing Extras packages will be installed from the list in :
  10. #    /usr/local/backup/EXTRAS (if it exists)
  11. #
  12. # /usr/local/backup/extras *must* contain EXTRAS packages which
  13. # have unique names up to the first _ (underscore), matching the
  14. # package names in EXTRAS package list
  15. #
  16. # All required repos *must* be active in :
  17. #    /etc/apt/sources.list (or wherever)
  18. #
  19. # and all required repo GPG/PGP keys *must* be imported
  20. #
  21. # apt transactions logged to ~/APTLOG
  22. ######################################################################
  23.  
  24. myecho()
  25. {
  26.         echo -e "\e[36mCLONE\e[m $*"
  27. }
  28.  
  29. backup_dir=/usr/local/backup
  30. extradir=${backup_dir}/extras
  31.  
  32. # sanity checks
  33.  
  34. if [[ ! $(type -t dpkg) ]]; then
  35.   myecho dpkg program not found
  36.   exit 1
  37. fi
  38.  
  39. if [[ ! $(type -t awk) ]]; then
  40.   myecho awk program not found
  41.   exit 1
  42. fi
  43.  
  44. if [[ ! $(type -t comm) ]]; then
  45.   myecho comm program not found
  46.   exit 1
  47. fi
  48.  
  49. if [[ ! $(type -t apt-get) ]]; then
  50.   myecho apt-get program not found
  51.   exit 1
  52. fi
  53.  
  54. ##################################################################
  55. do_extras_exit()
  56. {
  57.         if [[ "$extras" == "yes" ]]; then
  58.                 myecho EXTRAS package list found
  59.  
  60.                 # no need to get current again
  61.                 extra_list=$(LC_ALL=C comm -13 <(echo "$current") ${backup_dir}/EXTRAS)
  62.  
  63.                 if [[ "$extra_list" ]]; then
  64.                         myecho Installing $(echo "$extra_list" | wc -l) 'extra package(s) :'
  65.                         echo
  66.                         echo $extra_list
  67.                         echo
  68.  
  69.                         for p in $extra_list ; do
  70.                                 package_deb=$(echo ${extradir}/${p}_*)
  71.  
  72.                                 if [[ "${package_deb: -1}" = "*" ]]; then
  73.                                         myecho package $p not found
  74.  
  75.                                 else    if [[ "$package_deb" =~ " " ]]; then
  76.                                         myecho more than one debfile matches package $p
  77.  
  78.                                 else
  79.                                         dpkg --force-all -i $package_deb > /dev/null 2>&1
  80.                                 fi
  81.                                 fi
  82.                         done
  83.  
  84.                         myecho Extras cloning complete
  85.                 else
  86.                         myecho Extras already installed
  87.                 fi
  88.         else
  89.                 myecho EXTRAS package list not found
  90.         fi
  91.         exit
  92. }
  93.  
  94. ##################################################################
  95.  
  96. # get current (clean install ?) package list
  97. current=$(LC_ALL=C dpkg --get-selections | awk '{print $1}' | cut -f1 -d:)
  98.  
  99. if [[ -f ${backup_dir}/EXTRAS ]]; then
  100.         extras=yes
  101. else
  102.         extras=no
  103. fi
  104.  
  105. if [[ -f ${backup_dir}/ALL ]]; then
  106.         myecho ALL package list found
  107. else
  108.         myecho ALL package list not found
  109.         do_extras_exit
  110. fi
  111.  
  112. # get allbase (ALL except EXTRAS) package list
  113. if [[ "$extras" == "yes" ]]; then
  114.         allbase=$(LC_ALL=C comm -13 ${backup_dir}/EXTRAS ${backup_dir}/ALL)
  115. else
  116.         allbase=$(cat ${backup_dir}/ALL)
  117. fi
  118.  
  119. # get currentbase (current except EXTRAS) package list
  120. if [[ "$extras" == "yes" ]]; then
  121.         currentbase=$(LC_ALL=C comm -13 ${backup_dir}/EXTRAS <(echo "$current"))
  122. else
  123.         currentbase=$current
  124. fi
  125.  
  126. # check to see if we are already done
  127. if [[ ! $(LC_ALL=C diff <(echo "$currentbase") <(echo "$allbase")) ]]; then
  128.         myecho Base cloning already done
  129.         do_extras_exit
  130. fi
  131.  
  132. # remove old APTLOG
  133. rm -f ~/APTLOG
  134.  
  135. # remove unwanted (in currentbase but not allbase) packages
  136. unwanted=$(LC_ALL=C comm -13 <(echo "$allbase") <(echo "$currentbase"))
  137.  
  138. # don't try with empty list
  139. if [[ "$unwanted" ]]; then
  140.         echo 'Purging' $(echo "$unwanted" | wc -l) 'unwanted package(s) :'
  141.         echo
  142.         echo $unwanted
  143.         echo
  144. #       dpkg -P --force-all $unwanted
  145. #       dpkg -P --force-all $unwanted >> ~/APTLOG 2>&1
  146. else
  147.         myecho No unwanted packages
  148. fi
  149.  
  150. # update & upgrade packages (only if base clone isn't done yet)
  151. myecho Updating available package list
  152. #apt-get -y --force-yes update
  153. #apt-get -y --force-yes update >> ~/APTLOG 2>&1
  154.  
  155. myecho Upgrading packages
  156. #apt-get -y --force-yes upgrade
  157. #apt-get -y --force-yes upgrade >> ~/APTLOG 2>&1
  158.  
  159. # add missing (in allbase but not currentbase) packages
  160. # no need to get currentbase again
  161. missing=$(LC_ALL=C comm -13 <(echo "$currentbase") <(echo "$allbase"))
  162.  
  163. # don't try with empty list
  164. if [[ "$missing" ]]; then
  165.         myecho Installing $(echo "$missing" | wc -l) 'missing package(s) :'
  166.         echo
  167.         echo $missing
  168.         echo
  169. #       apt-get -y --force-yes install $missing
  170. #       apt-get -y --force-yes install $missing >> ~/APTLOG 2>&1
  171. else
  172.         myecho No missing packages
  173. fi
  174.  
  175. # clear cache
  176. myecho Clearing apt cache
  177. #apt-get clean >> ~/APTLOG 2>&1
  178. #apt-get autoclean >> ~/APTLOG 2>&1
  179.  
  180. myecho Base cloning complete
  181. do_extras_exit
captcha