Upon the allowance of bail and the execution and approval of the undertaking, the magistrate must, if the defendant is in custody, make and sign an order for his discharge, upon the delivery of which to the proper officer the defendant must be discharged.