Technology TidBits

Answers to various technical questions on php programming, mysql, linux, and many more categories.
25% off Hosting at HostGator.com:
Use Code techbits25



How can I mirror / backup a set of files via ncftp?

Last updated: 06/24/2009
Often times it's useful to mirror data from a production server to your local computer, or another server.  I've done this for testing purposes, but also for backups.  Most hosting providers backup once a day, or less often.  If you have data being updated often, and you want it saved, you may want to pull it down multiple times per day.  (NOTE:  This method does not copy mysql database information.)

Also, just because your host tells you they run backups, doesn't mean they do - or the backups may not work correctly.  The time to find out is not after the server crashes, or you accidently delete some files on your site.  Your website is your property, and ultimately, you need to take ownership of having backups. 

As the question implies, you first need to install ncftp on the system.  You can get it here:

http://www.ncftp.com/ncftp

Now, set up a "config" file with the remote site information:

host www.yoursite.com
user username
pass yourpassword

Save this file as "ncftp.cfg", and run a "chmod 700 ncftp.cfg" (prevents others from seeing the file).

Now, create a file called "mirror.sh" and add the following lines:

#!/bin/sh

/usr/local/bin/ncftpget -R -f ncftp.cfg /home/mirror remote_dir1 remote_dir2

where "/home/mirror" is the local directory where you want to store the copied files, and remote_dir1 and remote_dir2 are directories on the ftp server that you want to mirror down.

Save this file and run a "chmod +x mirror.sh" to make it executable.

Run the command to make sure it works: "./mirror.sh"

If you want this to run at regular intervals, you should create a cron entry, such as:

0       18      *       *       *       cd /home/localfiles; ./mirror.sh >/dev/null 2>&1

Obviously, check to make sure things are running smoothly, and rest assured of your local backup. 
blog comments powered by Disqus
Have your own Tech-bit to contribute? Submit it here

Other questions in this category:
How can I find out what is taking up all the space on my Linux server?
How can I use 'find' to execute commands on all files found?
How do I figure out how many processors and the speeds, etc. are in my Linux box?
How can I remove DOS linebreaks (^M) from my files?
My hostname keeps reverting to localhost (or localhost.localdomain) - how can I permanently fix this?
How can I determine if iptables is blocking traffic to my site?
Why does my ftp file listing stop at 2000 files?
How can I relocate files from a tar archive with extracting the files?
How can I get a list of files most recently updated / edited?
Some find examples for excluding a directory, and executing commands
Useful script to track spammers connecting to your server
How can I turn off / view my linux firewall using iptables?
How can I split a bit tar file into multiple small files?
How can I exclude some files / directories from a tar file?



Powered by KnowledgebasePublisher 1.1
Host Gator
Content provided by Roberts WebForge, Inc.