Advertisement
gheja

dedup_run.sh

May 15th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.77 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. tmp1=`tempfile`
  4. tmp2=`tempfile`
  5. dir=`pwd`
  6.  
  7. if [ "$1" != "--run-here" ]; then
  8.         echo "$0 --run-here"
  9.         exit 1
  10. fi
  11.  
  12. cd /opt/dedup
  13. find -type f | grep -vE '^\./(DEBIAN|dedup)' | xargs -d '\n' sha256sum > $tmp1
  14.  
  15. cd "$dir"
  16. find -type f | grep -vE '^\./(DEBIAN|dedup)' | xargs -d '\n' sha256sum > $tmp2
  17.  
  18. cat >dedup_install.sh <<'EOF'
  19. #!/bin/bash
  20.  
  21. dedup()
  22. {
  23.         local file_source="$1"
  24.         local file_target="$2"
  25.         local uid="$3"
  26.         local gid="$4"
  27.         local mode="$5"
  28.         local mtime="$6"
  29.  
  30.         cp "$file_source" "$file_target"
  31.         chown $uid:$gid "$file_target"
  32.         chmod $mode "$file_target"
  33.         touch -d @$mtime "$file_target"
  34. }
  35.  
  36. cd /
  37.  
  38. echo -n "dedup: installing files... "
  39. EOF
  40.  
  41. cat >dedup_remove.sh <<'EOF'
  42. #!/bin/bash
  43.  
  44. echo -n "dedup: removing files... "
  45. EOF
  46.  
  47. cat $tmp2 | while read line; do
  48.         checksum=${line:0:64}
  49.         filename=${line:68}
  50.  
  51.         if [ -L "${dir}/${filename}" ]; then
  52.                 continue
  53.         fi
  54.  
  55.         line2=`cat $tmp1 | grep -E "^${checksum} " | head -n 1`
  56.  
  57.         if [ "$line2" == "" ]; then
  58.                 continue
  59.         fi
  60.  
  61.         filename2=${line2:68}
  62.  
  63.         uid=`stat --format '%u' "${dir}/${filename}"`
  64.         gid=`stat --format '%g' "${dir}/${filename}"`
  65.         mode=`stat --format '%a' "${dir}/${filename}"`
  66.         mtime=`stat --format '%Y' "${dir}/${filename}"`
  67.  
  68.         echo "dedup '/opt/dedup/${filename2}' '${filename}' $uid $gid $mode $mtime" >> dedup_install.sh
  69.         echo "rm '${filename}'" >> dedup_remove.sh
  70.  
  71.         rm "${dir}/${filename}"
  72. done
  73.  
  74. echo "echo \"done.\"" >> dedup_install.sh
  75. echo "echo \"done.\"" >> dedup_remove.sh
  76.  
  77. chmod 700 dedup_remove.sh
  78. chmod 700 dedup_install.sh
  79.  
  80. rm $tmp1
  81. rm $tmp2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement