The Spanish champions have not won in San Sebastian since 2007 and needed a Lionel Messi strike to earn a point in their league visit in November.
They will get a chance to end that winless run of eight matches on January 17, though, before the return leg at Camp Nou a week later.
The leaders made it a record 40 games unbeaten as they battled back to earn a 3-3 draw in the second leg of the last round against Sevilla on Thursday - eclipsing Barcelona's previous best of 39.
Real's city rivals Atletico Madrid are also into the latter stages of this year's competition after beating Las Palmas 4-3 on aggregate.
Copa del Rey draw in full:
Real Sociedad v Barcelona
Alcorcon v Alaves
Atletico Madrid v Eibar
Real Madrid v Celta Vigo