2020-10-30 14:08:56 -04:00
---
2021-01-11 10:10:32 -05:00
stage: Enablement
group: Distribution
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-30 14:08:56 -04:00
---
2021-04-19 14:09:09 -04:00
# Restoring from backup after a failed upgrade **(FREE SELF)**
2016-03-08 17:52:48 -05:00
Upgrades are usually smooth and restoring from backup is a rare occurrence.
However, it's important to know how to recover when problems do arise.
## Roll back to an earlier version and restore a backup
In some cases after a failed upgrade, the fastest solution is to roll back to
2021-04-22 23:09:40 -04:00
the previous version you were using. We recommend this path because the failed
2021-08-16 20:10:22 -04:00
upgrade might have made database changes that cannot be readily reverted.
2016-03-08 17:52:48 -05:00
First, roll back the code or package. For source installations this involves
checking out the older version (branch or tag). For Omnibus installations this
2020-06-22 05:08:42 -04:00
means installing the older
[`.deb` or `.rpm` package ](https://packages.gitlab.com/gitlab ). Then, restore from a
backup.
2016-03-08 17:52:48 -05:00
Follow the instructions in the
2020-04-21 11:21:10 -04:00
[Backup and Restore ](../raketasks/backup_restore.md#restore-gitlab )
2016-03-08 17:52:48 -05:00
documentation.
## Potential problems on the next upgrade
When a rollback is necessary it can produce problems on subsequent upgrade
attempts. This is because some tables may have been added during the failed
upgrade. If these tables are still present after you restore from the
older backup it can lead to migration failures on future upgrades.
Starting in GitLab 8.6 we drop all tables prior to importing the backup to
prevent this problem. If you've restored a backup to a version prior to 8.6 you
2021-08-16 20:10:22 -04:00
may have to manually correct the problem next time you upgrade.
2016-03-08 17:52:48 -05:00
Example error:
2020-03-02 22:08:31 -05:00
```plaintext
2016-03-08 17:52:48 -05:00
== 20151103134857 CreateLfsObjects: migrating =================================
-- create_table(:lfs_objects)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DuplicateTable: ERROR: relation "lfs_objects" already exists
```
Copy the version from the error. In this case the version number is
`20151103134857` .
2021-08-16 20:10:22 -04:00
WARNING:
Use the following steps only if you are certain you must do them.
2016-03-08 17:52:48 -05:00
### GitLab 8.6+
2020-03-31 08:08:09 -04:00
Pass the version to a database Rake task to manually mark the migration as
2016-03-08 17:52:48 -05:00
complete.
2020-03-02 22:08:31 -05:00
```shell
2016-03-08 17:52:48 -05:00
# Source install
sudo -u git -H bundle exec rake gitlab:db:mark_migration_complete[20151103134857] RAILS_ENV=production
# Omnibus install
sudo gitlab-rake gitlab:db:mark_migration_complete[20151103134857]
```
2021-08-16 20:10:22 -04:00
After the migration is successfully marked, run the Rake `db:migrate` task again.
Repeat this process until all failed migrations are complete.
2016-03-08 17:52:48 -05:00
### GitLab < 8.6
2020-03-02 22:08:31 -05:00
```shell
2016-03-08 17:52:48 -05:00
# Source install
2020-02-28 01:09:19 -05:00
sudo -u git -H bundle exec rails console -e production
2016-03-08 17:52:48 -05:00
# Omnibus install
sudo gitlab-rails console
```
At the Rails console, type the following commands:
2020-03-02 22:08:31 -05:00
```ruby
2016-03-08 17:52:48 -05:00
ActiveRecord::Base.connection.execute("INSERT INTO schema_migrations (version) VALUES('20151103134857')")
exit
```
2021-08-16 20:10:22 -04:00
After the migration is successfully marked, run the Rake `db:migrate` task again.
Repeat this process until all failed migrations are complete.