Skip to content

Instantly share code, notes, and snippets.

@lucianoratamero
Last active May 5, 2023 06:48
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucianoratamero/bc66b1a370b9dc030674ff89c39ac87c to your computer and use it in GitHub Desktop.
Save lucianoratamero/bc66b1a370b9dc030674ff89c39ac87c to your computer and use it in GitHub Desktop.
Export Gnome settings and extensions python script

migrate_gnome_settings.py

This script makes it easier to copy Gnome settings and extensions from one pc to the other. It supports python>=3.5.

To use it, download it, then run python3 migrate_gnome_settings.py --export-settings to create a tar.gz file with all the settings.

Then, on your new system, copy the script and the gzip to your user's home directory and run python3 migrate_gnome_settings.py --import-settings.

For now, the script migrates:

  • icons;
  • themes;
  • wallpapers;
  • extensions;
  • keybindings;
  • firefox settings;
  • most app settings.

Note: if you choose to export firefox settings, DO NOT share your configuration with anyone, since it WILL contain private data.

#! /usr/bin/env python
import argparse
import getpass
import os.path
import subprocess
import tarfile
REL_GNOME_EXTENSIONS_DIR = '/.local/share/gnome-shell/extensions/'
REL_GNOME_BGS_DIR = '/.local/share/backgrounds/'
REL_GNOME_ICONS_DIR = '/.icons/'
REL_GNOME_THEMES_DIR = '/.themes/'
REL_FIREFOX_SETTINGS_DIR = '/.mozilla/'
GNOME_EXTENSIONS_DIR = os.path.expanduser('~') + REL_GNOME_EXTENSIONS_DIR
GNOME_BGS_DIR = os.path.expanduser('~') + REL_GNOME_BGS_DIR
GNOME_ICONS_DIR = os.path.expanduser('~') + REL_GNOME_ICONS_DIR
GNOME_THEMES_DIR = os.path.expanduser('~') + REL_GNOME_THEMES_DIR
FIREFOX_SETTINGS_DIR = os.path.expanduser('~') + REL_FIREFOX_SETTINGS_DIR
DCONF_SETTINGS_FILENAME = os.path.expanduser('~') + '/dconf-extensions-settings.dump'
TAR_FILENAME = os.path.expanduser('~') + '/gnome_settings.tar.gz'
def export_settings():
export_firefox_settings = input('>>>> Do you want to export firefox settings? The filesize will be WAY bigger. [y/N] ').lower()
print('>>>> Starting to export settings...')
dconf_settings = subprocess.run(['dconf', 'dump', '/'], capture_output=True, check=False).stdout
clean_dconf_settings = dconf_settings.replace(getpass.getuser().encode('utf8'), b'%%USER%%')
dconf_settings_dump = open(DCONF_SETTINGS_FILENAME, 'wb')
dconf_settings_dump.write(clean_dconf_settings)
with tarfile.open(TAR_FILENAME, "w:gz") as tar:
print('>>>> Exporting icons...')
tar.add(GNOME_ICONS_DIR, arcname=REL_GNOME_ICONS_DIR)
print('>>>> Exporting themes...')
tar.add(GNOME_THEMES_DIR, arcname=REL_GNOME_THEMES_DIR)
print('>>>> Exporting extensions...')
tar.add(GNOME_EXTENSIONS_DIR, arcname=REL_GNOME_EXTENSIONS_DIR)
print('>>>> Exporting wallpapers...')
tar.add(GNOME_BGS_DIR, arcname=REL_GNOME_BGS_DIR)
if export_firefox_settings == 'y':
print('>>>> Exporting firefox settings...')
tar.add(FIREFOX_SETTINGS_DIR, arcname=REL_FIREFOX_SETTINGS_DIR)
print('>>>> Exporting dconfs...')
tar.add(DCONF_SETTINGS_FILENAME, arcname='dconf-extensions-settings.dump')
subprocess.run(['rm', DCONF_SETTINGS_FILENAME], check=False)
print(f'>>>> Done! Check the tar created at {TAR_FILENAME}!')
if export_firefox_settings == 'y':
print('\n------------------------------ DISCLAIMER ------------------------------\n')
print('>>>> Since you chose to export firefox settings, your tar file WILL contain personal and sensitive data.')
print('>>>> DO NOT share it with anyone, and store it safely. :]')
def import_settings():
print('>>>> Starting to import settings...')
print('>>>> Unpacking static files...')
subprocess.run(['tar', '-xzf', TAR_FILENAME, '-C', os.path.expanduser('~')], check=True)
print('>>>> Importing dconfs...\n')
dconf_settings = open(DCONF_SETTINGS_FILENAME, 'rb').read()
subprocess.run(
['dconf', 'load', '-f', '/'],
input=dconf_settings.replace(b'%%USER%%', getpass.getuser().encode('utf8')),
check=False
)
subprocess.run(['rm', DCONF_SETTINGS_FILENAME], check=False)
print('\n>>>> Done! You may need to restart your Gnome session for settings to load (logout and login).')
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='This is a simple script to export and \
import all settings and extensions from and for a Gnome user.'
)
parser.add_argument(
'--export-settings',
action="store_true",
help="Exports a tar file with all settings and extensions."
)
parser.add_argument(
'--import-settings',
action="store_true",
help='''Imports all settings and extensions from a previously exported tar file.
This file should be located at your user\'s home directory.'''
)
args = parser.parse_args()
if args.export_settings:
export_settings()
elif args.import_settings:
import_settings()
@fqqf
Copy link

fqqf commented Dec 1, 2022

Thank you

@jojo2massol
Copy link

issue :
no catch if file not found :

>>>> Exporting icons...
Traceback (most recent call last):
  File "/home/eos/migrate_gnome_settings.py", line 98, in <module>
    export_settings()
  File "/home/eos/migrate_gnome_settings.py", line 35, in export_settings
    tar.add(GNOME_ICONS_DIR, arcname=REL_GNOME_ICONS_DIR)
  File "/usr/lib/python3.10/tarfile.py", line 1986, in add
    tarinfo = self.gettarinfo(name, arcname)
  File "/usr/lib/python3.10/tarfile.py", line 1865, in gettarinfo
    statres = os.lstat(name)
FileNotFoundError: [Errno 2] No such file or directory: '/home/eos/.icons/'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment