v-copy-fs-directory 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/bash
  2. # info: copy directory
  3. # options: USER SRC_DIRECTORY DST_DIRECTORY
  4. #
  5. # The function copies directory on the file system
  6. user=$1
  7. src_dir=$2
  8. dst_dir=$3
  9. # Checking arguments
  10. if [ -z "$dst_dir" ]; then
  11. echo "Usage: USER SRC_DIRECTORY DST_DIRECTORY"
  12. exit 1
  13. fi
  14. # Checking vesta user
  15. if [ ! -e "$VESTA/data/users/$user" ]; then
  16. echo "Error: vesta user $user doesn't exist"
  17. exit 3
  18. fi
  19. # Checking user homedir
  20. homedir=$(grep "^$user:" /etc/passwd | cut -f 6 -d :)
  21. if [ -z $homedir ]; then
  22. echo "Error: user home directory doesn't exist"
  23. exit 12
  24. fi
  25. # Checking source dir
  26. if [ ! -e "$src_dir" ]; then
  27. echo "Error: source directory $src_dir doesn't exist"
  28. exit 3
  29. fi
  30. # Checking source path
  31. rpath=$(readlink -f "$src_dir")
  32. if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
  33. echo "Error: invalid source path $src_dir"
  34. exit 2
  35. fi
  36. # Checking destination path
  37. rpath=$(readlink -f "$dst_dir")
  38. if [ -z "$(echo $rpath |egrep "^/tmp|^$homedir")" ]; then
  39. echo "Error: invalid destination path $dst_dir"
  40. exit 2
  41. fi
  42. # Copying directory
  43. sudo -u $user cp -r "$src_dir" "$dst_dir" >/dev/null 2>&1
  44. if [ $? -ne 0 ]; then
  45. echo "Error: directory $src_dir was not copied"
  46. exit 3
  47. fi
  48. # Exiting
  49. exit