Guest User

Untitled

a guest
Dec 30th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #!/bin/sh
  2. BACKUP=/var/www/html/mysql_backup/backups
  3. cd $BACKUP
  4. sudo mkdir `date '+%d-%m-%Y'`
  5. NOW=$(date +"%d-%m-%Y")
  6.  
  7. MUSER="user"
  8. MPASS="pass"
  9. MHOST="host"
  10. MYSQL="$(which mysql)"
  11. MYSQLDUMP="$(which mysqldump)"
  12. GZIP="$(which gzip)"
  13. MAIL="info@example.com"
  14. STATUSFILE="/tmp/statusfile.$NOW"
  15.  
  16. # It succeeds but stderr will get:
  17. # Warning: Using a password on the command line interface can be insecure.
  18. # You can fix this with the below hack:
  19. credentialsFile=/mysql-credentials.cnf
  20. echo "[client]" > $credentialsFile
  21. echo "user=$MUSER" >> $credentialsFile
  22. echo "password=$MPASS" >> $credentialsFile
  23. echo "host=$MHOST" >> $credentialsFile
  24.  
  25. echo "Backup report from $NOW" > $STATUSFILE
  26. DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
  27. for db in $DBS
  28. do
  29. FILE=$BACKUP/$NOW/mysql-$db.$NOW-$(date +"%T").sql.gz
  30. $MYSQLDUMP --defaults-extra-file=$credentialsFile --lock-all-tables $db | $GZIP -9 > $FILE
  31. if [ "$?" -eq "0" ]; then
  32. echo "$db backup is OK" >> $STATUSFILE
  33. else
  34. echo "##### WARNING: ##### $db backup failed" >> $STATUSFILE
  35. fi
  36. done
  37. # delete backups older than 1 week
  38. find /path/to/base/dir/* -type d -ctime +10 -exec rm -rf {} \;
  39.  
  40. mail -s "Backup report for $NOW" -- $MAIL < $STATUSFILE
  41. rm $STATUSFILE
Add Comment
Please, Sign In to add comment