-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_arc_history.sh
executable file
·121 lines (101 loc) · 3.09 KB
/
merge_arc_history.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
# Set paths
NEW_MAC_DB="$HOME/Library/Application Support/Arc/User Data/Default/History"
BACKUP_DIR="$HOME/arc_history_backup_$(date +%Y%m%d_%H%M%S)"
REMOTE_HOST="jasper@10.10.11.127"
TEMP_DIR="/tmp/arc_history_merge_$(date +%Y%m%d_%H%M%S)"
TEMP_NEW_DB="$TEMP_DIR/new_history"
TEMP_OLD_DB="$TEMP_DIR/old_history"
# Create directories
mkdir -p "$BACKUP_DIR"
mkdir -p "$TEMP_DIR"
# Create backup of current history
echo "Creating backup of current Arc history..."
cp "$NEW_MAC_DB" "$BACKUP_DIR/History.backup"
if [ $? -ne 0 ]; then
echo "Failed to create backup. Aborting."
exit 1
fi
# Copy the remote history database
echo "Copying history from old Mac (Albus)..."
scp "$REMOTE_HOST:~/Library/Application Support/Arc/User\ Data/Default/History" "$TEMP_OLD_DB"
if [ $? -ne 0 ]; then
echo "Failed to copy remote history. Aborting."
rm -rf "$TEMP_DIR"
exit 1
fi
echo "Please completely quit Arc browser (Cmd+Q) and press Enter to continue..."
read
# Copy current database to temp location
cp "$NEW_MAC_DB" "$TEMP_NEW_DB"
# Function to wait for database to be unlocked
wait_for_db() {
local db=$1
local attempts=0
local max_attempts=5
while [ $attempts -lt $max_attempts ]; do
if sqlite3 "$db" "SELECT 1;" >/dev/null 2>&1; then
return 0
fi
echo "Database locked, waiting..."
sleep 2
attempts=$((attempts + 1))
done
return 1
}
# Merge the databases using temporary copies
echo "Merging histories..."
if ! wait_for_db "$TEMP_NEW_DB"; then
echo "Database remained locked. Aborting."
rm -rf "$TEMP_DIR"
exit 1
fi
sqlite3 "$TEMP_NEW_DB" <<EOF
ATTACH '$TEMP_OLD_DB' AS old_db;
-- Begin transaction
BEGIN TRANSACTION;
-- Create temporary table for merged URLs
CREATE TEMPORARY TABLE merged_urls AS
SELECT * FROM urls
UNION ALL
SELECT * FROM old_db.urls;
-- Update the urls table with combined visit counts and latest timestamps
INSERT OR REPLACE INTO urls (url, title, visit_count, typed_count, last_visit_time, hidden)
SELECT
url,
MAX(title),
SUM(visit_count),
SUM(typed_count),
MAX(last_visit_time),
MAX(hidden)
FROM merged_urls
GROUP BY url;
-- For visits, insert all records except exact duplicates
INSERT INTO visits (url, visit_time, from_visit, transition, segment_id, visit_duration, incremented_omnibox_typed_score)
SELECT DISTINCT url, visit_time, from_visit, transition, segment_id, visit_duration, incremented_omnibox_typed_score
FROM old_db.visits v1
WHERE NOT EXISTS (
SELECT 1 FROM visits v2
WHERE v2.url = v1.url
AND v2.visit_time = v1.visit_time
);
-- Commit transaction
COMMIT;
-- Detach old database
DETACH old_db;
-- Vacuum the database
VACUUM;
EOF
if [ $? -eq 0 ]; then
echo "History merge completed successfully!"
echo "Moving merged database into place..."
mv "$TEMP_NEW_DB" "$NEW_MAC_DB"
echo "Backup saved at: $BACKUP_DIR/History.backup"
else
echo "Error occurred during merge. Restoring backup..."
cp "$BACKUP_DIR/History.backup" "$NEW_MAC_DB"
echo "Backup restored."
fi
# Cleanup
rm -rf "$TEMP_DIR"
echo "You can now restart Arc browser."