• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 437
  • Last Modified:

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!
0
Brad_nelson1
Asked:
Brad_nelson1
  • 7
1 Solution
 
avizitCommented:
I have a backup script which tars and zips a directory and saves it in a filename with date in it ...

#!/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



0
 
SunjithCommented:
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.
0
 
SunjithCommented:
The MYSQL_HOST and FTP_HOST can be any domain name or IP address.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
SunjithCommented:
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".
0
 
Brad_nelson1Author Commented:
Hey nice script!
Any chance we can make it backup 2 or 3 mysql databases and folders?

Thanks
Brad
0
 
SunjithCommented:
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.$DATE.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
0
 
SunjithCommented:
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.
0
 
SunjithCommented:
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.
0
 
SunjithCommented:
Here's a patch to the above script for using two database and two dirs.:
-----------
--- backup.sh   2004-10-19 23:00:35.758969441 +0530
+++ backup2.sh  2004-10-19 23:00:17.478404524 +0530
@@ -12,22 +12,30 @@
 # Expect configuration
 set timeout          60

-# MySQL configuration
+# MySQL configuration for database 1
 set MYSQL_USER       username
 set MYSQL_PASS       password
 set MYSQL_HOST       localhost
 set MYSQL_DB         dbname
 set MYSQL_PORT       3306

+# MySQL configuration for database 2
+set MYSQL_USER2      username
+set MYSQL_PASS2      password
+set MYSQL_HOST2      localhost
+set MYSQL_DB2        db2name
+set MYSQL_PORT2      3306
+
 # FTP configuration
 set FTP_USER         ftpuser
 set FTP_PASS         ftppass
-set FTP_HOST         localhost
+set FTP_HOST         host.com
 set FTP_BACKUPDIR    backup

 # Backup configuration
 set TEMP_DIR         /tmp
 set BACKUP_DIR       /home/myself
+set BACKUP_DIR2      /home/other

 # Script configuration
 set BACKUP_LOG       /var/log/backup.log
@@ -43,9 +51,10 @@

 # 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
+exec mysqldump -u $MYSQL_USER2 -p$MYSQL_PASS2 -h $MYSQL_HOST2 -P $MYSQL_PORT2 $MYSQL_DB2 >$TEMP_DIR/$MYSQL_DB2.$DATE.sql

 # Script -- Create archive
-exec tar jcfP $TEMP_DIR/$DATE.tar.bz2 $TEMP_DIR/$MYSQL_DB.$DATE.sql $BACKUP_DIR
+exec tar jcfP $TEMP_DIR/$DATE.tar.bz2 $TEMP_DIR/$MYSQL_DB.$DATE.sql $TEMP_DIR/$MYSQL_DB2.$DATE.sql $BACKUP_DIR $BACKUP_DIR2

 # Script -- FTP
 spawn -noecho ftp $FTP_HOST
-------------

Hope that helps.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now