Jump to content

*nix Script needed for backup job


Recommended Posts

Posted

Hi All,

 

I wonder if anybody has a script or scripts that will do the following:

 

1 - Full dump of mysql db to \usr\backups\mysql\mysqldump-dd-mm-yy.sql

2 - copy var\www\*.* to usr\backups\www

3 - copy var\moodledata to usr\backups\moodledata

4 - zip or tar the backups folder (usr\backups) and give a name of backup-dd-mm-yyyy.zip

5 - FTP to another NAS server internal (TeraStation)

6 - Once successfull rename rename the backups folder and create a new one.

 

This is the only way I can think of having this server fully covered in the event of a disaster.

 

Any ideas would be greatly apreciated ;-)

Posted

#!/bin/sh
export PATH=/bin:/usr/bin:/sbin:/usr/sbin 
# List of databases to be backed up separated by space
dblist="drupal itreservations moodle mysql"    

# Directory for backups
backupdir=/path/to/directroy   #edit

# Number of versions to keep
numversions=4

# Full path for MySQL hotcopy command
hotcopycmd=/usr/bin/mysqlhotcopy

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

# Create directory if needed
/bin/mkdir -p ${backupdir}
if [ ! -d ${backupdir} ] 
then
       /bin/echo "Invalid directory: ${backupdir}"
exit 1
fi

# Hotcopy begins here
echo "Hotcopying MySQL Databases..."
RC=0
for database in $dblist
do
       /bin/echo "Hotcopying $database ..."
       $hotcopycmd $userpassword $database ${backupdir}
       RC=$?
       if [ $RC -gt 0 ]
       then
               break;
       fi

       # Rollover the backup directories
       i=$numversions
       /bin/mv ${backupdir}/${database} ${backupdir}/${database}.0 2> /dev/null
       /bin/rm -fr ${backupdir}/${database}.$i 2> /dev/null
       while [ $i -gt 0 ]
       do
               /bin/mv ${backupdir}/${database}.`expr $i - 1` ${backupdir}/${database}.$i 2> /dev/null
                i=`expr $i - 1`
       done
done

if [ $RC -gt 0 ]
then
       /bin/echo "MySQL Hotcopy failed!"
       exit $RC 
else
       # Hotcopy is complete. List the backup versions!
       /bin/ls -l ${backupdir}
       /bin/echo "MySQL Hotcopy is complete!"
fi
exit 0

 

the above script will do the mysql backup (hotcopy) edit paths, passwords and database names to your requirements. to copy things just add something along the lines

 /usr/bin/rsync -avrlHKpogDt /var/www/ /backup/path 

or cp -a to keep things simple

for date time thing something like

#!/bin/bash
time=`/bin/date +%Y-%m-%d`

backup_dir=/backup/$time
/bin/mkdir -p $backup_dir
/bin/tar zcvf $backup_dir/backup.tar.gz /backup/ 2&> /dev/null

# to remove any backup older that 14 days
/usr/bin/find /backup -type d -mtime +14 -exec /bin/rm -rf {} \;

 

hope theres something useful in there

personally I use rsync to back up the servers to remote boxes rather than ftp

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...