🔥 OVH fire disaster: file storage lost, database preserved, how to recover?

prepare.php

<?php

$sql_host     = '127.0.0.1';
$sql_port     = 5432;
$sql_database = 'nextcloud';
$sql_user     = 'nextcloud';
$sql_password = 'nextcloud';

$PDO = new PDO( "pgsql:host={$sql_host};port={$sql_port};dbname={$sql_database};user={$sql_user};password={$sql_password}" );
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$PDO->beginTransaction();

// 1. Empty trashes
$PDO->exec( '
	TRUNCATE oc_files_trash
' );

// 2. Delete trash and versionned files
$PDO->exec( "
	DELETE FROM oc_filecache 
	WHERE 	path like 'files_trashbin/files/%'
		OR  path like 'files_trashbin/keys/%'
		OR	path like 'files_trashbin/versions/%'
		OR  path like 'files_versions/%'
" );

$PDO->exec( "
	DELETE FROM oc_filecache_extended
	WHERE fileid NOT IN (SELECT fileid FROM oc_filecache)
" );

// 3. Check data directory
$PDOStatement = $PDO->query( "
	SELECT id
	FROM oc_storages
	WHERE id like 'local::%'
" );

if ( false !== $id = $PDOStatement->fetchColumn() ) {
	$root_path = substr( $id, 7 );
	echo "ROOT: $root_path\n";
	if ( ! is_writable( $root_path ) ) {
		die( "Unable to write in {$root_path}" );
	}
}

// 4. Create user directories
$PDOStatement = $PDO->query( "
	SELECT 	m.root_id,
	    	m.mount_point
	FROM oc_mounts m
	LEFT outer join oc_share s
	 ON s.file_source = m.root_id
	WHERE s.file_source IS NULL
" );

$PDOStatementFiles = $PDO->prepare( "
	WITH RECURSIVE files AS (
	    SELECT  p.fileid,
	            p.path,
	            p.storage_mtime,
	            p.parent,
	            p.mimetype
	    FROM oc_filecache p
        WHERE 	p.fileid = :root

        UNION

        SELECT  c.fileid,
                c.path,
                c.storage_mtime,
                c.parent,
                c.mimetype
        FROM oc_filecache c
        INNER JOIN files f
        ON 	c.parent = f.fileid
	)

	SELECT f.path, m.mimetype, f.storage_mtime
	FROM files f
	INNER JOIN oc_mimetypes m
	ON m.id = f.mimetype;
" );

while ( false !== $mount_data = $PDOStatement->fetch(PDO::FETCH_ASSOC) ) {
	$PDOStatementFiles->execute( [ 'root' => $mount_data['root_id'] ] );
	while ( false !== $file_data = $PDOStatementFiles->fetch( PDO::FETCH_ASSOC ) ) {

		if ( ! file_exists( $fullpath = "{$root_path}{$mount_data['mount_point']}{$file_data['path']}" ) ) {
			if ( $file_data['mimetype'] == 'httpd/unix-directory' ) {
				echo "mkdir $fullpath\n";
				mkdir( $fullpath, 0755 );
				touch( $fullpath, $file_data['storage_mtime'] );
			} else {
				echo "touch $fullpath\n";
				touch( $fullpath, $file_data['storage_mtime'] );
				chmod( $fullpath, 0644 );
			}
		}
	}
}

$PDO->exec('
	CREATE TABLE oc_share_temp(
	    id BIGINT,
	    permissions SMALLINT
	);
');

$PDO->exec('
	INSERT INTO oc_share_temp(id, permissions)
	SELECT  id, permissions from oc_share
');

$PDO->exec('
	UPDATE oc_share 
	SET permissions = permissions | 2
');

$PDO->commit();

share_restore.php

<?php

$sql_host     = '127.0.0.1';
$sql_port     = 5432;
$sql_database = 'nextcloud';
$sql_user     = 'nextcloud';
$sql_password = 'nextcloud';

$PDO = new PDO( "pgsql:host={$sql_host};port={$sql_port};dbname={$sql_database};user={$sql_user};password={$sql_password}" );
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$PDO->beginTransaction();

if ($argc !=2 ) {
	die ("USAGE: " . __FILE__ . " <username>\n");
}

$PDOStatement = $PDO->prepare("
	SELECT count(*)
	FROM oc_accounts
	WHERE uid = :uid
");

$PDOStatement->execute(['uid' => $argv[1]]);

if (1 !== $PDOStatement->fetchColumn()) {
	die("Unknown user\n");
}

$PDOStatement = $PDO->prepare("
	UPDATE ONLY oc_share s
	SET permissions = t.permissions
	FROM oc_share_temp t
	WHERE t.id = s.id
	AND s.uid_owner = :owner
");

$PDOStatement->execute(['owner' => $argv[1]]);

$PDO->commit();

I had to face the same problem not related to OVH though. I performed a fresh NC install, recreated as fresh the users and groups based on the data base backup. Created passwords identical to originals by copying hash value (SL update) stored in Database.
Made a copy of Synch files safely on each PC’s, launched the NC Sync application as fresh (means you are required to connect to NC to get the dongle), file sharing was only through groups.
I applied it for 2 users, we had some files in conflicts but not that much. The other 5 users will come on line later on.