The main thing you need to change is the first 3 variables, a username and password for account that has permissions to backup the databases, and the path (no trailing slash) as to where to put the backups. MAKE SURE THIS IS NOT INSIDE A DIRECTORY TO BE BACKED UP!
Also, the last line, 4320 is how old of files to delete. (note, this will use the timestamp of the last time ACCESSED, not created or modified). this is in hours (24 hours * 180 days for his use)
If you have more you want to backup, duplicate the first tar command you see, change home.tgz to be the final compressed filename and the other home is the directory (relative to the root) you are backing up. (the line after it does selected backups of what I wanted from his /etc directory)
The mysql backup portion will loop through all databases (other than the "test" one installed on default).
Assuming you ran this script as is, here is how it lays things out. All you need defined before first running it will be the /backup directory, it creates all else.
/backup/current -> this is the latest copy backed up. This should be copied and taken off-site often
/backup/current/data -> this is the compressed dump of the databases.
/backup/current/files -> this holds a compressed copy of the files you told it to back up
/backup/current/*.txt -> filelists of what was backed up, and mysql check report
/backup/dated -> each time the script runs, "current" gets moved into here, under yesterday's date
/backup/dated/2010-10 -> all the files for the month of October, 2010
/backup/dated/2010-10/2010-10-28 -> what was in current on October 28, 2010
Here is the script, enjoy. I welcome comments for improving it.
Code: Select all
#!/bin/bash
dbuser="backup" # Give only SHOW DATABASES, SELECT, LOCK TABLES, and RELOAD permissions on all DB's
dbpass="savemystuff"
buroot="/backup"
yfolder=`date -d yesterday +%Y-%m-%d`
ymfolder=`date -d yesterday +%Y-%m`
tfolder=`date +%Y-%m-%d`
tprefix=`date +%Y-%m-%d_`
mfolder=`date +%Y-%m`
mkdir $buroot/
mkdir $buroot/current
mkdir $buroot/dated
mkdir $buroot/dated/$ymfolder
mkdir $buroot/dated/$ymfolder/$yfolder
mv $buroot/current/* $buroot/dated/$ymfolder/$yfolder
mkdir $buroot/current/files
mkdir $buroot/current/data
cd /
tar -czvf `echo $buroot/current/files/$tprefix home.tgz | sed 's/ //'` home > $buroot/current/list_home.txt
tar -czvf `echo $buroot/current/files/$tprefix etc.tgz | sed 's/ //'` etc/hosts etc/httpd* etc/php* etc/yum* > $buroot/current/list_etc.txt
/usr/bin/mysqlcheck --user=$dbuser --password=$dbpass --fast --all-databases > $buroot/current/check_db.txt
cd $buroot
for i in `echo show databases|mysql -u $dbuser -p$dbpass`
do
if [ "$i" != "information_schema" -a "$i" != "Database" -a "$i" != "test" ]
then
mysqldump -u $dbuser -p$dbpass $i > "$tprefix$i"
tar -czf "$buroot/current/data/$tprefix$i.tgz" "$tprefix$i"
rm "$tprefix$i"
fi
done
/usr/sbin/tmpwatch 4320 $buroot/dated