Skip to content

Instantly share code, notes, and snippets.

@Tatsh
Last active February 10, 2023 04:47
Show Gist options
  • Save Tatsh/48e37aa7a9f1e76f8a4fa5eaf04a5106 to your computer and use it in GitHub Desktop.
Save Tatsh/48e37aa7a9f1e76f8a4fa5eaf04a5106 to your computer and use it in GitHub Desktop.

NOTE: Requires a backup of your old Chrome configuration directory.

The intent of this script is to save time by not having to reconfigure your extensions when you sign in with a clean Chrome config, and to transfer general things like cookies so you do not have to sign into everything again. This also copies flags you have enabled in chrome://flags.

After deleting your old configuration from the normal location (e.g. ~/.config/google-chrome-beta), open Chrome and sign in. Do this for every profile as necessary. You need to determine which profile directory ('Default' or 'Profile X') belongs to whom in both the old config and the new. Then you can use this script.

Example usages:

./reconstruct-chrome-profile.sh ./old-chrome-config-backup Default Default
./reconstruct-chrome-profile.sh ./old-chrome-config-backup 'Profile 3' 'Profile 2'
#!/usr/bin/env bash
finish() {
if [ -z "$scratch" ] && [ -d "$scratch" ]; then
rm -fR "$scratch"
fi
local -r rv=$?
if ((rv == 0)); then
exit 0
fi
echo 'Error occurred!' >&2
exit "$rv"
}
trap finish EXIT INT QUIT TERM
main() {
local -r old_config_dir="$1"
local -r old_profile_name="$2"
local -r new_profile_name="$3"
scratch=$(mktemp -d -t tmp.XXXXXXXXXX)
local i
if [ -z "$old_config_dir" ] ||
[ -z "$old_profile_name" ] ||
[ -z "$new_profile_name" ]; then
echo "Usage: $0 OLD_CONFIG_DIR OLD_PROFILE_NAME NEW_PROFILE_NAME"
exit 1
fi
killall -w chrome
set -e
jq --argfile old "${old_config_dir}/Local State" \
'.browser.enabled_labs_experiments = $old.browser.enabled_labs_experiments' \
"${HOME}/.config/google-chrome-beta/Local State" > "${scratch}/new.json"
cp -f "${scratch}/new.json" "${HOME}/.config/google-chrome-beta/Local State"
for i in 'Local Extension Settings' IndexedDB 'Local Storage' 'Session Storage' \
'Managed Extension Settings' Storage databases WebStorage 'Extension Rules' \
'Extension State' 'Extension Scripts'; do
rsync --force -rltd \
"${old_config_dir}/${old_profile_name}/${i}/" \
"${HOME}/.config/google-chrome-beta/${new_profile_name}/${i}"
done
for i in 'Affiliation Database' Cookies 'Extension Cookies' History 'Login Data' \
'Login Data For Account' 'Network Action Predictor' 'Safe Browsing Cookies' 'Shortcuts' \
'Top Sites' 'Web Data'; do
cp -f \
"${old_config_dir}/${old_profile_name}/${i}" \
"${old_config_dir}/${old_profile_name}/${i}-journal" \
"${HOME}/.config/google-chrome-beta/${new_profile_name}/"
done
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment