Brad_nelson1
asked on
I need a good linux backup to ftp script
I need to backup my files and mysql databases to a remote server via ftp.
I want to be able to schedule it to run each hour.
it also needs to zip up the files and databases into a tar or zip and time and date stamp it.
Does anyone have a script or know of a program to do that?
Thanks for the help!
I want to be able to schedule it to run each hour.
it also needs to zip up the files and databases into a tar or zip and time and date stamp it.
Does anyone have a script or know of a program to do that?
Thanks for the help!
Hi!
The following script takes backup of one of your mysql database and one of the directories (recursively, maybe your home dir) configured to an ftp server. You need to have 'expect' installed in order to use this. You may modify the script to match your needs.
-------------------------- ------
#!/usr/bin/expect -f
# File: backup.sh
#
# Author: Sunjith P S <sunjith@gmail.com>
#
# Function: Backup a mysql database and a directory to a ftp server
# Parameters: <none>
#
# License: This script is provided AS IS with no guarantees what so ever.
# Expect configuration
set timeout 60
# MySQL configuration
set MYSQL_USER username
set MYSQL_PASS password
set MYSQL_HOST localhost
set MYSQL_DB dbname
set MYSQL_PORT 3306
# FTP configuration
set FTP_USER ftpuser
set FTP_PASS ftppass
set FTP_HOST localhost
set FTP_BACKUPDIR backup
# Backup configuration
set TEMP_DIR /tmp
set BACKUP_DIR /home/myself
# Script configuration
set BACKUP_LOG /var/log/backup.log
# Timeout for file transfer. -1 = Never timeout. Use any positive value in seconds.
set TRANSFER_TIMEOUT -1
# Variables
set DATE [exec date +%F]
# Script
log_file $BACKUP_LOG
log_user 1
# Script -- Take backup of MySQL database
exec mysqldump -u $MYSQL_USER -p$MYSQL_PASS -h $MYSQL_HOST -P $MYSQL_PORT $MYSQL_DB >$TEMP_DIR/$MYSQL_DB.$DATE .sql
# Script -- Create archive
exec tar jcfP $TEMP_DIR/$DATE.tar.bz2 $TEMP_DIR/$MYSQL_DB.$DATE. sql $BACKUP_DIR
# Script -- FTP
spawn -noecho ftp $FTP_HOST
expect {
": $" {
send "$FTP_USER\r"
expect {
"ssword:" {
send "$FTP_PASS\r"
expect {
"ftp>" {
send "cd $FTP_BACKUPDIR\r"
expect {
"ftp>" {
send "put $TEMP_DIR/$DATE.tar.bz2 $DATE.tar.bz2\r"
expect {
-timeout $TRANSFER_TIMEOUT "ftp>" {
send "quit\r"
}
}
}
}
}
}
} timeout {
# uhh.. some error occured. It didn't ask for password :-(
exit -1
}
}
} timeout {
# FTP server may not be responding.
exit -1
} eof {
# oops! Maybe ftp crashed.
exit -1
}
}
# uncomment the following line if you need the temporary files to be removed.
#exec rm -f $TEMP_DIR/$MYSQL_DB.$DATE. sql $TEMP_DIR/$DATE.tar.bz2
exit 0
-------------------------- ------
Put the above script in a file: backup.sh
-----------
chmod 755 backup.sh
crontab -e
-----------
Add the following line in crontab:
-----------
0 * * * * /path/to/backup.sh
-----------
Make sure the process has appropriate privileges to write to temporary directory configured. Note that exceptions have not been handled much. So, better provide correct values in configuration section.
Also note that mysql password is provided in command line. So, be cautious if you are using this script on a shared server.
The following script takes backup of one of your mysql database and one of the directories (recursively, maybe your home dir) configured to an ftp server. You need to have 'expect' installed in order to use this. You may modify the script to match your needs.
--------------------------
#!/usr/bin/expect -f
# File: backup.sh
#
# Author: Sunjith P S <sunjith@gmail.com>
#
# Function: Backup a mysql database and a directory to a ftp server
# Parameters: <none>
#
# License: This script is provided AS IS with no guarantees what so ever.
# Expect configuration
set timeout 60
# MySQL configuration
set MYSQL_USER username
set MYSQL_PASS password
set MYSQL_HOST localhost
set MYSQL_DB dbname
set MYSQL_PORT 3306
# FTP configuration
set FTP_USER ftpuser
set FTP_PASS ftppass
set FTP_HOST localhost
set FTP_BACKUPDIR backup
# Backup configuration
set TEMP_DIR /tmp
set BACKUP_DIR /home/myself
# Script configuration
set BACKUP_LOG /var/log/backup.log
# Timeout for file transfer. -1 = Never timeout. Use any positive value in seconds.
set TRANSFER_TIMEOUT -1
# Variables
set DATE [exec date +%F]
# Script
log_file $BACKUP_LOG
log_user 1
# Script -- Take backup of MySQL database
exec mysqldump -u $MYSQL_USER -p$MYSQL_PASS -h $MYSQL_HOST -P $MYSQL_PORT $MYSQL_DB >$TEMP_DIR/$MYSQL_DB.$DATE
# Script -- Create archive
exec tar jcfP $TEMP_DIR/$DATE.tar.bz2 $TEMP_DIR/$MYSQL_DB.$DATE.
# Script -- FTP
spawn -noecho ftp $FTP_HOST
expect {
": $" {
send "$FTP_USER\r"
expect {
"ssword:" {
send "$FTP_PASS\r"
expect {
"ftp>" {
send "cd $FTP_BACKUPDIR\r"
expect {
"ftp>" {
send "put $TEMP_DIR/$DATE.tar.bz2 $DATE.tar.bz2\r"
expect {
-timeout $TRANSFER_TIMEOUT "ftp>" {
send "quit\r"
}
}
}
}
}
}
} timeout {
# uhh.. some error occured. It didn't ask for password :-(
exit -1
}
}
} timeout {
# FTP server may not be responding.
exit -1
} eof {
# oops! Maybe ftp crashed.
exit -1
}
}
# uncomment the following line if you need the temporary files to be removed.
#exec rm -f $TEMP_DIR/$MYSQL_DB.$DATE.
exit 0
--------------------------
Put the above script in a file: backup.sh
-----------
chmod 755 backup.sh
crontab -e
-----------
Add the following line in crontab:
-----------
0 * * * * /path/to/backup.sh
-----------
Make sure the process has appropriate privileges to write to temporary directory configured. Note that exceptions have not been handled much. So, better provide correct values in configuration section.
Also note that mysql password is provided in command line. So, be cautious if you are using this script on a shared server.
The MYSQL_HOST and FTP_HOST can be any domain name or IP address.
If you dont have expect installed, you may download the rpm and install it. To find the appropriate rpm for your distro, go to http://rpm.pbone.net/ and search for "expect".
ASKER
Hey nice script!
Any chance we can make it backup 2 or 3 mysql databases and folders?
Thanks
Brad
Any chance we can make it backup 2 or 3 mysql databases and folders?
Thanks
Brad
Yes, of course. You can add as many. To add one more database, add the following line in MySQL configuration section:
---------------
set MYSQL_DB2 db2name
---------------
and add the following line after the existing 'exec mysqldump':
---------------
exec mysqldump -u $MYSQL_USER -p$MYSQL_PASS -h $MYSQL_HOST -P $MYSQL_PORT $MYSQL_DB >$TEMP_DIR/$MYSQL_DB2.$DAT E.sql
---------------
and modify existing 'exec tar' line to:
---------------
exec tar jcfP $TEMP_DIR/$DATE.tar.bz2 $TEMP_DIR/$MYSQL_DB.$DATE. sql $TEMP_DIR/$MYSQL_DB2.$DATE .sql $BACKUP_DIR
---------------
Also include the new database dump in the existing 'exec rm' line if you want the temporary file to be removed.
I hope with that you got the idea of how to modify the script to add as many databases as necessary.
Regards,
Sunjith
---------------
set MYSQL_DB2 db2name
---------------
and add the following line after the existing 'exec mysqldump':
---------------
exec mysqldump -u $MYSQL_USER -p$MYSQL_PASS -h $MYSQL_HOST -P $MYSQL_PORT $MYSQL_DB >$TEMP_DIR/$MYSQL_DB2.$DAT
---------------
and modify existing 'exec tar' line to:
---------------
exec tar jcfP $TEMP_DIR/$DATE.tar.bz2 $TEMP_DIR/$MYSQL_DB.$DATE.
---------------
Also include the new database dump in the existing 'exec rm' line if you want the temporary file to be removed.
I hope with that you got the idea of how to modify the script to add as many databases as necessary.
Regards,
Sunjith
oops! a correction. $MYSQL_DB in my previous post in line exec mysqldump should be $MYSQL_DB2.
If the username and password etc. for the new mysql database is different, you will have to configure new usernames and passwords also.
If the username and password etc. for the new mysql database is different, you will have to configure new usernames and passwords also.
And to add more dirs, add the following in backup config section:
-------------------
set BACKUP_DIR2 /home/anotherdir
-------------------
and append $BACKUP_DIR2 to the 'exec tar' line. That's it. You can add as many dirs like that also.
-------------------
set BACKUP_DIR2 /home/anotherdir
-------------------
and append $BACKUP_DIR2 to the 'exec tar' line. That's it. You can add as many dirs like that also.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#!/bin/csh
set DATE=`date +%F`
tar -cvjf {$1}-$DATE.tar.bz2 $1
you can easily modify the above to suit your requirements...
also to make it run every hour you can use cron ..
you can find details of cron at http://www.unixgeeks.org/security/newbie/unix/cron-1.html
bout the ftp i think you have to write ftp script for it , which have been discussed quite a lot in here
check
http:Q_10174414.html
http:Q_10026433.html