Hi! I am trying to install a bash script that will email me a backup of my mySQL databases. Can someone please tell me (I’m quite new to this) how to setup this script? Thanks!
#!/bin/bash
This script runs as a nightly cron job to back a MySQL database to a Gmail account
Prints date of the format MM/DD/YY TODAY1="date +%D” # Prints date of the format MMDDYYYY TODAY2=“date +%m%d%Y” # The email address to which we’re sending backups ADDRESS="someone@gmail.com” # Local directory where we’ll be doing work and keeping copies of all archived files BACKUPDIR=”/root/backup” # MySQL username and password MYSQLUSER="root” MYSQLPASS=“f00b@R” # MySQL database to backup (or –all-databases to backup everything) DATABASE=”–all-databases” $ Name of backup file (used for plaintext and gzipped files) FILENAME="mysql_backup”
Change to the backup directory (so absolute directories aren’t included in the archive) cd $BACKUPDIR
Dump the MySQL databases to a plaintext file mysqldump -u $MYSQLUSER -p $MYSQLPASS $DATABASE > $FILENAME_$TODAY2
Package (tar) and compress (gzip) the plaintext MySQL dump # Append the date (MMDDYYYY) to the filename tar czf $FILENAME_$TODAY2.tar.gz $FILENAME_$TODAY2
Mail the resulting archive to our Gmail account # Body of email is the output from the date command at the exact time the cron job runs # Subject is “$FILENAME MM/DD/YY” date | mutt -s “$FILENAME ($TODAY1)” -a $FILENAME_$TODAY2.tar.gz $ADDRESS
If you are using bash (check with ‘echo $SHELL’ from the prompt) as your shell then set the first line to
#!/usr/bin/bash
with no spaces.
What sometimes happens with these scripts when ftp’d is that the format is incorrect.
To correct it use SSH to connect to your shell and open the script in pico with
pico -w script
Go to the very end and add a blank line and save it.
Once you chmod it 755 you can run it as normal with ./script or bash script