In this tutorial we are going to make a PHP script that archives your website into a .tar file, then automatically moves it to an external FTP server for safe keeping. We can then setup a cron for this script which will execute it automatically every day/week/month , however frequent you want it. This will only work on Linux servers however that shouldn’t be a problem as most hosts run Linux.
First we’ll setup some variables to allow for easy editing.
$dir = '/path/to/file'; // Directory to backup.
$filename = 'backups/backup' . date("MdY") . '.tar'; //path to where the file will be saved.
$ftp_server = 'urlToFTPServer.Com'; //External FTP server
$ftp_user_name = 'ftpUsername'; //External FTP server username
$ftp_password = 'ftpPassword'; //External FTP server password
Now we need to archive the site. We’re going to wrap an if statement around it so if for some reason it doesn’t archive, the FTP part of the script won’t try and copy it over to another server. The system function executes a system command, here we are calling tar cvf which will archive our $dir (directory) and save it to the path specified in $filename.
if(system("tar cvf $filename $dir")){
We’ll then connect to the FTP server itself. Because we set variables, if we need to change servers, it’s very easy to implement.
If it connects to the server, then we can move our backup across. We give it the same name as the archive on our own server and store it in a /backups directory.
Once that is done, we can close the FTP stream as we don’t need to use it anymore.
ftp_close($conn_id);
} //Initial if statement
All thats needed to do is automate this script. For this we use something built into the Linux Operating System called a Cron Tab. Crons will automatically run a script at a time you specify. We can set it up to run every time at the same day so you have a daily backup. You can make this more frequent or less frequent depending on how often the data of your site changes.
by
Janeth Kent Date:
29-04-2013
hits :
5270
Janeth Kent
Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.
MENSAJE
OPERACIÓN EJECUTADA
We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".
These cookies are necessary for the website to function and cannot be disabled on our systems. They are generally only set in response to your actions in requesting services, such as setting your privacy preferences, logging in or completing forms. You can set your browser to block or alert you to these cookies, but some areas of the site will not work. These cookies do not store any personally identifiable information
Performance Cookies
These cookies allow us to count visits and traffic sources so that we can assess the performance of our site and improve it. They help us know which pages are the most or least visited, and how visitors navigate the site. All information collected by these cookies is aggregated and therefore anonymous. If you do not allow these cookies to be used, we will not know when you visited our site and will not be able to assess whether it worked properly
Functional Cookies
These cookies allow the website to provide better functionality and customization. They may be set by our company or by external providers whose services we have added to our pages. If you do not allow these cookies to be used, some of these services may not function properly
Targeted Cookies
These cookies may be set through our site by our advertising partners. They may be used by those companies to profile your interests and display relevant ads on other sites. They do not directly store personal information, but are based on the unique identification of your browser and Internet device. If you do not allow these cookies to be used, you will see less targeted advertising