#!/usr/bin/perl
#
# automatically backup a Google Mini's config file
# Add this to cron to backup your Google Mini:
# 0 1 * * * /path/to/minibackup.pl
#
# Author: Matt Dunford <zoot@zotikos.com>
# License: GPL

use WWW::Mechanize;
use IO::Socket::SSL;
use DateTime;

$dest = '/backups/miniconfigs';  # dir to save the backups
$base = 'https://mini.mars.planet:8443/';  # needs ending '/'
$username = '';  # usually admin
$passwd = '';

###

die "the directory $dest doesn't exist\n" if (!-d $dest);

$base = $base . '/' if ($base !~ /\/$/);

my $mech = WWW::Mechanize->new(autocheck => 1);
$mech->agent_alias('Windows IE 6');

$url = $base . 'EnterpriseController';
$mech->get($url);
$mech->reload();

$mech->form_name('login');
$mech->field('userName', $username);
$mech->field('password', $passwd);
$mech->click();

$url = $base . 'EnterpriseController?actionType=importExport';
$mech->get($url);
$mech->reload();

$mech->form_name('headersSetup');
$mech->field('password1', $passwd);
$mech->field('password2', $passwd);
$mech->click('export');

$dt = DateTime->now();
$date = $dt->day . '-' . $dt->month . '-' . $dt->year;
$file = "$dest/config.xml-$date";

open(OUT, ">$file");
print OUT $mech->content;
close OUT;

