Guest User

Untitled

a guest
Feb 21st, 2018
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Backing up the OVH server, checking the archive and sending it through FTP
  4. #
  5.  
  6. BACKUPHOME=/home/backups
  7. FTPUSER=ftpbackupser
  8. FTPPASS=S0m3Th1nGH4rD
  9. FTPSERVER=ftp.server.com
  10. SERVER=youservername
  11. MYSQLUSER=backup
  12. MYSQLPASS=S0m4Th1nGTr1cKy
  13. PKGLIST=$BACKUPHOME/installed-packages
  14. DBDUMP=$BACKUPHOME/mysqldumpall.sql
  15. ARCHIVE=$BACKUPHOME/$SERVER-backup-`date +%d-%m-%Y`.tar.bz2
  16. ADMINEMAIL=your.email@domain.com
  17.  
  18. # create BACKUPHOME if not exists
  19. mkdir -p $BACKUPHOME
  20. # get installed Debian packages
  21. dpkg --get-selections| awk -F' ' '{print $1}' > $PKGLIST
  22. RETVAL=$?
  23. if [ $RETVAL != 0 ];then
  24. echo "Issue while using dpkg --get-selections of $SERVER on $FTPSERVER" | mail -s "Issue while uploading $ARCHIVE" $ADMINEMAIL
  25. fi
  26. # get mysql database
  27. mysqldump -u$MYSQLUSER -p$MYSQLPASS --all-databases > $DBDUMP
  28. RETVAL=$?
  29. if [ $RETVAL != 0 ];then
  30. echo "Issue while performing mysqldump of $SERVER on $FTPSERVER" | mail -s "Issue while uploading $ARCHIVE" $ADMINEMAIL
  31. fi
  32. # get directories and files
  33. tar --preserve-permissions --preserve-order -j -c -f $ARCHIVE /etc /var /home /opt /usr/local/bin $DBDUMP \
  34. --exclude=/var/lib/mysql/data \
  35. --exclude=$BACKUPHOME/$SERVER-backup* \
  36. --exclude=/var/cache/apt/archives > /dev/null 2>&1
  37. RETVAL=$?
  38. if [ $RETVAL != 0 ];then
  39. echo "Issue while making the archive from $SERVER to $FTPSERVER" | mail -s "Issue while making $ARCHIVE" $ADMINEMAIL
  40. fi
  41. # remove one-month-old archive on the FTP
  42. lftp -e "rm -f $SERVER-backup-`date -d "-10 day" +%d-%m-%Y`.tar.bz2;exit" -u $FTPUSER,$FTPPASS $FTPSERVER > /dev/null 2>&1
  43. if [ $RETVAL != 0 ];then
  44. echo "Issue while removing the old archive of $SERVER on $FTPSERVER" | mail -s "Issue while uploading $ARCHIVE" $ADMINEMAIL
  45. fi
  46. # send the archive to ftp account
  47. lftp -e "mput $ARCHIVE;exit" -u $FTPUSER,$FTPPASS $FTPSERVER > /dev/null 2>&1
  48. RETVAL=$?
  49. if [ $RETVAL != 0 ];then
  50. echo "Issue while uploading the archive from $SERVER to $FTPSERVER" | mail -s "Issue while uploading $ARCHIVE" $ADMINEMAIL
  51. fi
  52. # remove the local backups
  53. rm -f $PKGLIST $DBDUMP $ARCHIVE
  54. exit 0
Add Comment
Please, Sign In to add comment