bash scripting help please

]SK[

One of Freddy's beloved
Joined
Dec 22, 2003
Messages
302
I use this script to backup my mysql databases, the filename changes per day so I know which backup is from which day. The only thing is these after 30 days I have 60 backups in one folder. I cant think of a way to auto delete files that are 5+days old. Any ideas here?

Code:
#!/bin/sh

# List all of the MySQL databases that you want to backup in here,
# each seperated by a space
databases="mysql vbulletin"

# Directory where you want the backup files to be placed
backupdir=/root/backups/mysql

# MySQL dump command, use the full path name here
mysqldumpcmd=mysqldump

# MySQL Username and password
userpassword=" --user=root --password=xxxxxxxx"

# MySQL dump options
dumpoptions=" --quick --add-drop-table --add-locks --extended-insert --lock-tables"

# Unix Commands
gzip=/bin/gzip
uuencode=/usr/bin/uuencode
mail=/bin/mail

# Send Backup?  Would you like the backup emailed to you?
# Set to "y" if you do
sendbackup="n"
subject="Your MySQL Backup"
mailto="root@localhost"

# Create our backup directory if not already there
mkdir -p ${backupdir}
if [ ! -d ${backupdir} ]
then
   echo "Not a directory: ${backupdir}"
   exit 1
fi

# Get currentdate
today=`date +%d-%m-%Y`

# Dump all of our databases
echo "Dumping MySQL Databases"
for database in $databases
do
   $mysqldumpcmd $userpassword $dumpoptions $database > ${backupdir}/${database}-$today.sql
done
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,925
you could add a find command like so:

Code:
find ${backupdir} -mtime +7 -exec rm {} \;

this will find all files in ${backupdir} with a creation stamp (mtime) of 7 days or older and delete them. naturally 7 can become whatever number that goes with your backup stratagy. try man find for more details.
 

]SK[

One of Freddy's beloved
Joined
Dec 22, 2003
Messages
302
Works perfectly. Cheers for that.
 

Users who are viewing this thread

Top Bottom