--- backup.module	2006-10-01 19:11:58.000000000 -0700
+++ backupnew.module	2006-12-16 19:41:38.000000000 -0800
@@ -57,6 +57,12 @@ function backup_menu($may_cache) {
 			"callback" => "backup_page",
 			"type" => $menu_type,
 			);
+    $retval[] = array(
+      "path" => "admin/settings/backup",
+      "title" => t("backup"),
+      "callback" => "backup_settings",
+      "access" => user_access("make backups"),
+      );
 	}
 
 	return($retval);
@@ -150,7 +156,7 @@ function backup_page_submit() {
 * @return mixed If there is content to be displayed, it is returned.
 *	Otherwise, NULL is returned on success.
 */
-function _backup_backup() {
+function _backup_backup($destination = NULL) {
 
 	$files = backup_get_files();
 	$programs = backup_get_programs();
@@ -173,7 +179,7 @@ function _backup_backup() {
 	*	calls tempname() and returns that string, since we currently have
 	*	the string '/tmp/ referenced in multiple places.
 	*/
-	$tmp_dir = '/tmp';
+	$tmp_dir = variable_get('backup_tmp_dir', '/tmp');
 	$tmpfile = tempnam($tmp_dir, 'drupal_backup_');
 	//
 	// $tmpfile2 is our destination file for the tarball.
@@ -231,21 +237,55 @@ function _backup_backup() {
 		return($content);
 	}
 
-	//
-	// Start the file download
-	//
+  //
+  // Get variables ready for file
+  //
 	$server_name = getenv('SERVER_NAME');
 	$filename = 'backup-' . $server_name . '-'.strftime('%Y-%m-%d-%H-%M-%S') . '.tgz';
 
-	$headers = array(
-   		//'Content-Length: ' . filesize($tmpfile2),
-		'Content-Disposition: attachment; filename="' 
-			. $filename . '"',
-   		//'Content-Transfer-Encoding: binary',
-   		'Content-Type: binary/octet-stream',
-		);
+  //
+  // Upload file to Amazon S3 if the user has chosen to do so.
+  //
+  if ($destination == 'S3') {
+    require_once(drupal_get_path('module', 'backup') . '/s3.class.php');
+    $bucket = variable_get('backup_s3_bucket','');
+    $data = file_get_contents($tmpfile2);
+    $transfer = new s3();
+
+    // For some reason, the transfer fails if we try to set the keyID and secretKey through the S3 class,
+    // which is why we must manually hack our numbers into the s3.class.php file during installation
+    // $transfer->keyId = variable_get('backup_s3_key_id','');
+    // $transfer->secretKey = variable_get('backup_s3_secret_key','');
+
+    // Turn this on for debugging. Disable for production sites, as it can reveal important data about your S3 account.
+    $transfer->_debug = FALSE;
+
+    //  _contentType must be set to NULL or the transfer will fail. Is this universal?
+    $transfer->_contentType = NULL;
+
+    // Create the bucket on the server if it does not exist.
+    $transfer->putBucket($bucket);
+
+    // Transfer the file
+    $transfer->putObject($filename, $data, $bucket, NULL, NULL);
+  }
+
+	//
+	// Start the file download if user is downloading file manually
+	//
+  if (!$destination) {
+
+  	$headers = array(
+     		//'Content-Length: ' . filesize($tmpfile2),
+  		'Content-Disposition: attachment; filename="' 
+  			. $filename . '"',
+     		//'Content-Transfer-Encoding: binary',
+     		'Content-Type: binary/octet-stream',
+  		);
+  
+  	file_transfer($tmpfile2, $headers);
 
-	file_transfer($tmpfile2, $headers);
+  }
 
 	return(null);
 
@@ -670,11 +710,11 @@ function backup_database($tmpfile, &$err
 
 /**
 * Our crontab.  It will delete any tempory files that this module leaves
-* laying around.
+* laying around, and also backup to Amazon S3 if necessary.
 */
 function backup_cron() {
 
-	$tmp_dir = '/tmp';
+	$tmp_dir = variable_get('backup_tmp_dir', '/tmp');
 
 	$fp = @opendir($tmp_dir);
 
@@ -713,6 +753,13 @@ function backup_cron() {
 
 	}
 
+  if (((time() - variable_get('backup_cron_timestamp', '')) >= 1) && variable_get('backup_s3', 0)) {
+    /* reset timer */
+    variable_set('backup_cron_timestamp', time());
+    _backup_backup('S3');
+    watchdog('backup', 'Automatic backup to Amazon S3', WATCHDOG_NOTICE);
+  }
+
 } // End of backup_cron()
 
 
@@ -738,5 +785,25 @@ function backup_show_path() {
 } // End of backup_show_path()
 
 
-
-?>
+/**
+* Allows us to configure a few variables.
+*/
+function backup_settings() {
+  global $user;
+  $form['backup_tmp_dir'] = array('#type' => 'textfield', '#title' => 'tmp directory', '#description' => t('Enter a username to check if it will be denied or allowed.'), '#size' => 30, '#maxlength' => 64, '#default_value' => (variable_get('backup_tmp_dir', '/tmp')));
+  $form['s3'] = array('#type' => 'fieldset', '#title' => t('Amazon S3'));
+  $form['s3']['backup_s3'] = array('#type' => 'checkbox', '#default_value' => variable_get('backup_s3', 0), '#title' => 'Use Amazon S3 for Backups', '#description' => 'Your site will be backed up to the Amazon S3 file storage service. You must have an Amazon S3 account and enter your information below.');
+  $frequency = array('3600' => t('Hourly'), '86400' => t('Daily'), '604800' => t('Weekly'), '2592000' => t('Monthly'));
+  $form['s3']['backup_frequency'] = array('#type' => 'select', '#title' => t('Backup Frequency'), '#default_value' => variable_get('backup_frequency', 15), '#options' => $frequency, '#description' => t('How often should a backup be saved to S3? (Must invoke cron)') );
+  if (variable_get('backup_s3_restrict_keys', 0) && $user->uid != 1) {
+    $form['s3']['backup_s3_restricted'] = array('#type' => 'item', '#title' => t('To change the values of the Amazon S3 keys, login as user #1'));
+  } else {
+    $form['s3']['backup_s3_restrict_keys'] = array('#type' => 'checkbox', '#default_value' => variable_get('backup_s3_restrict_keys', 0), '#title' => 'Restrict access to keys', '#description' => 'Check this box if you wish to restrict access of these fields to user #1.');
+    $form['s3']['backup_s3_key_id'] = array('#type' => 'textfield', '#title' => 'Amazon S3 Key ID', '#description' => t('Enter your Amazon S3 Key ID.'), '#size' => 30, '#maxlength' => 128, '#default_value' => (variable_get('backup_s3_key_id', '')));
+    $form['s3']['backup_s3_secret_key'] = array('#type' => 'textfield', '#title' => 'Amazon S3 Secret Key', '#description' => t('Enter your Amazon S3 Secret Key.'), '#size' => 30, '#maxlength' => 128, '#default_value' => (variable_get('backup_s3_secret_key', '')));
+    $form['s3']['backup_s3_bucket'] = array('#type' => 'textfield', '#title' => 'Amazon S3 Bucket', '#description' => t('Enter the name of the bucket the backup files should be uploaded into.'), '#size' => 30, '#maxlength' => 64, '#default_value' => (variable_get('backup_s3_bucket', '')));
+    $form['user']['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
+  }
+  $output .= drupal_get_form('system_settings_form', $form);
+  return $output;
+} // End of backup_settings()
\ No newline at end of file
