background image

Content tagged with: FTP

Eric's picture

Recently, I had to work on a few Drupal sites and only had FTP access to the webservers. One thing is for certain: FTP is slow and painful. I prefer SSH access so I can interact with Subversion, compress files, dump mysql databases, and transfer files securely. I tried to copy the entire remote docroot to my local development environment (using my FTP client, CyberDuck), and the time estimate to copy all the files individually was ridiculous. There are over 500 files in Drupal core alone, not to mention all the 3rd party modules and uploaded files. I decided to upload a tiny PHP file to execute once to backup the filesystem outside the docroot, so I could copy a single compressed file. Before you attempt something like this, you MUST understand the security risk and vulnerability of exposing site archives and having PHP scripts like this on your server. For instance, if someone was able to get a hold of your settings.php file, they'll have access to your MySQL DNS (connection string). Hopefully, your webserver does not have MySQL and other important services exposed through your firewall, but that is a different topic altogether. I'm already having doubts sharing this PHP snippet. I uploaded the following code to a file in the docroot of Drupal, browsed to the web path once, then promptly removed it from existence. Afterward, I was able to copy the entire filesystem as one file (one transfer), maintain my sanity, and saved myself hours of slow FTP transfers.

<?php
// define a list of valid IPs that can access this file
// yes, I know, this will not prevent spoofers, etc
$validIPs = array(
 
'MY-IPADDRESS',
 
'MY-OTHER-IPADDRESS'
);

// ensure the request is coming from a valid IP address
if (!in_array($_SERVER['REMOTE_ADDR'], $validIPs)) die;

// define a path to the archive to create
// VERY IMPORTANT: you must prefix the file path with "../" to ensure the archive is created outside the docroot path!
// NOTE: you may need to update the file path to work in your hosting situation
$filePath = "../backup.tar.gz";

// ensure the file does not already exist
if (file_exists($filePath)) die;

// define the command to execute to compress the site
// NOTE: you may need to specify the full path to the tar command
$command = "tar -czf $filePath .";

// execute the command to compress the entire site
exec($command);

echo
"done."
?>

After this code is executed, be sure to remove the PHP script and archive!

Eric's picture

I recently wrote a quick BASH shell script to FTP a log file to another server monthly. First, I modified the logrorate configuration to rotate a service's logs monthly. Then I added a cron job to be executed the following script once a month. NOTE: It's important to give logrotate enough time to finish rotating the logs. Here's my script:

#!/bin/bash

_user="MYFTPUSER"
_password="MYFTPPASSWORD"

# create a date string in the format YYYYMM for last month
_date=$(date +%Y%m --date="-1 month")

# Create FTP connection and put the log in the user's home folder
ftp -n MYFTPSERVER <<EOF
user $_user $_password
binary
put /var/log/MYROTATEDLOG.log.1 ~/MYROTATEDLOG.$_date.log
bye
EOF