Automatic Website Backup From Linux Server PHP Script

Back up Issue:

Manual backing up of website content from the server to local machine should be fully automated. The system must be able to zip the website content and download to the local machine twice every week using simple PHP and shell script.

Solution: Automatic Website Backup From Linux Server – PHP script

Automatic Website Backup From Linux Server PHP script using FTP function and sh code for zipping the backup files on timely base.
Created two services to make a weekly backup.
1)      Server( Centos) – A shell script which compresses the entire web site files and stores in a specific path. This script runs twice every week. Cron will take care to execute periodically based on scheduled time.

2)      Client or Local machine – PHP script which downloads the zip file through FTP commands. Using windows task scheduler, the PHP script will execute periodically to download the files.

 

Code & Detail View

On server Side: Create a shell script with the below code and let us name it as “site_backup.sh”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
# Clean up the old backup files
rm -rf /var/www/vhosts/website/httpdocs/bk
 
#Make new directory
mkdirbk
 
# Source Folder
cd /var/www/vhosts/website/httpdocs/
 
# Dirs to backups
DIRS="/var/www/vhosts/website/httpdocs/site/"
 
# Save zip file elsewhere on the server
DEST="/var/www/vhosts/website/httpdocs/bk/"
 
#Path to binary files
ZIP=/usr/bin/zip
for d in $DIRS
do
zipfile="${DEST}/`date '+%Y%m%d'`.zip"
echo -n "Creating $zipfile..."
 
# create zip file
${ZIP} -r $zipfile $d &>/dev/null && echo "Done!"
done

Setting up the Cron

Crontab –l : List all the cron job

Crontab –e : To edit the crontab. This command opens the cron configuration file Vi editor. Referhttp://www.cs.rit.edu/~cslab/vi.html to learn more about vi editor

 

Include this lines and save the CRON.

10 * * * 3 sh /var/www/vhosts/site/httpdocs/site_backup.sh

10 * * * 6 sh /var/www/vhosts/site/httpdocs/site_backup.sh

So we created two cron jobs, one runs at 10 pm on Wednesday and other runs on Saturday the same time. Basically a cron takes five parameters and the script to be run. To have insight How to create a cron ?http://www.centos.org/docs/5/html/5.2/Deployment_Guide/s2-autotasks-cron-configuring.html

 

Local Machine typically a windows

Create a PHP script and name it as you want ex: ftp_download.php to download the zip file from the linux server.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
 
* Script to download zip file from the specified FTP path.
 
*/
 
// Calculating today’s date
 
$currentDate = date('Ymd');
 
//Server and local machine path
 
$local_file = "E:/server backup/{$currentDate}.zip";
 
$server_file = "/httpdocs/bk/$currentDate.zip";
 
// Set up basic FTP connection
 
$ftp_server = "xx.xx.xx.xxx"; //IP address or any host name
 
$ftp_user_name = "yyyyyyy"; //FTP User name
 
$ftp_user_pass = "zzzzzzz"; // FTP Password
 
$conn_id = ftp_connect($ftp_server);
 
// FTP login with username and password
 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
 
ftp_pasv($conn_id, true);  //Passive Login
 
// Download the file from server and save it on local machine
 
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
 
echo "Successfully written to $local_file\n";
 
} else {
 
echo "Download Fails\n";
 
}
 
// Close the connection
 
ftp_close($conn_id);

Now you can make this script to run periodically using the windows scheduler if you use windows machine, or use cron if you use the Linux based machine. Since we are creating backup files on Wednesday and Saturday, with the name of the file being simply the date itself. You can schedule to run this PHP script on Wednesday and Saturday.