If the dog is attacking/acting aggressively and you ride or run away it’ll stimulate his chase instinct and he might go after you. If you’re static your best bet is to back away slowly, try not to stare at the dog or bare your teeth. If it looks like the dog is going to attack, you might try to scold the dog, depending on its size and aggression levels.
If getting attacked is inevitable, then it’s obviously beneficial to have a weapon to hand. If possible use it to keep the dog at a distance. Aim for the nose and the eyes. There are loads of things which might make a good weapon – a pump, seat post or a stick can be used to deter the dog, to keep it at a distance. A multi-tool, Swiss army knife or similar will make a good stabbing or gouging weapon, even car keys can be used to this end.
If things really go south and a dog starts biting you then depending on the size of the dog you could be in a lot of trouble. Whatever you do – try to stay on your feet. If you are anticipating the attack, if possible wrap a jacket or similar around your forearm. Often dogs will go for the throat. Use your forearm to protect your throat and hopefully he will bite down on it and stay like that.
If the dog latches onto your trouser leg / or your leg – good. Let it hold on. Pick your moment and gouge its eyes. If you have a weapon in your hand then use it. Be relentless – keep attacking until you are sure the dog has got the message – most dogs when riled can take a lot of punishment.
If a dog gets hold of your arm, you might have to accept there is a possibility it’ll get broken, depending on the size of the dog. Again this is a good opportunity to attack the dog. Blind it, beat it, stab it. If possible lift it by the tail – it’ll relinquish its grip. Again, whatever you do, dont let it drag you to the ground.
One technique which works is to force your fist down the dogs throat – this will discourage it, and make the dogs only weapon useless. Leaving you free to incapacitate it as you see fit.