How do I write backup procedure I've read that a decent backup strategy would be:

1) Full backup weekly
2) Differential backups daily
3) Log backups every 5 minutes.


Now question is how do I write this stored procedure and create a sql job out of it. For eg. For differential backup, it is necessary that full backup must exist. Now when it is Monday 12 AM and it is time for both weekly and differential backup to run. It is possible that differential backup runs before full backup. How do I handle these tricky conditions? Could anybody please share the solution for this?
First, I would recommend you go get Ola Hallengren's backup scripts.  They are full featured and well documented, supporting exactly the type of operations you are looking to complete.  This saves you a lot of effort of writing your own scripts from scratch and the solution is recommended by many notables in the community.

Second, I would create SQL Agent jobs to call the backup stored procedures, with the following jobs:


Full backup job that runs weekly, creating full backups for all databases.  For argument's sake, this is at 2 AM Sunday.
Differential backup job that runs on all days other than the day of the full.  This would be 2 AM Monday-Saturday.
Log backup job that executes every 5 minutes.


This is a very basic and general schedule and you will probably need to tweak and adjust it based on your environment.  The largest area of adjustment will probably be how often you run your log backups.  Depending on the size, activity, the number of databases in FULL recovery mode, and the fact that this approach backs up your databases serially (one after the other), all log backups on your instance may not complete in 5 minutes.  There are several ways to handle this, such as increasing the window between log backups and creating different backup jobs for different databases (so you can have them execute in parallel).