Advertisement
rockdrilla

apt.cmd

Jan 19th, 2016
482
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.62 KB | None | 0 0
  1. #!/bin/bash
  2. this=$(basename "$0")
  3. there=$(dirname "$0")
  4.  
  5. idle='nice -n +40 chrt -i 0 ionice -c 3'
  6. dir_cache=/var/cache/apt/archives
  7. dir_lists=/var/lib/apt/lists
  8. f_storage=/opt/apt.lists.tar.xz
  9. compress='xz -C none -v'
  10. ratio=1
  11. N00B=1
  12.  
  13. __install()   { for i in ${shortcuts}; do ln -vfs ${N00B:+-i} "${this}" "${there}/$i"; done; }
  14. __uninstall() { for i in ${shortcuts}; do rm -vf  ${N00B:+-i} "${there}/$i"; done; }
  15.  
  16. shortcuts='a-u a-s a-r a-p'
  17.  
  18. __help() { cat >&2 <<-EOF
  19. usage:
  20.   ${this} {cmd} [args]
  21.     {a-u|u|update}
  22.     {a-s|s|save}
  23.     {a-r|r|restore}
  24.     {a-p|p|purge}
  25.  
  26.     {install}
  27.     {uninstall}
  28. EOF
  29. exit 1; }
  30.  
  31. __try_file()  { local d=$(dirname "$1"); test \( -f "$1" -a -w "$1" \) -o \( ! -e "$1" -a -w "$d" \) ; }
  32. __try_dir()   { test -d "$1" -a -w "$1"; }
  33. __test_file() { test -f "$1" -a -r "$1"; }
  34. __action() {
  35.     case "$1" in
  36.     a-u|u|update)
  37.         exec ${idle} apt-get update
  38.     ;;
  39.     a-s|s|save)
  40.         __try_file "${f_storage}" || exit 1
  41.         f_tmp=$(mktemp)
  42.         du -hd0 "${dir_lists}"
  43.         tar cPf - "${dir_lists}" | ${idle} ${compress} -${ratio} > "${f_tmp}"
  44.         cp -f "${f_tmp}" "${f_storage}"
  45.         rm "${f_tmp}"
  46.     ;;
  47.     a-r|r|restore)
  48.         __test_file "${f_storage}" || exit 1
  49.         ls -lhgG "${f_storage}"
  50.         ${idle} ${compress} -d < "${f_storage}" | tar xPf -
  51.     ;;
  52.     a-p|p|purge)
  53.         __try_dir "${dir_lists}" || exit 1
  54.         __try_dir "${dir_cache}" || exit 1
  55.         find "${dir_lists}" "${dir_cache}" -mindepth 1 -delete
  56.     ;;
  57.     install)      __install ;;
  58.     uninstall)    __uninstall ;;
  59.     *)            __help ;;
  60.     esac
  61. }
  62.  
  63. case "${this}" in
  64. apt.cmd)  [ $# -eq 0 ] && __help; __action "$@" ;;
  65. *)        __action "${this}" "$@" ;;
  66. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement