Nightly Backup script

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

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`
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`
  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"
    rm "$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)

ZeroOne's picture

They have: 21 posts

Joined: Nov 2010

Most of hosting providers do daily back-ups

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

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.

If I remember right, you put curly braces around the variable name: ${tprefix}home.

Kudos to you for writing that in Bash. I would have given up on the Bourne again shell after the for loop Smiling

sanseo's picture

They have: 18 posts

Joined: Oct 2010

very nice scripts.

very interesting for beginning hosting company who have limited man powers this will help them to take backup automatically and they need to just monitor.

wow awesome task.

thanks to share.

They have: 19 posts

Joined: Aug 2011


Off course, This script does really a great work, full nightly back up for his CentOS system. I do go for the same..
Good that you have shared very useful information here. I am just surprised to read it!

Web Hosting Coupons & Discounts of 2014 -

They have: 3 posts

Joined: Oct 2011

A quick notice - use "mkdir -p" to create all parent directories in a single command.
I'll examine your script in details later.

{links removed}

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.