This section will show you how to make a backup of a local directory. The backup will be performed by creating a ZIP file of the local directory and then uploading it to an FTP site.

We will use the EXEC command for creating the ZIP file. This command is used to call external programs from within the script. In the following examples the EXEC command will call the free command-line tool Info-Zip to create the ZIP file. However, you may use any other command-line ZIP utility, for example, PkZip or PowerArchiver. We will use the EXEC command like this:

EXEC("C:\Info-Zip\zip.exe -r MyBackupFile.zip C:\MyFolder")

When ScriptFTP reaches this line it will call C:\Info-Zip\zip.exe and wait for the external program to finish. Once zip.exe has finished we will have MyBackupFile.zip with the contents of C:\MyFolder in the local working directory (change it with LOCALCHDIR) and will be ready to upload it to the FTP site:

# Create the backup file
EXEC("C:\Info-Zip\zip.exe -r MyBackupFile.zip C:\MyFolder")
 
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypass")
 
# Upload the file
PUTFILE("MyBackupFile.zip")
 
# Close host
CLOSEHOST

This method will replace the zip file everytime it is uploaded to the FTP site. If you want to avoid this problem we can add the current date to the ZIP filename:

# Get the current date
$current_date=GETDATE(FORMAT3)
 
# Build the zip filename
# concatenating text strings
$zip_file_name="MyBackupFile-".$current_date.".zip" 
 
# Build the command line
$command_line="C:\Info-Zip\zip.exe -r ".$zip_file_name." C:\MyFolder"
 
# Create the zip file
EXEC($command_line)
 
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypass")
 
# Upload the file
PUTFILE($zip_file_name)
 
# Close connection
CLOSEHOST

You may add even more features to this script file. The following script will make the backup and also delete the intermediate zip file after successful upload. It will also log the script run and it includes some error handling.

# ------- Enter your own settings here --------
# FTP server
$ftp_server="127.0.0.1"
$ftp_user="carl"
$ftp_password="123456"
 
# The local directory you need to backup
$backup_dir="C:\MyDir\"
 
# The ZIP file name
$zip_file_name="MyBackup.zip"
 
# The remote destination folder where
# the ZIP file is uploaded to
$remote_destination_folder="/"
 
# Get the Windows temp directory from
# the system environment variable TEMP
# The backup ZIP file will be created there.
$local_temp_dir=GETENV("TEMP")
 
# The path of the free tool Info-zip
# used to create the zip file
$info_zip_path="C:\infozip\"
 
# --------Script starts here --------------
 
# Get the current date
$current_date=GETDATE(FORMAT3)
 
# Append the current date to the ZIP file name
# ( -> YYYYMMDD_MyBackup.zip)
$zip_file_name=$current_date."_".$zip_file_name
 
# Build the command line used to call the external ZIP tool
$backup_command_line=$info_zip_path."\zip.exe -r ".$zip_file_name." ".$backup_dir
 
# Go to the temp directory where the ZIP file will be created
$result=LOCALCHDIR($local_temp_dir)
IF($result!="OK")
    PRINT("Error changing current local directory. Closing ScriptFTP in 5 seconds")
    SLEEP(5)
    EXIT($result)
END IF
 
# Run Info-zip archiver
$result=EXEC($backup_command_line)
 
# Check if the ZIP file has been created
IF($result!=0)
    PRINT("Error. The ZIP file could not be created. Closing ScriptFTP in 5 seconds")
    SLEEP(5)
    EXIT($result)
END IF
 
# Once this point is reached the ZIP file will already be created
 
# Connect to FTP server
$result=OPENHOST($ftp_server,$ftp_user,$ftp_password)
IF($result!="OK")
    PRINT("Error. Cannot connect to FTP server. Closing ScriptFTP in 5 seconds")
    SLEEP(5)
    EXIT($result)
END IF
 
# Change current remote dir to the destination dir
$result=CHDIR($remote_destination_folder)
IF($result!="OK")
    PRINT("Error. Cannot go to remote destination dir. Closing ScriptFTP in 5 seconds")
    SLEEP(5)
    CLOSEHOST
    EXIT($result)
END IF
 
# Upload the ZIP file
$result=PUTFILE($zip_file_name)
IF($result!="OK")
    PRINT("Error. Cannot upload the ZIP file. Closing ScriptFTP in 5 seconds")
    SLEEP(5)
    CLOSEHOST
    EXIT($result)
END IF
 
CLOSEHOST
 
# Delete the local copy of the zip file
EXEC("del /F /Q ".$zip_file_name)
 
PRINT("-----------------------------------------------")
PRINT("Backup finished. Closing ScriptFTP in 5 seconds.")
PRINT("-----------------------------------------------")
 
SLEEP(5)
EXIT(0)