Von Lars, 9 Monate vorher, geschrieben in Bash.
Einbetten
  1. #!/bin/bash
  2. # Shell script to backup MySQL database
  3.  
  4. # Set these variables
  5. MyUSER=""       # DB_USERNAME
  6. MyPASS=""       # DB_PASSWORD
  7. MyHOST=""       # DB_HOSTNAME
  8.  
  9. # Backup Dest directory
  10. DEST="" # /home/username/backups/DB
  11.  
  12. # Email for notifications
  13. EMAIL=""
  14.  
  15. # How many days old files must be to be removed
  16. DAYS=3
  17.  
  18. # Linux bin paths
  19. MYSQL="$(which mysql)"
  20. MYSQLDUMP="$(which mysqldump)"
  21. GZIP="$(which gzip)"
  22.  
  23. # Get date in dd-mm-yyyy format
  24. NOW="$(date +"%d-%m-%Y_%s")"
  25.  
  26. # Create Backup sub-directories
  27. MBD="$DEST/$NOW/mysql"
  28. install -d $MBD
  29.  
  30. # DB skip list
  31. SKIP="information_schema
  32. another_one_db"
  33.  
  34. # Get all databases
  35. DBS="$($MYSQL -h $MyHOST -u $MyUSER -p$MyPASS -Bse 'show databases')"
  36.  
  37. # Archive database dumps
  38. for db in $DBS
  39. do
  40.     skipdb=-1
  41.     if [ "$SKIP" != "" ];
  42.     then
  43.                 for i in $SKIP
  44.                 do
  45.                         [ "$db" == "$i" ] && skipdb=1 || :
  46.                 done
  47.     fi
  48.  
  49.     if [ "$skipdb" == "-1" ] ; then
  50.         FILE="$MBD/$db.sql"
  51.         $MYSQLDUMP -h $MyHOST -u $MyUSER -p$MyPASS $db > $FILE
  52.     fi
  53. done
  54.  
  55. # Archive the directory, send mail and cleanup
  56. cd $DEST
  57. tar -cf $NOW.tar $NOW
  58. $GZIP -9 $NOW.tar
  59.  
  60. echo "MySQL backup is completed! Backup name is $NOW.tar.gz" | mail -s "MySQL backup" $EMAIL
  61. rm -rf $NOW
  62.  
  63. # Remove old files
  64. find $DEST -mtime +$DAYS -exec rm -f {} \;
captcha