-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailDBOptimiser.sh
More file actions
executable file
·53 lines (42 loc) · 1.33 KB
/
MailDBOptimiser.sh
File metadata and controls
executable file
·53 lines (42 loc) · 1.33 KB
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
#!/bin/bash
# Apple Mail database defragmentation / optimisation script
# Start script
clear
echo "Apple Mail Database optimisation started..."
# Define variable(s)
os=$(sw_vers -productVersion)
# Close Apple Mail
AppRunning=$(pgrep Mail)
if [[ -n $AppRunning ]]; then
osascript -e 'quit app "Mail"'
fi
# Check for OS X version
if [[ $os = 10.11.* ]]; then
mailversion="V3"
else
mailversion="V2"
fi
# Calculate database size before starting optimisation
sizebefore=$(ls -lnah ~/Library/Mail/$mailversion/MailData | grep -E 'Envelope Index$' | awk {'print $5'})B
# Run database optimisation (SQL vaccuming)
/usr/bin/sqlite3 ~/Library/Mail/$mailversion/MailData/Envelope\ Index vacuum &> /private/tmp/MailDatabaseOptimisation.log
error=$(cat /private/tmp/MailDatabaseOptimisation.log)
# Calculate database size after optimisation
sizeafter=$(ls -lnah ~/Library/Mail/$mailversion/MailData | grep -E 'Envelope Index$' | awk {'print $5'})B
# Create success/error message
if [[ -z $error ]]; then
error=0
echo "Done."
else
echo $error
fi
# Show stats
printf "\nMail index size before:\t%s\nMail index size after:\t%s\n\nEnjoy the new speed!\n\n" "$sizebefore" "$sizeafter"
# Set exit code
if [[ "$error" -ne "0" ]]; then
logger "Apple Mail Database Optimisation failed."
exit 1
else
logger "Apple Mail Database Optimisation finished."
exit 0
fi