≡ Menu

Shell script to backup directories & files from your home directory and email them as .tar.gz file

  1. #!/bin/bash
  2. # mybackup - Backup selected files & directories and email them as .tar.gz file to
  3. # your email account.
  4. # List of BACKUP files/dirs stored in file ~/.mybackup
  5. #
  6. # Usage : ./mybackup
  7. #
  8. # Notes : Very handy tool to take backup (nowdays we have 1 gig+ email a/c)
  9. #
  10. # Copyright (C) 2004 nixCraft project
  11. # Email/Contact : http://cyberciti.biz/fb/
  12. # Date : Aug-2004
  13. # Last updated : Aug-2005
  14. # -------------------------------------------------------------------------
  15. # This program is free software; you can redistribute it and/or
  16. # modify it under the terms of the GNU General Public License
  17. # as published by the Free Software Foundation; either version 2
  18. # of the License, or (at your option) any later version.
  19. # -------------------------------------------------------------------------
  20. # This script is part of nixCraft shell script collection (NSSC)
  21. # Visit http://bash.cyberciti.biz/ for more information.
  22. # -------------------------------------------------------------------------
  23.  
  24. FILE=~/.mybackup
  25. NOW=`date +"%d-%m-%Y"`
  26. OUT="`echo $USER.$HOSTNAME`.$NOW.tar.gz"
  27. TAR=`which tar`
  28.  
  29. # mail setup
  30. MTO="nixbackup@somedom.com"
  31. MSUB="Backup (`echo $USER @ $HOSTNAME`) as on `date`"
  32. MES=~/tmp/mybackup.txt
  33. MATT=~/tmp/$OUT
  34.  
  35. # make sure we put backup in our own tmp and not in /tmp
  36. [ ! -d ~/tmp ] && mkdir ~/tmp || :
  37. if [ -f $FILE ]; then
  38. IN="`cat $FILE | grep -E -v "^#"`"
  39. else
  40. echo "File $FILE does not exists"
  41. exit 3
  42. fi
  43.  
  44. if [ "$IN" == "" ]; then
  45. echo "$FILE is empty, please add list of files/directories to backup"
  46. echo "Use mybackupadd script"
  47. exit 2
  48. fi
  49.  
  50. $TAR -zcf ~/tmp/$OUT $IN >/dev/null
  51. # create message for mail
  52. echo "Backup successfully done. Please see attached file." > $MES
  53. echo "" >> $MES
  54. echo "Backup file: $OUT" >> $MES
  55. echo "" >> $MES
  56.  
  57. # bug fix, we can't send email with attachment if mutt is not installed
  58. which mutt > /dev/null
  59. if [ $? -eq 0 ]; then
  60. # now mail backup file with this attachment
  61. mutt -s "$MSUB" -a "$MATT" $MTO < $MES
  62. else
  63. echo "Command mutt not found, cannot send an email with attachment"
  64. fi
  65.  
  66. # clean up
  67. /bin/rm -f $MATT
  68. /bin/rm -f $MES

Comments on this entry are closed.

  • Boris C.

    Hi, i am not experienced in this, but i would like to know where to find help files on this script.

  • Marcelo Saied

    nice script

  • vamsi

    thanks :D
    its very useful ;)

  • abhishek

    what does this line will do. I too new for scripting :

    IN=”`cat $FILE | grep -E -v “^#”`”