Web Hosting Forum | Lunarpages

Author Topic: HOW TO: Backup site and database with cron  (Read 5393 times)

Offline TheFLPatriot

  • Newbie
  • *
  • Posts: 2
HOW TO: Backup site and database with cron
« on: December 12, 2011, 07:40:51 PM »
There is a similar topic here: http://www.lunarforums.com/web_hosting_tutorials_faqs_and_resources/site_and_mysql_backups_via_cron-t22118.0.html
However neither of these suited my needs so I wrote a new script. This script simply makes backup of your whole home directory (everything in your account) except for your backup directory. It also will backup all of your databases. These backups are kept inside of your backup directory for you to do with them as you please.

My reasoning behind this script is that it is simpler to have your local machine connect and download the backups, than to set up a full FTP server. Most people have cable or DSL at their home, and my not have a machine on 24/7 ready to accept a file. This way you can set up a simple script that will use your ftp client to download the files to your home machine without a home server.

Now for the script. I have fully commented every line to the best of my ability. I have used parts of the script in the referenced post.

Code: [Select]
<?
$datestamp = date("Y-m-d_H-i-s"); // Current date to append to filename of backup file in format of YYYY-MM-DD

// CONFIGURE THE FOLLOWING VARIABLES TO MATCH YOUR SETUP
$backupdir = "backup"; // The name of your backup directory
$filename = "$backupdir/backup-website-$datestamp.tar";   // The name of the backup file

$command = "cd ~/"; // Change dir to your home
$command = "tar cvf $filename --exclude=$backupdir *"; // Tar your whole home (website) except for the backup dir
$result = exec($command); // Execute the command

$command = "gzip -9 -S .gz $filename"; // Zip your backup
$result = exec($command); // Execute the command



// Duplicate this section for each database if database have diffrent usernames
// CONFIGURE THE FOLLOWING VARIABLES TO MATCH YOUR SETUP
$dbuser = "username"; // Database username
$dbpwd = "password"; // Database password
$dbname = "--all-databases"; // Database name. Use --all-databases if you have more than one
$dbhost = "hostname/IP"; // Database host

// Pick which line to use. If using all databases pick the first line, if indivual pick the second
$filename = "$backupdir/backup-databases-$datestamp.sql";   // All databases in one file
//$filename = "$backupdir/backup-$dbname-$datestamp.sql";   // The name of the backup file

$command = "mysqldump -h $dbhost -u $dbuser -p$dbpwd $dbname | gzip > $filename"; // Dump the database(s) into the backup file
$result = passthru($command); // Execute the command

$command = "gzip -9 -S .gz $filename"; // Zip your backup
$result = exec($command); // Execute the command
?>

I am not exactly an expert in scripting, but I will try to answer any questions. I will also include the script I use to download these backups (although you can just use any ftp client at your convenience).

Code: [Select]
#!/bin/sh
###### Description
# This script connects to a remote server and downloads a backup file
# All refrences to "home server" refer to the machine that you are backing up to. ie. your home server
# All references to "remote server" refer to the machine you are backin up. ie. your hosted web site with LP

###### Prerequisites
# This script requires ncftp client. For other clients adjustment will be required.
# If you set your ftp account to start in the folder where your backups are there is no need to change FTPF

###### Script
FTPU="username" # ftp login name
FTPP="password" # ftp password
FTPS="hostname/ip" # remote ftp server
FTPF="/srv/backup" # directory where the backup files are kept.
DFILE="*.gz" # the files to download


ncftpget -u $FTPU -p $FTPP $FTPS  $FTPF $DFILE

##### End of script#####

The above script works on Ubuntu 11.10 server. YMMV
« Last Edit: December 12, 2011, 10:45:42 PM by TheFLPatriot »

Offline keiooz

  • Trekkie
  • **
  • Posts: 19
Re: HOW TO: Backup site and database with cron
« Reply #1 on: June 12, 2012, 03:40:44 AM »
I have switched my server to Ubuntu, does this work in its latest update?

 

Share |