Solved

I need a good linux backup to ftp script

Posted on 2004-10-17
9
406 Views
Last Modified: 2013-11-13
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
Comment
Question by:Brad_nelson1
  • 7
9 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 12335719
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
 
LVL 2

Expert Comment

by:Sunjith
ID: 12345756
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
 
LVL 2

Expert Comment

by:Sunjith
ID: 12345770
The MYSQL_HOST and FTP_HOST can be any domain name or IP address.
0
 
LVL 2

Expert Comment

by:Sunjith
ID: 12345790
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Brad_nelson1
ID: 12348122
Hey nice script!
Any chance we can make it backup 2 or 3 mysql databases and folders?

Thanks
Brad
0
 
LVL 2

Expert Comment

by:Sunjith
ID: 12348488
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
 
LVL 2

Expert Comment

by:Sunjith
ID: 12348625
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
 
LVL 2

Expert Comment

by:Sunjith
ID: 12349552
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
 
LVL 2

Accepted Solution

by:
Sunjith earned 500 total points
ID: 12349723
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

This document is written for Red Hat Enterprise Linux AS release 4 and ORACLE 10g.  Earlier releases can be installed using this document as well however there are some additional steps for packages to be installed see Metalink. Disclaimer: I hav…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now