Export Lifecycle app GPS locations to Owntracks
1#!/bin/bash
2
3# LifeCycle Database Export Script
4# Exports primary observational data tables to JSON format
5
6# Default values
7DB_FILE="life.db"
8OUTPUT_DIR="lifecycle_export"
9
10# Parse command line arguments
11usage() {
12 echo "Usage: $0 [OPTIONS]"
13 echo "Options:"
14 echo " -d, --database FILE Path to the LifeCycle database file (default: life.db)"
15 echo " -o, --output DIR Output directory for exported JSON files (default: lifecycle_export)"
16 echo " -h, --help Display this help message"
17 echo ""
18 echo "This script exports LifeCycle database tables to JSON format for use with OwnTracks."
19 exit 1
20}
21
22while [[ $# -gt 0 ]]; do
23 case $1 in
24 -d|--database)
25 DB_FILE="$2"
26 shift 2
27 ;;
28 -o|--output)
29 OUTPUT_DIR="$2"
30 shift 2
31 ;;
32 -h|--help)
33 usage
34 ;;
35 *)
36 echo "Unknown option: $1"
37 usage
38 ;;
39 esac
40done
41
42# Check if database file exists
43if [ ! -f "$DB_FILE" ]; then
44 echo "Error: Database file '$DB_FILE' not found"
45 echo "Please specify the correct path using -d/--database option"
46 exit 1
47fi
48
49# Create output directory
50mkdir -p "$OUTPUT_DIR"
51
52echo "Exporting LifeCycle database to JSON..."
53echo "Database: $DB_FILE"
54echo "Output directory: $OUTPUT_DIR"
55echo
56
57# Function to export table to JSON
58export_table() {
59 local table_name=$1
60 local output_file="$OUTPUT_DIR/${table_name}.json"
61
62 echo "Exporting $table_name..."
63
64 # Determine appropriate sort column based on table
65 local sort_clause=""
66 case "$table_name" in
67 "LocationEvent"|"Visit"|"Activity"|"Motion"|"Transit")
68 sort_clause="ORDER BY timestamp"
69 ;;
70 "LocationPosition"|"LocationName"|"ActivityType"|"ActivityCategory"|"TimeZone")
71 sort_clause="ORDER BY id"
72 ;;
73 *)
74 sort_clause="ORDER BY rowid"
75 ;;
76 esac
77
78 sqlite3 "$DB_FILE" <<EOF
79.mode json
80.output $output_file
81SELECT * FROM $table_name $sort_clause;
82.output stdout
83EOF
84
85 if [ -f "$output_file" ]; then
86 local count=$(jq -r '. | length' "$output_file" 2>/dev/null || wc -l < "$output_file" | tr -d ' ')
87 echo " → $count records exported to $output_file"
88 else
89 echo " → Error: Failed to create $output_file"
90 fi
91}
92
93# Export primary observational data tables
94echo "=== PRIMARY LOCATION TRACKING DATA ==="
95export_table "LocationEvent"
96export_table "Visit"
97export_table "LocationPosition"
98export_table "LocationName"
99
100echo
101echo "=== ADDITIONAL CORE DATA ==="
102export_table "Activity"
103export_table "Motion"
104export_table "Transit"
105
106echo
107echo "=== REFERENCE DATA ==="
108export_table "ActivityType"
109export_table "ActivityCategory"
110export_table "TimeZone"
111
112echo
113echo "Export completed!"
114echo "Files created in: $OUTPUT_DIR/"
115ls -la "$OUTPUT_DIR/"
116
117# Create a summary file
118echo "Creating export summary..."
119cat > "$OUTPUT_DIR/export_summary.txt" <<EOF
120LifeCycle Database Export Summary
121Generated: $(date)
122Source Database: $DB_FILE
123
124Primary Location Data:
125- LocationEvent: Raw GPS coordinates and sensor data
126- Visit: Stationary periods at locations
127- LocationPosition: Named locations with coordinates
128- LocationName: Location identifiers and names
129
130Core Activity Data:
131- Activity: Classified activities and behaviors
132- Motion: Movement type detection (walk/run/drive/etc)
133- Transit: Transportation between locations
134
135Reference Data:
136- ActivityType: Activity classifications (sleep, work, etc)
137- ActivityCategory: Activity categories (transport, social, etc)
138- TimeZone: Timezone reference data
139
140Note: This export focuses on primary observational data.
141Derived tables like DailyStats, LongTermStats, and Insights are excluded.
142EOF
143
144echo "Summary created: $OUTPUT_DIR/export_summary.txt"