How to backup, restore and migrate


My gogs version is :

Gogs version

(the version number is the same as the latest).
I unziped the backup file,the sessions dir are included.

Thank you very much for your help and patience.


Thanks! I think you can file an issue on GitHub as a record, because from the code backup should only include “attachments” and “avatars” subdirs under “data” directory.


Hello @all.

I’m new to Gogs, also to git. So my question may be somehow strange. But I’ve got a curious problem. I’ve setup Gogs on Win 7 Pro 64 bit. It’s running as a service on this Windows box. Then I’ve wrote a simple batch file to get a backup of Gogs configs and also the repositories. The problem: The batch works fine when I’m logged on to the system. But running exactly the same batch from task scheduler, it looks like that the command gogs.exe backup will not create the backup in non-interactive mode.

First I thought it may caused by the user. But I’ve testet this with several users with the same result…

Does anybody here got the same experience? Or does anybody know any hints to solve the problem?



PS: The batch looks like this one

@echo off
cd \gogs
d:\gogs\gogs.exe backup
xcopy gogs-backup-*.zip \\server\development\backup\gogs\ /c /v /y /j
del /f /q gogs-backup-*.zip
cd \


Sorry, forgot most important information: I’m running Gogs in version


[root@switch-subversion /home/git]# su - git -c "/opt/gogs/gogs restore"
2017/09/19 10:56:33 [ INFO] Restore backup from:
2017/09/19 10:56:40 [FATAL] Fail to import ‘custom’: rename /tmp/gogs-backup/custom /opt/gogs/custom: invalid cross-device link


invalid cross-device link
mkdir /opt/gogs/tmp
chown git:git /opt/gogs/tmp

[FATAL] Fail to extract ‘’: open 省略: permission denied
chmod u+w -R /opt/gogs-repositories/

chown git:git /opt/gogs
mkdir /opt/gogs/tmp
chown git:git /opt/gogs/tmp
mv /opt/gogs/custom.bak /root/recycle_bin/
chmod u+w -R /opt/gogs-repositories/
su - git -c “/opt/gogs/gogs restore --config=/opt/gogs/custom/conf/app.ini --tempdir=/opt/gogs/tmp”



su - git -c "/opt/gogs/gogs backup -h"
gogs backup - Backup files and database

gogs backup [command options] [arguments…]

Backup dumps and compresses all related files and database into zip file,
which can be used for migrating Gogs to another server. The output format is meant to be
portable among all supported database engines.

–config value, -c value Custom configuration file path (default: “custom/conf/app.ini”)
–verbose, -v Show process details
–tempdir value, -t value Temporary directory path (default: “/tmp”)
–target value Target directory path to save backup archive (default: “./”)
–archive-name value Name of backup archive (default: “”)
–database-only Only dump database
–exclude-repos Exclude repositories

su - git -c “/opt/gogs/gogs backup --tempdir /opt/gogs/tmp --target /opt/gogs_bak/”

su - git -c "/opt/gogs/gogs restore -h"
gogs restore - Restore files and database from backup

gogs restore [command options] [arguments…]

Restore imports all related files and database from a backup archive.
The backup version must lower or equal to current Gogs version. You can also import
backup from other database engines, which is useful for database migrating.

If corresponding files or database tables are not presented in the archive, they will
be skipped and remain unchanged.

–config value, -c value Custom configuration file path (default: “custom/conf/app.ini”)
–verbose, -v Show process details
–tempdir value, -t value Temporary directory path (default: “/tmp”)
–from value Path to backup archive
–database-only Only import database
–exclude-repos Exclude repositories


I’ve done a successful backup, but now trying to restore it and getting this.

2 things:

  • I’m performing the backup on one instance, and restoring it on another (new, empty) instance
  • both instances are running in Docker containers.

a25ff52a1650:/app/gogs$ ./gogs restore --from /data/gogs
a25ff52a1650:/app/gogs$ ./gogs restore --from /data/
2017/09/19 19:37:01 [ INFO] Restore backup from: /data/
2017/09/19 19:37:27 [FATAL] Fail to import database: fail to insert strcut: database disk image is malformed


On Ubuntu 16.04 using the build, I had to do a few tweaks, some undocumented, to be able to perform a backup. Here is the command line I’ve had to use:

sudo -u gogs HOME=/var/www/gogs GOGS_CUSTOM=/etc/gogs /opt/gogs/gogs backup -v --exclude-repos

  • Have to sudo, because gogs is not allowed to login.
  • Have to set HOME because com.HomeDir() returns the root user’s home directory, but allows overriding it that way
  • Have to set GOGS_CUSTOM as explained on issue #2924 because sets the custom directory to /etc/gogs in its /etc/gogs/conf.d/other file.

At this point, the backup looks good for the DB and conf, attachments and avatars (and skips sessions).

When doing it in crontab, I have to wrap it in a script, and add USER=gogs so that the script looks like:


export APP_DIR=/opt/gogs
export USER=gogs
export HOME=/var/www/gogs
export GOGS_CUSTOM=/etc/gogs
export TARGET=$HOME/backups

/opt/gogs/gogs backup -v --exclude-repos --target $TARGET
ls -l $TARGET
  • The APP_DIR vs TARGET difference was needed because when gogs backup is not run from the gogs directory, the data directory is skipped

  • The USER was needed, because without it, when running from the gogs user crontab, gogs backup fails with an error like:

    2018/04/25 12:35:01 [FATAL] […g/setting/setting.go:571 NewContext()] Expect user ‘gogs’ but current user is:

(yes current user is empty)


is can migrate another OS?

ex) Backup from Windows Migrate to Linux(Cent OS)


Yes, you should be able to.