This paper is devoted to solve a backward problem for a time-fractional diffusion equation by a variational method. The regularity of a weak solution for the direct problem as well as the existence and uniqueness of a weak solution for the adjoint problem are proved. We formulate the backward problem into a variational problem by using the Tikhonov regularization method, and obtain an approximation to the minimizer of the variational problem by using a conjugate gradient method. Four numerical examples in one-dimensional and two-dimensional cases are provided to show the effectiveness of the proposed algorithm.Mathematics Subject Classification. 65M32, 35R11.with the homogeneous Dirichlet condition and initial condition u(x, t) = 0, x ∈ ∂Ω, t ∈ (0, T ),(1.2)