Instead of using a cursor directly against sys.databases, try putting the rows into a #temp table first. Also, try adding FAST_FORWARD option to the cursor just to be sure.
If you have the time to explore another more reliable option, look at the backup solution provided at http://ola.hallengren.com.
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato