Wednesday, March 27, 2013

Bacula Errors and Other Fun Stuff

So I recently upgraded PostgreSQL from 8.3 to 8.4 and more or less at the same time upgraded Bacula to 5.2.14 from some earlier version, I don't remember what can I say.

Anyway in the process I ran into this error when opening bconsole:

JobId 0: Fatal error: Pool Default not in database. sql_create.c:189 pool record Default already exists

I couldn't for the life of me figure this out.

So I eventually got into pgsql as bacula and did a select statement


bacula=> select * from pool
;
 poolid |  name   | numvols | maxvols | useonce | usecatalog | acceptanyvolume | volretention | voluseduration | maxvoljobs | maxvolfiles | maxvolbytes | autoprune | recycle | actiononpurge | pooltype | labeltype | labelformat | enabled | scratchpoolid | recyclepoolid | nextpoolid | migrationhighbytes | migrationlowbytes | migrationtime 
--------+---------+---------+---------+---------+------------+-----------------+--------------+----------------+------------+-------------+-------------+-----------+---------+---------------+----------+-----------+-------------+---------+---------------+---------------+------------+--------------------+-------------------+---------------
      1 | Default |      44 |     800 |       0 |          1 |               0 |      2592000 |              0 |         40 |           0 |           0 |         1 |       1 |             1 | Backup   |         0 | stor2-      |       1 |             0 |             0 |          0 |                  0 |                 0 |             0
      1 | Default |      44 |     800 |       0 |          1 |               0 |      2592000 |              0 |         40 |           0 |           0 |         1 |       1 |             1 | Backup   |         0 | stor2-      |       1 |             0 |             0 |          0 |                  0 |                 0 |             0
(2 rows)

As you can see my default pool is listed twice, oh man...

So I couldn't think of a clever way to fix that so I did a Google Search which led me to Stack Overflow 

http://stackoverflow.com/questions/1746213/how-to-delete-duplicate-entries-in-postgresql

and on to this function

CREATE OR REPLACE FUNCTION remove_duplicates(text, text) RETURNS void AS $$
DECLARE
  tablename ALIAS FOR $1;
  duplicate_column ALIAS FOR $2;
BEGIN
  EXECUTE 'CREATE TEMPORARY TABLE _DISTINCT_' || tablename || ' AS (SELECT DISTINCT ON (' || duplicate_column || ') * FROM ' || tablename || ' ORDER BY ' || duplicate_column || ' ASC);';
  EXECUTE 'DELETE FROM ' || tablename || ';';
  EXECUTE 'INSERT INTO ' || tablename || ' (SELECT * FROM _DISTINCT_' || tablename || ');';
  EXECUTE 'DROP TABLE _DISTINCT_' || tablename || ';';
  RETURN;
END;
$$ LANGUAGE plpgsql;
Of course I also had to run CREATE LANGUAGE plpgsql; first
Then I ran 
bacula=> SELECT remove_duplicates('pool','poolid');
 remove_duplicates 
-------------------
 
(1 row)

bacula=> select * from pool
;
 poolid |  name   | numvols | maxvols | useonce | usecatalog | acceptanyvolume | volretention | voluseduration | maxvoljobs | maxvolfiles | maxvolbytes | autoprune | recycle | actiononpurge | pooltype | labeltype | labelformat | enabled | scratchpoolid | recyclepoolid | nextpoolid | migrationhighbytes | migrationlowbytes | migrationtime 
--------+---------+---------+---------+---------+------------+-----------------+--------------+----------------+------------+-------------+-------------+-----------+---------+---------------+----------+-----------+-------------+---------+---------------+---------------+------------+--------------------+-------------------+---------------
      1 | Default |      44 |     800 |       0 |          1 |               0 |      2592000 |              0 |         40 |           0 |           0 |         1 |       1 |             1 | Backup   |         0 | stor2-      |       1 |             0 |             0 |          0 |                  0 |                 0 |             0
(1 row)

Now there are no more messages in bconsole and no more errors!

Remove Stale/Old Active Sync Devices from All Mailboxes

I had an issue where several users that no longer work here still had active sync devices in AD for whatever reason so I needed to get rid of them.

Quick Google Search found this Technet thread

The long and short of it are these two lines that gather all the active sync devices that haven't checked in within the past 30 days and then removes them.

For my purposes I changed it 90 days the first time through, however I now run this script once monthly.

-- Powershell Start --

$DevicesToRemove = Get-ActiveSyncDevice -result unlimited | Get-ActiveSyncDeviceStatistics | where {$_.LastSuccessSync -le (Get-Date).AddDays("-30")}

$DevicesToRemove | foreach-object {Remove-ActiveSyncDevice ([string]$_.Guid) -confirm:$false}

-- Powershell End --

Now let's say the user doesn't have a mailbox on Exchange anymore.

You first have to temporarily recreate the mailbox

-- Powershell Start --

Enable-Mailbox -Identity:'OU/User'

-- Powershell End --

Then rerun the above script.

If you don't create the mailbox you get a can't find recipient error.

Then after the active sync devices are gone simply run

-- Powershell Start --

Remove-Mailbox -Identity:'OU/User'

-- Powershell End --

If for some reason you still can't remove the Active Sync devices you can open ADSI Edit and look for

CN=ExchangeActiveSyncDevices container under the user object

Then simply remove that.