Can I use a transistor to switch to battery backup power when main power goes out?
Yes, you should be aware of several things, and no, it will not work as you intend.
Let's start with step one: A transistor, NPN or PNP, used as a switch should always have a base resistor. If you're using simple types, stick to 510 Ohm or more. In many cases 5kOhm or 10kOhm will do fine. Because the bipolar transistor is a current amplifier and it wants to keep its base at a fixed voltage (about 0.7V away from emitter for simple types, up to 4V for darlingtons and other &quot;specials&quot;), so any voltage you apply will cause current to flow into the base and if that is not limited by a resistor: Poof! Transistor dead.
(An answer I wrote to design an on/off circuit using transistors, it skips a few important characteristics of transistors, but for your first experiments with NPN and PNP transistors as switches, if you have the time, you may find it interesting)
Step two is: Your 5V will always be 4V below the 9V, so with the extra resistor you have actually made an emitter follower that sort of &quot;demands&quot; a certain current to flow through dictated by the battery voltage and the resistor. How that works exactly is something for another time, as I expect that to become way too complex right now (more complex than the link there ^^).
Step three is: You don't need that many NPN transistors. You might need one to prevent the battery from powering something else on the 5V rail, but if your circuit is as tollerant as the LED you drew, that can be fixed differently. Current from a battery will always flow back into the battery. Current only ever goes in loops, it cannot just wander off into the world. As I said, it may &quot;loop through other parts&quot;, but it cannot just meander off into the world through just the VCC or the GND.
The last step is:
EDIT: Assuming you want the 9V directly as backup, that is. Which you probably don't judging by the comments and their progression. 9V will kill an arduino and no fixed resistor will help you there. See below for further edited updates



simulate this circuit &ndash; Schematic created using CircuitLab
D1 prevents current from the back-up battery to flow into the main power or the other devices, but does lower the voltage for the LED a little. If you use a 1N4148 (maximum current = 100mA, keep that in mind!) it will cost about 0.7V. You can also use a Schottky diode, that usually costs between 0.15V to 0.4V depending on the type you pick and at what effective current.
It should not leak too much, so don't pick a 100A diode, or the reverse leakage could turn off your battery and cause some high-frequency oscillations. (Just assume that as a fact for now).
Now, if the Main Supply now falls off, the current into the base of Q3 will decrease to a point that Q3 turns off. Q3 was on while Main Power was there and that caused it to pull down the base of Q2, in turn turning it off. With Q2 turned off the base of Q1 was pulled high by R7, so that was off too.
If now Q3 is turned off, the Q2 will suddenly get current into its base through R4 (make sure R4 is powered by the battery, so before Q1!). This turns on Q2, which then pulls the base of Q1 low, pulling current out of that base, allowing Q1 to turn on. You then have backup power.
The balance between R5 and R6 will have some influence on when the battery will turn on, if you need it to turn on sooner because you see the LED turning off, you need to increase R5 or decrease R6. If you need it to turn on later, increase R6 or decrease R5. Try to keep R5 and R6 between 1k and 100k if at all possible.
D2 is just there as an extra protection. It should in normal cases not be needed, but because bipolar transistors can act very strangely if the battery gets shorted or reversed, it's smart to add it anyway. Better safe than sorry.
Of course, R4 will cause there to be a leakage current of 90μA at all times while the Main Power is on (because Q3 shorts that resistor to ground when the Main Power is on). If you need that leakage to be much lower further tricks are needed, but with a standard 9V alkaline, I'm betting that 90μA will not make the biggest difference on its lifespan.
EDIT: Solution as suggested in the comments, now knowing that you are powering an Arduino:



simulate this circuit
As long as the battery voltage is lower than the main power and the diodes are of the same type, you will normally be drain 99.9999% (0.0001% for leakage from the batteries) of your power from the Main Power. When the Main Power falls away you get 100% from the batteries with no delay at all, since the diodes will cross over smoothly from one source to the next.
When your batteries are at 4V-ish (not really empty yet, but it'll take some time before they get there) the supply may fall below 3.3V that the arduino likes.
If you then use Schottky diodes with a 0.1V forward voltage that'll take much longer.
You could also use a 5V fixed voltage regulator after a 9V battery, but you should then find one with a low quiescent current and all that, the 3x AA option, in all cases, is simply the easiest. Especially if you need only small amounts of back-up power and can afford to check and change the batteries regularly as should be the case with a door lock.