backup_advanced.ftp

This script ZIPs a local directory and uploads the created ZIP file to a FTP site. It uses the free command-line tool Info-zip. Download it at https://www.info-zip.org
The ZIP file name is created based on the current date.
This script also has error handling on most of command calls.

# ------- 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
$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 was created
IF($result!=0)
 PRINT("Error. The ZIP file could not be created. Closing ScriptFTP in 5 seconds")
 SLEEP(5)
 EXIT($result)
END IF
 
# Reached this point means that we already have the ZIP file 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)