A couple of weeks ago, I made a post about the fact that most website owners don’t even have backup of their website. The fact is, while most web hosting companies have backup copies of their web server’s data, they just won’t restore your website on-demand. This is due to the fact that their backup are meant to restore data in case of a server failure for instance.
For most people who use shared hosting services, it is possible to generate full or partial website backups manually. While this can prove to be useful for static websites, it wouldn’t make sense to manually backup a dynamic website everyday.
For this purpose, I’ve created a Simple FTP Backup Script for Linux-based web servers. This Perl backup script allows you archive files and MySQL databases from your hosting account and upload them to a remote FTP server.
By scheduling the execution of this script automatically through a CRON task, you will be able to store daily backups of your website on a remote FTP server.
In order to run this script, you will Perl as well as the Net::ftp class.
Here’s how to use this script :
/usr/bin/perl /path/to/ftp-backup-script.pl
It’s as simple as that!
Of course, this is a beta release and I cannot guarantee the results nor do I take responsibility for loss of data so use it carefully.
Stay tuned as I will show you how to schedule backup tasks using cPanel and strategic ways to create backups efficiently.
Any comments to help me improve this backup script are welcomed!
Posted in Articles | 22 Comments
22 responses so far ↓
1. Response by : How to Backup your Website Automatically with cPanel on Jul 26, 2009 at 2:14 pm
[...] 26th, 2009 · No Comments In my last post, I released my Simple FTP Backup Script (v0.9b). This Perl script allows you to perform a backup of your files and MySQL databases as well [...]
2. Response by : MA on Aug 12, 2009 at 11:51 pm
Thank you for writing this script! I have a bluehost account and the backup works like a charm. The ftp transfer is the only portion I’m running into problems with. Any help you can provide will be greatly appreciated. I can’t seem to get past Net::FTP->new(). I get an error – Can’t call method “login” on an undefined value at ftp-backup-script.pl. My ftp host is another bluehost account. The parameter I’m passing for the hostname is of the format ‘domain.com’. Any ideas what I’m doing wrong? Thank you for your time.
3. Response by : The Web Hosting Hero on Aug 14, 2009 at 7:49 am
I have an account with Bluehost also although this script have been tested on my dedicated server.
I’ll test the script on my Bluehost account later today and let you know what’s wrong.
4. Response by : Syaman on Aug 23, 2009 at 5:48 am
Hi I’ve scheduled the script as a cronjob.
Checking my backup destination directory however, I can see full_site_backup tar.gz file but there is no .sql.gz backup file of my MySQL database.
So it seems like the MySQL backup did not run for me.
I am using a DirectAdmin-based shared hosting service, and for $dbuser and $dbpwd I’ve placed my phpMyAdmin login credentials. For $dbhost I’ve left it as ‘localhost’.
What can I do to troubeshoot this?
My next question is regarding
$ftp_dir = “/remote_backups”;
Should this be the full path to the directory where the backups will be FTP’d to?
Thank you
5. Response by : Syaman on Aug 23, 2009 at 7:50 am
Hi I’ve resolved the problems in my previous posts. Backups worked fine as did FTP backup.
My next issue seemed to be that the backup file was not deleted even though it was FTP’d and $delete_backup = 1 was set.
Turns out that I had to make a few changes to the last few lines of your script.
—
# DELETING FULL SITE BACKUP
if($delete_backup = 1)
{
unlink($full_site_backup);
}
—
I had to change to:
—
# DELETING FULL SITE BACKUP
if($delete_backup == 1)
{
unlink($full_backup_file);
}
—
Hope this helps everyone
6. Response by : Jon on Aug 30, 2009 at 11:38 pm
This scripts works perfect.
I need one to restore my backup.
The way back
is any script available?
Thank you
7. Response by : The Web Hosting Hero on Aug 31, 2009 at 7:24 am
@Syaman: yes you are right about the script. The corrected script is now available for download.
Thank you!
8. Response by : The Web Hosting Hero on Aug 31, 2009 at 7:27 am
@Jon: unfortunately there’s no restore script but it’s quite easy to achieve manually. The thing is that you usually can’t create a MySQL database other than from your control panel.
Extract the content from the main backup archive. You’ll find two other compressed files. One contains the files from your website and the other contains the MySQL data dump.
Extract the content from both archives, upload the files to your web server and use phpMyAdmin to restore the MySQL data.
9. Response by : Marc J on Sep 5, 2009 at 4:44 am
Hi,
This script looks like exactly what I need. A few questions: -
Can it be set to use FTP in passive mode?
Can the FTP port be changed (I need 121)?
10. Response by : Marc J on Sep 5, 2009 at 12:48 pm
To answer my own question, I changed: -
my $ftp = Net::FTP->new($ftp_host, Debug => 0)
to: -
my $ftp = Net::FTP->new($ftp_host, Debug => 0, Port => 121, Passive => 1)
And it works for me
One thing, in the cron job output I get: -
tar: Removing leading `/’ from member names
tar: Removing leading `/’ from member names
at the start, it doesn’t seem to do any damage – is there any way to stop this?
11. Response by : Marc J on Sep 6, 2009 at 5:18 am
One more request (sorry!).
Would it be possible to add folders to exlude from the file backup? It seems the backup dir is already excluded (I’m assuming to prevent the backup possibly including itself), but I have a rather large folder within the backup dit that I do not need to backup, and doing so is wasting a lot of bandwidth every time I FTP it off-site.
Ideally, the option to define a list of excluded folders, if there are more than one…then the script WOULD be perfect
12. Response by : Steve on Sep 19, 2009 at 4:59 am
Just found your script and if it works it will be great just one question before trying. Is there a limit on the size of the backup as doing a backup within cpanel I get the message unable to complete backup due to the file being to big
13. Response by : The Web Hosting Hero on Oct 2, 2009 at 7:16 am
@Steve: the issue is not really the size of the file, it’s rather about the amount of memory allowed for PHP scripts to run or the maximum execution time.
These settings are set by your web host. Who are you hosting with?
14. Response by : james on Nov 2, 2009 at 1:39 pm
noticed also the date stamp is a month behind on mine, as my backup today came up as 2-10-2009.
15. Response by : Marc J on Nov 2, 2009 at 2:00 pm
@james: Unix timestamp for months runs from 0 – 11: -
Jan = 0
.
..
..
Dec = 11
16. Response by : MaxEmil on Jan 6, 2010 at 12:43 pm
Great script, thanks!
An easy googlesearch for “tar exclude” gives an answer
http://answers.google.com/answers/threadview/id/739467.html for how to exclude several directories.
By defining several –exclude
or by using an exclude file.
BR\ME
17. Response by : dennis on Jun 18, 2010 at 4:46 am
goodmorning,
when i edit the $directory to backup with the string /home/[user]/domains/[domainname]/public_html (which contains 47 MB), i still get two backup files from 980 MB. why is this?
Greetings, Dennis, The Netherlands
18. Response by : The Web Hosting Hero on Jun 18, 2010 at 8:47 am
@dennis: What are the file names and in which directory are they?
19. Response by : The Web Hosting Hero on Jun 18, 2010 at 8:48 am
@dennis: of course, I am referring to the 2 backup files.
20. Response by : Marc J on Jun 19, 2010 at 1:29 am
Occassionally the FTP connect / login / upload fails (problem at the receiving end, not the script) and when this happens the backup file is left on the server (even if $delete_backup = 1).
As these backups are quite large it only takes a few failures in a row to hit the account disk quota. Is there any way of deleting the local backup file on error as well?
21. Response by : Tim on Aug 6, 2010 at 11:12 am
Hi,
I have multiple mysql db’s with different usernames and passwords for each (this is how the host company does it)
How can I get the script to accommodate this as it looks like yours will only accept a global u/n & p/w?
Thanks
22. Response by : The Web Hosting Hero on Aug 9, 2010 at 12:34 pm
At this point, you have 3 options:
1) use a master MySQL account that has access to all db’s
2) modify the script to include an array of databases and iterate through it
3) make multiple copies of this script, one for each db instance (this is not very elegant though)
But I’ll keep that as a suggestion for a future release of the script.
Leave a Comment