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 “”

# Clean up the old backup files
rm -rf /var/www/vhosts/website/httpdocs/bk

#Make new directory

# Source Folder
cd /var/www/vhosts/website/httpdocs/

# Dirs to backups

# Save zip file elsewhere on the server

#Path to binary files
for d in $DIRS
zipfile="${DEST}/`date '+%Y%m%d'`.zip"
echo -n "Creating $zipfile..."

# create zip file
${ZIP} -r $zipfile $d &>/dev/null && echo "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. Refer to learn more about vi editor


Include this lines and save the CRON.

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

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

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 ?


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.


* 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/$";

// Set up basic FTP connection

$ftp_server = ""; //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


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.