Replicate a Firebase Real-Time Database with a simple Perl script

Here is a very simple script to replicate data from one Firebase RTD to a backup RTD. Just be sure to modify the array of root keys (@firebaseBackupKeys) to reflect your database. This version can be called from a web browser, or via a cron job.

#!/usr/bin/perl


use CGI::Carp qw(fatalsToBrowser);
print 'Content-type: text/html', "\n\n";
use Firebase;



# Get a connection to Firebase;
$fb = Firebase->new(
	firebase => 'SourceFirebase',

	);

# Backup Firebase Database:
$fbb = Firebase->new(
	firebase => 'DestinationFirebase',
	);

@firebaseBackupKeys = ('config', 'devices', 'purchases',
'subscribers', 'subscriptionOffers', 'subscriptions' );



&FirebaseToFirebase();



sub getFirebaseItem {

	my ($firebaseObject, $firebaseParentPath) = @_;

	# Returns a ref to a hash:
	return $firebaseObject->get($firebaseParentPath);


}


sub putFirebaseItem {

	my ($firebaseObject, $path, $hashRefParams) = @_;

	$firebaseObject->put($path, $hashRefParams);

}


sub FirebaseToFirebase {

	my $messages = "";

	foreach $dbKey (@firebaseBackupKeys){

	    # Replicate all of the data from the initial firebase $fb to the backup $fbb
	    $childOfKey = &getFirebaseItem(
	                $fb,
	                "/".$dbKey
	                );
        $messages  = $messages . "<p>Received $dbKey </p>";

	    &putFirebaseItem($fbb, "/".$dbKey, $childOfKey );

        $messages  = $messages . "<p>Replicated $dbKey </p>";

	}

	print($messages);



} 

Leave a Reply

Your email address will not be published. Required fields are marked *