Solved

I need a good linux backup to ftp script

Posted on 2004-10-17
9
411 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

One of the most frequently asked questions on EE in the "Windows Installer" zone is how to eliminate self-triggered installation of some product.  The problem occurs when, suddenly, whenever a certain application is launched, or even when a folder i…
This article will explain how to establish a SSH connection to Ubuntu through the firewall and using a different port other then 22. I have set up a Ubuntu virtual machine in Virtualbox and I am running a Windows 7 workstation. From the Ubuntu vi…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

863 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

17 Experts available now in Live!

Get 1:1 Help Now