SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2012-11-29 15:01:20

hozza
Member
From: /dev/null
Registered: 2012-11-20
Posts: 45
Website

Bash Website Mirror lftp

#!/bin/bash
ARCHIVE="$1_$(date +%Y-%m-%d_%H-%M).tar.gz"

if [ -d "/home/hozza/Sites/$1" ]; then
	{
		{
			lftp -e "mirror -e -n -p --parallel=10 --log=/home/hozza/Backups/logs/$1_$(date +%Y-%m-%d_%H-%M).txt $2/ /home/hozza/Sites/$1/; exit top" $3 &&
			cd /home/hozza/Sites/ &&
			tar -czf /home/hozza/Backups/$ARCHIVE $1 &&
			echo "Mirror Log: $1_$(date +%Y-%m-%d_%H-%M).txt" &&
			echo "Archive Created: $ARCHIVE" &&
			notify-send -i gnome-network-properties -t 0 "Mirror & Archive Complete" "$(date +%Y-%m-%d) $(date +%H:%M) \n $1"
		}
	} || notify-send -i error -t 0 "Mirror & Archive Error" "$(date +%Y-%m-%d) $(date +%H:%M) \n $1 \n Something failed! D:"
else
	notify-send -i error -t 0 "Mirror & Archive Error" "$(date +%Y-%m-%d) $(date +%H:%M) \n $1 \n Does not exist."
fi

Offline

Be excellent to each other!

#2 2012-11-29 16:13:18

xaos52
The Good Doctor
From: Planet of the @s
Registered: 2011-06-24
Posts: 4,602

Re: Bash Website Mirror lftp

Offline

#3 2012-12-01 02:39:12

hozza
Member
From: /dev/null
Registered: 2012-11-20
Posts: 45
Website

Re: Bash Website Mirror lftp

Yes some argument validation would be nice, if any are missing then the script should not run.

If anything fails (except the notify-send command) then the script should stop and provide feedback via the notify-send command.

Can anyone help me understand how to do these? And could you explain your code example?

Offline

#4 2012-12-01 20:37:20

hozza
Member
From: /dev/null
Registered: 2012-11-20
Posts: 45
Website

Re: Bash Website Mirror lftp

I gave up tiring to get my head around bash for the moment as I'm on a tight deadline. So I wrote the mirror program I wanted in a language I know, it's only temporary, I know  PHP isn't meant for this kind of thing.

Here is is:

<?php
	//init
	parse_str(implode('&', array_slice($argv, 1)), $_GET);
	$error = array();

	$lPrefix 		 = '/home/hozza/Sites/';
	$archiveLocation = '/home/hozza/Backups/';

	$lDir = isset($_GET['l']) ? $_GET['l'] : $error[] = 'Local Directory Required';
	$rDir = isset($_GET['r']) ? $_GET['r'] : $error[] = 'Remote Directory Required';
	$bookmark = isset($_GET['b']) ? $_GET['b'] : $error[] = 'lftp Bookmark Required';

	//Check for args
	if(count($error) == 0) {
		$archiveName = end(explode('/', $lDir)) . '_' . date('Y-m-d_H-i');

		//Validate local dir
		if(is_dir($lPrefix . $lDir)) {
			
			//preserve Sublime Text 2 config SFTP files
			$ST2_SFTP_conf = false;
			if(file_exists($lPrefix . $lDir . '/sftp-config.json')) {
				$ST2_SFTP_conf = file_get_contents($lPrefix . $lDir . '/sftp-config.json');
				unlink($lPrefix . $lDir . '/sftp-config.json');
			}

			//Start mirror
			$lftOutput = explode("\n", shell_exec('lftp -e "mirror -e -p --parallel=10 --log=' . $archiveLocation . 'logs/' . $archiveName . '.txt ' . $rDir . '/ ' . $lPrefix . $lDir . '/; exit top" ' . $bookmark));
			
			//Tar regardless of lftp error or success
			$tarOutput = shell_exec('cd ' . $lPrefix . ' && tar -czf ' . $archiveLocation . $archiveName . '.tar.gz ' . $lDir);
			
			//Output completion or errors
			shell_exec('notify-send -i gnome-network-properties -t 0 "Mirror & Archive Complete" "' . $archiveName . '\n\n' . implode('\n', $lftOutput) . $tarOutput . '"');

			//put back ST2 SFTP conf
			if($ST2_SFTP_conf != false) file_put_contents($lPrefix . $lDir . '/sftp-config.json', $ST2_SFTP_conf);
		}
		else shell_exec('notify-send -i error -t 0 "Mirror & Archive Error" "' . date('Y-m-d') . ' ' . date('H-i') . '\n' . $lDir . ' \n Does not exist! D:"');
	}
	else shell_exec('notify-send -i error -t 0 "Mirror & Archive Error" "' . date('Y-m-d') . ' ' . date('H-i') . '\n' . implode('\n', $error) . '"');
?>

Same args etc as before! smile

~/Programs/mirror.php l=/local/path r=/remote/path b=mysite

Last edited by hozza (2012-12-01 20:46:03)

Offline

#5 2012-12-07 01:27:49

hozza
Member
From: /dev/null
Registered: 2012-11-20
Posts: 45
Website

Re: Bash Website Mirror lftp

how would I get lftp to output to terminal?

Offline

#6 2012-12-07 17:24:46

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: Bash Website Mirror lftp

lftp is an excellent program, but you might also consider a look at wget for mirroring a site. It has some useful options.

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: bleh

Debian Logo