compact_backups.sh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #! /bin/sh
  2. FTP_SERVER=backup.serverkompetenz.de
  3. BAK_PATH=backup/full
  4. BAK=$1
  5. WGET=$(whereis wget)
  6. if [ $? != 0 ] ; then
  7. echo "Unable to find wget executeable." >&2
  8. exit 4
  9. fi
  10. CURL=$(whereis curl)
  11. if [ $? != 0 ] ; then
  12. echo "Unable to find curl executeable." >&2
  13. exit 4
  14. fi
  15. NCFTPPUT=$(whereis ncftpput)
  16. if [ $? != 0 ] ; then
  17. echo "Unable to find ncftpput executeable." >&2
  18. exit 4
  19. fi
  20. if [ -z ${BAK} ]; then
  21. echo "USAGE: `basename $0` BAK_DIR" >&2
  22. exit 1
  23. fi
  24. # Only root can do this...
  25. if [ "`id -u`" -ne 0 ]; then
  26. echo "`basename $0`: Need to be root to backup data." >&2
  27. exit 2
  28. fi
  29. if [ ! -e /root/.ncftplogin ]; then
  30. echo "`basename $0`: missing login credentials in /root.ncftplogin" >&2
  31. exit 3
  32. fi
  33. set -A FILES `${CURL} -s -n -l "ftp://${FTP_SERVER}/${BAK_PATH}/${BAK}/" \
  34. | grep -v '^\.' | grep -v '\.gz$'`
  35. let RESULT=$?
  36. if [ ${RESULT} != 0 ]; then
  37. echo "curl returned ${RESULT}"
  38. exit 5
  39. fi
  40. let IDX=0
  41. while [ ${IDX} -lt ${#FILES[@]} ]; do
  42. FNAME=${FILES[IDX]}
  43. echo "Compressing ${FNAME}"
  44. ${WGET} -q -O - "ftp://${FTP_SERVER}/${BAK_PATH}/${BAK}/${FNAME}" \
  45. | gzip -9 \
  46. | ${NCFTPPUT} -f /root/.ncftplogin -c ${FTP_SERVER} "${BAK_PATH}/${BAK}/${FNAME}.gz"
  47. let RESULT=$?
  48. if [ ${RESULT} != 0 ]; then
  49. echo "Unable to compact ${BAK_PATH}/${BAK}/${FNAME}"
  50. else
  51. /usr/bin/ftp <<-EOF
  52. open ${FTP_SERVER}
  53. epsv
  54. binary
  55. verbose
  56. prompt
  57. cd "${BAK_PATH}/${BAK}"
  58. del "${FNAME}"
  59. EOF
  60. fi
  61. IDX=$((IDX + 1))
  62. done