[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

I need a good linux backup to ftp script

Posted on 2004-10-17
9
Medium Priority
?
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
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
 

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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

You ever wonder how to backup Linux system files just like Windows System Restore?  Well you can use Timeshift in Linux to perform those similar action.  This tutorial will show you how to backup your system files and keep regular intervals. Note…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses

656 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