How do you backup a database within SQL Server 2005 from the command line?

Solution 1:

OSQL -E -Q "BACKUP DATABASE database_name TO DISK = 'path and filename'"

As in:

OSQL -E -Q "BACKUP DATABASE master TO DISK = 'C:\master.bak'"

Be sure you're running that as a user who has rights to backup that database (or, alternatively, use the "-U" and "-P" arguments to supply a username/password on the command line... not a good idea, thought.)

Solution 2:

Actually better here is to use SQLCMD instead of OSQL. OSQL is considered deprecated as of SQL Server 2005, though it's still around in SQL Server 2008.