Advertisement
stspringer

Bash Script to clone Firefox Profile to other PCs

Mar 26th, 2025 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. Hello Everyone,
  2. For Linux Mint users. I just thought I would share this script that will clone your Firefox Profile and YASD "Yet Another Speed Dial" add-on in Firefox to other PSs on your LAN. The main idea is if you have more than one PC on your LAN that run Firefox and they all use YASD you may easily clone from your Master PC, the one you sit at most often, to other PCs on your LAN. You will want to use just one "Master" PC to clone from, or clarity, have this script live on just your Master PC.
  3.  
  4. Also, optionally, you may add an alias to your .bashrc; this way, you can just type clonefirefox at the terminal and fire off the script that way.
  5.  
  6. This script uses SSH to work, so if you use SSH ports other than the default port "22", you will have to edit the port number in the script.
  7.  
  8. If you use Chrome on Linux, you may want to switch to Firefox; it is faster, fewer resources are used with Firefox.
  9.  
  10. HTH
  11.  
  12. #!/bin/bash
  13.  
  14. # === Firefox Profile Multi-PC Cloner (resilient version) ===
  15.  
  16. # === CONFIG ===
  17. REMOTE_PROFILE_DIR=".mozilla/firefox"
  18. TARGETS=(
  19. )
  20.  
  21. # === Auto-detect local profile ===
  22. LOCAL_PROFILE=$(awk -F= '/^Path/ && f {print $2; exit} /^\[Profile[0-9]+\]/ {f=1}' ~/.mozilla/firefox/profiles.ini)
  23. if [[ -z "$LOCAL_PROFILE" ]]; then
  24. echo "❌ Could not auto-detect local Firefox profile. Aborting."
  25. exit 1
  26. fi
  27. LOCAL_PROFILE_DIR="$HOME/.mozilla/firefox/$LOCAL_PROFILE"
  28.  
  29. # === Get local IPs/hostname to skip self ===
  30. LOCAL_IPS=($(hostname -I))
  31. LOCAL_HOSTNAME=$(hostname)
  32.  
  33. # === Create tarball ===
  34. cd "$HOME/.mozilla/firefox" || exit 1
  35. TAR_FILE="$HOME/firefox-clone.tar.gz"
  36. echo "$(date '+%F %T') πŸ“¦ Creating profile archive..."
  37. tar czf "$TAR_FILE" "$LOCAL_PROFILE" profiles.ini
  38.  
  39. # === Loop through targets ===
  40. for entry in "${TARGETS[@]}"; do
  41. IFS=':' read -r userhost port <<< "$entry"
  42. user=$(echo "$userhost" | cut -d@ -f1)
  43. host=$(echo "$userhost" | cut -d@ -f2)
  44.  
  45. # Skip self if IP or hostname matches
  46. if [[ "${LOCAL_IPS[*]}" =~ $host || "$LOCAL_HOSTNAME" == "$host" ]]; then
  47. echo "$(date '+%F %T') πŸ” Skipping self ($host)"
  48. continue
  49. fi
  50.  
  51. echo -e "\n$(date '+%F %T') πŸ”„ Cloning to $userhost (port $port)..."
  52.  
  53. SOCKET_PATH="/tmp/ssh_mux_$user@$host:$port"
  54.  
  55. # Try to establish SSH connection with timeout
  56. if ssh -p "$port" -o ConnectTimeout=5 -o ConnectionAttempts=1 -o ControlMaster=yes -o ControlPersist=300 -o ControlPath="$SOCKET_PATH" -Nf "$userhost" 2>/dev/null; then
  57.  
  58. echo "$(date '+%F %T') ❌ Closing Firefox on $host..."
  59. ssh -p "$port" -o ConnectTimeout=5 -o ControlPath="$SOCKET_PATH" "$userhost" "pkill -u \$USER firefox" 2>/dev/null
  60.  
  61. echo "$(date '+%F %T') πŸ“ Backing up existing profile on $host..."
  62. ssh -p "$port" -o ConnectTimeout=5 -o ControlPath="$SOCKET_PATH" "$userhost" \
  63. "mkdir -p ~/.mozilla/firefox/backups && cp -r ~/.mozilla/firefox/* ~/.mozilla/firefox/backups/profile_backup_$(date +%Y%m%d_%H%M%S)"
  64.  
  65. echo "$(date '+%F %T') ♻️ Rotating backups (keeping last 3)..."
  66. ssh -p "$port" -o ConnectTimeout=5 -o ControlPath="$SOCKET_PATH" "$userhost" \
  67. "cd ~/.mozilla/firefox/backups && ls -dt profile_backup_* 2>/dev/null | tail -n +4 | xargs -r rm -rf"
  68.  
  69. echo "$(date '+%F %T') πŸ“€ Sending archive..."
  70. scp -P "$port" -o ConnectTimeout=5 -o ControlPath="$SOCKET_PATH" "$TAR_FILE" "$userhost:~"
  71.  
  72. echo "$(date '+%F %T') πŸ“‚ Extracting profile on remote..."
  73. ssh -p "$port" -o ConnectTimeout=5 -o ControlPath="$SOCKET_PATH" "$userhost" \
  74. "rm -rf ~/.mozilla/firefox/* && mkdir -p ~/.mozilla/firefox && tar xzf ~/firefox-clone.tar.gz -C ~/.mozilla/firefox/"
  75.  
  76. echo "$(date '+%F %T') βœ… Done with $userhost"
  77.  
  78. # Close SSH connection
  79. ssh -O exit -o ControlPath="$SOCKET_PATH" "$userhost" 2>/dev/null
  80. else
  81. echo "$(date '+%F %T') ⚠️ Skipping $userhost: SSH connection failed or timed out."
  82. fi
  83. done
  84.  
  85. # === Clean up ===
  86. echo "$(date '+%F %T') 🧹 Cleaning up local tarball..."
  87. rm -f "$TAR_FILE"
  88.  
  89. echo -e "\n$(date '+%F %T') πŸŽ‰ Firefox profile has been cloned to all reachable targets."
  90. exit 0
  91.  
  92. ################################################################################
  93. .bashrc
  94. ################################################################################
  95. Ensure the script is in your home directory
  96. Ensure the script is executable
  97. chmod +x clone_firefox_profile.sh
  98.  
  99. Open your .bashrc in your home directory
  100. nano .bashrc
  101.  
  102. I added my special aliases under what you see below that is in a .bashrc file
  103. After you save the .bashrc don't forget to run "source ~/.bashrc" to update the .bashrc file
  104.  
  105. also sudo -i to root and do the same to the.bashrc that lives in /root
  106.  
  107. # Alias definitions.
  108. # You may want to put all your additions into a separate file like
  109. # ~/.bash_aliases, instead of adding them here directly.
  110. # See /usr/share/doc/bash-doc/examples in the bash-doc package.
  111.  
  112. if [ -f ~/.bash_aliases ]; then
  113. . ~/.bash_aliases
  114. fi
  115.  
  116. #begin my add
  117. alias clonefirefox="~/clone_firefox_profile.sh"
  118. #end my add
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement