Nightly Backup script
I had to set this up for a friend, and thought I would share it here. Note, his system has dual 250 gig drives, one dedicated for his /backup directory, and his nightly backup is just under 80 megs, so he opted to save copies for 6 months. This script does a full nightly back up for his CentOS system.
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.
dbuser="backup" # Give only SHOW DATABASES, SELECT, LOCK TABLES, and RELOAD permissions on all DB's
yfolder=`date -d yesterday +%Y-%m-%d`
ymfolder=`date -d yesterday +%Y-%m`
mv $buroot/current/* $buroot/dated/$ymfolder/$yfolder
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
for i in `echo show databases|mysql -u $dbuser -p$dbpass`
if [ "$i" != "information_schema" -a "$i" != "Database" -a "$i" != "test" ]
mysqldump -u $dbuser -p$dbpass $i > "$tprefix$i"
tar -czf "$buroot/current/data/$tprefix$i.tgz" "$tprefix$i"
/usr/sbin/tmpwatch 4320 $buroot/dated
PS. I know the line in the tar files is messy with the echo piped to sed, but I couldn't figure out how to butt text up against a variable. I welcome how the heck to do that (with leaving the _ in the variable)