RT Cunningham


FTP vs. Everything - Using PHP to Run FTP Scripts on My Laptop

PHP FTP I’m really dense and obtuse sometimes. I don’t need to use Mega, Syncthing or anything other than FTP to do what I need to do. FTP will do the trick since I only need to back up a few files every day.

Since I’m running Linux Mint on my home computer, I’m using a PHP script to back up files and send them to my server.

Using PHP to Send Files with FTP

Plain FTP isn’t secure. User names and a passwords get sent using plain text. Sending files over an explicit TLS connection is a better idea. With PHP, I can do it either way but I’m smart enough to do it the secure way.

The PHP manual provides all the functions I need to know. Sending files with FTP seems slower than sending them in other ways but it doesn’t matter. It’s all automated and taking place while I sleep.

I’m running WordPress on my home computer instead of on the server. I have a PHP script that creates static files. I use FileZilla to upload them soon after I run the script. It doesn’t take long.

My backup script zips and gzips files before they’re sent. When I tested the script, it took almost an hour to transfer all the files.

I Can’t Use FTP to Remove Files

Syncing software does something FTP can’t do: Remove files on the server when they’re removed locally. My back up file names start with an eight-digit date like “20180226”. My back up script removes the older ones by comparing the dates.

I have more than 20 gigabytes of free space at the server and I don’t need more than two for my backups. I have a PHP script that runs daily on the server to prune the old files.

The PHP FTP Script

I back up my website directory, my Thunderbird directory and my database dump every day. All three are in this script but I changed some of the code to make it anonymous. I prune files older than three days.

$date = date('Ymd');
$local_path = '/home/name/backups/';
$remote_path = '/home/name/backups/';
shell_exec('zip -r6 ' . $local_path . $date . '' . ' /home/directory/website');
shell_exec('zip -r6 ' . $local_path . $date . '' . ' /home/directory/.thunderbird');
shell_exec('mysqldump -upassword database | gzip -c -9 > ' . $local_path . $date . '_database.sql.gz');
foreach(glob("*.gz") as $filename) {
  $a = explode('_', $filename);
  if ($date - $a[0] > 2 ) unlink ($filename);
foreach(glob("*.zip") as $filename) {
  $a = explode('_', $filename);
  if ($date - $a[0] > 2 ) unlink ($filename);
foreach (glob("*.gz")  as $filename) { $arcfile[] = $filename; }
foreach (glob("*.zip") as $filename) { $arcfile[] = $filename; }
$ftp_server=''; # Host name or IP address
$ftp_port = 21;             # Needed only if you use an alternate port
$conn_id = ftp_ssl_connect($ftp_server, $ftp_port);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if (!$login_result) exit;
ftp_pasv($conn_id, true);
for ($i = 0; $i < count($arcfile); $i++) {
  $remote_file = $remote_path . $arcfile[$i];
  $local_file = $local_path . $arcfile[$i];
  ftp_put($conn_id, $remote_file, $local_file, FTP_BINARY);

The Server Script

This is the corresponding script at the server. I had to add a day because the server is in California and my laptop is 15 to 16 hours ahead (depending on daylight savings time over there).

$date = date('Ymd')+1;
foreach (glob("*.gz") as $filename) {
  $a = explode('_', $filename);
  if ($date - $a[0] > 2 ) unlink ($filename);
foreach (glob("*.zip") as $filename) {
  $a = explode('_', $filename);
  if ($date - $a[0] > 2 ) unlink ($filename);

FTP is Good Enough for Me

I don’t have tons of audio and video files I want to back up online. Why? Because I don’t intend to share them with people who don’t live nearby.

If I want to share a huge video file with one of my relatives in the United States, I’ll probably use Mega because I won’t have to worry about running out of space.

I only have a few files to back up every day. Anything other than FTP to my server is probably overkill.

Share: Facebook | Twitter

By RT Cunningham
February 26, 2018