backup-www-script 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. FILE=/root/.mybackup
  24. NOW=`date +"%d-%m-%Y"`
  25. OUT="`echo $USER.$HOSTNAME`.$NOW.tar.lzma"
  26. TAR=`which tar`
  27. XZ=`which xz`
  28. # mail setup
  29. OURREMOTEUSER="backuper"
  30. OURREMOTEHOST="backup.usic.lan"
  31. OURREMOTEDIR="~/backups/www"
  32. #OURREMOTEDIR="172.16.200.2:/home/user/backups"
  33. MSUB="Backup (`echo $USER @ $HOSTNAME`) as on `date`"
  34. MES=/var/log/wwwbackup.txt
  35. MATT=/tmp/www/$OUT
  36. # make sure we put backup in our own tmp and not in /tmp
  37. [ ! -d /tmp/www ] && mkdir /tmp/www || :
  38. if [ -f $FILE ]; then
  39. IN="`cat $FILE | grep -E -v "^#"`"
  40. else
  41. echo "File $FILE does not exists"
  42. exit 3
  43. fi
  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. $TAR -c $IN | $XZ --format=lzma -9 >/tmp/www/$OUT 2>/dev/null
  50. # create message for mail
  51. echo "$NOW. Backup successfully done." >> $MES
  52. echo -e "Backup file: $OUT\n" >> $MES
  53. ssh $OURREMOTEUSER@$OURREMOTEHOST "mkdir $OURREMOTEDIR/$NOW" > /dev/null 2>&1
  54. scp $MATT $OURREMOTEUSER@$OURREMOTEHOST:$OURREMOTEDIR/$NOW/ >/dev/null 2>&1
  55. if [ $? == 0 ]; then
  56. echo "Backup successfully copied to $OURREMOTEDIR" >> $MES
  57. # cleanup archive
  58. rm -f $MATT
  59. else
  60. echo "Failed to copy backup to $OURREMOTEDIR" >> $MES
  61. fi
  62. # bug fix, we can't send email with attachment if mutt is not installed
  63. #which mutt > /dev/null
  64. #if [ $? -eq 0 ]; then
  65. # now mail backup file with this attachment
  66. # mutt -s "$MSUB" -a "$MATT" $MTO < $MES
  67. #else
  68. # echo "Command mutt not found, cannot send an email with attachment"
  69. #fi
  70. # clean up
  71. #rm -f $MATT
  72. #rm -f $MES