-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathbaskup.sh
executable file
·59 lines (51 loc) · 1.99 KB
/
baskup.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
#!/bin/sh
BACKUP_DIR=./backup
OS_Version=$(sw_vers -productVersion)
LAST_VERSION=10.13
NEEDS_MODIFICATION=$(echo $OS_Version '>=' $LAST_VERSION | bc -l)
BACKUP_ATTACHMENTS=0
while getopts ":a" opt; do
case $opt in
a)
echo "Running baskup for text + attachments"
BACKUP_ATTACHMENTS=1
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
function select_rows () {
sqlite3 ~/Library/Messages/chat.db "$1"
}
for line in $(select_rows "select distinct guid from chat;" ); do
contact=$line
arrIN=(${contact//;/ })
contactNumber=${arrIN[2]}
#Make a directory specifically for this folder
mkdir -p $BACKUP_DIR/$contactNumber/Attachments
#Perform SQL operations
if [[ $NEEDS_MODIFICATION == 1 ]]; then
select_rows "
select is_from_me,text, datetime((date/1000000000) + strftime('%s', '2001-01-01 00:00:00'), 'unixepoch', 'localtime') as date from message where handle_id=(
select handle_id from chat_handle_join where chat_id=(
select ROWID from chat where guid='$line')
)" | sed 's/1\|/Me: /g;s/0\|/Friend: /g' > $BACKUP_DIR/$contactNumber/$line.txt
else
select_rows "
select is_from_me,text, datetime(date + strftime('%s', '2001-01-01 00:00:00'), 'unixepoch', 'localtime') as date from message where handle_id=(
select handle_id from chat_handle_join where chat_id=(
select ROWID from chat where guid='$line')
)" | sed 's/1\|/Me: /g;s/0\|/Friend: /g' > $BACKUP_DIR/$contactNumber/$line.txt
fi
if [[ $BACKUP_ATTACHMENTS == 1 ]]; then
select_rows "
select filename from attachment where rowid in (
select attachment_id from message_attachment_join where message_id in (
select rowid from message where cache_has_attachments=1 and handle_id=(
select handle_id from chat_handle_join where chat_id=(
select ROWID from chat where guid='$line')
)))" | cut -c 2- | awk -v home=$HOME '{print home $0}' | tr '\n' '\0' | xargs -0 -I fname cp fname $BACKUP_DIR/$contactNumber/Attachments
fi
done