blob: 84ad25a842e436333ac296c9e65343bf69ccd7cf [file] [log] [blame]
kmobse051cf32010-06-24 22:36:11 -05001#!/sbin/sh
2#
Warren Togamib1637c82012-03-03 22:37:42 -10003# Backup and restore addon /system files
kmobse051cf32010-06-24 22:36:11 -05004#
5
Warren Togamib1637c82012-03-03 22:37:42 -10006export C=/tmp/backupdir
7export S=/system
Rashed Abdel-Tawab6326caa2018-08-07 10:49:49 -07008export V=16.0
kmobse051cf32010-06-24 22:36:11 -05009
Dan Pasanen524fe032018-01-26 10:26:47 -060010export ADDOND_VERSION=1
11
Tom Marshall65aff662014-12-12 11:56:04 -080012# Scripts in /system/addon.d expect to find backuptool.functions in /tmp
13cp -f /tmp/install/bin/backuptool.functions /tmp
14
Warren Togamib1637c82012-03-03 22:37:42 -100015# Preserve /system/addon.d in /tmp/addon.d
16preserve_addon_d() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020017 if [ -d /system/addon.d/ ]; then
18 mkdir -p /tmp/addon.d/
19 cp -a /system/addon.d/* /tmp/addon.d/
Dan Pasanen524fe032018-01-26 10:26:47 -060020
21 # Discard any scripts that aren't at least our version level
22 for f in /postinstall/tmp/addon.d/*sh; do
23 SCRIPT_VERSION=$(grep "^# ADDOND_VERSION=" $f | cut -d= -f2)
24 if [ -z "$SCRIPT_VERSION" ]; then
25 SCRIPT_VERSION=1
26 fi
27 if [ $SCRIPT_VERSION -lt $ADDOND_VERSION ]; then
28 rm $f
29 fi
30 done
31
Andreas Schneider9cfe9682015-09-03 11:04:58 +020032 chmod 755 /tmp/addon.d/*.sh
33 fi
Warren Togamib1637c82012-03-03 22:37:42 -100034}
35
Andreas Schneider9cfe9682015-09-03 11:04:58 +020036# Restore /system/addon.d from /tmp/addon.d
Warren Togamib1637c82012-03-03 22:37:42 -100037restore_addon_d() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020038 if [ -d /tmp/addon.d/ ]; then
Adrian DC13512fb2016-10-23 18:37:13 +020039 mkdir -p /system/addon.d/
Andreas Schneider9cfe9682015-09-03 11:04:58 +020040 cp -a /tmp/addon.d/* /system/addon.d/
41 rm -rf /tmp/addon.d/
42 fi
Warren Togamib1637c82012-03-03 22:37:42 -100043}
44
Chirayu Desai700fc8f2012-12-13 19:36:37 +053045# Proceed only if /system is the expected major and minor version
kmobse051cf32010-06-24 22:36:11 -050046check_prereq() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020047# If there is no build.prop file the partition is probably empty.
48if [ ! -r /system/build.prop ]; then
49 return 0
50fi
Adrian DC05ab72a2018-05-08 15:12:16 +020051if ! grep -q "^ro.lineage.version=$V.*" /system/build.prop; then
Adrian DC29fca642018-05-17 23:06:24 +020052 echo "Not backing up files from incompatible version: $V"
53 return 0
54fi
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010055return 1
kmobse051cf32010-06-24 22:36:11 -050056}
57
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000058check_blacklist() {
Gabriele M4fe35092017-06-03 18:08:39 +020059 if [ -f /system/addon.d/blacklist -a -d /$1/addon.d/ ]; then
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000060 ## Discard any known bad backup scripts
61 cd /$1/addon.d/
62 for f in *sh; do
Gabriele M4fe35092017-06-03 18:08:39 +020063 [ -f $f ] || continue
Tom Marshall517e6912015-11-18 14:45:04 -080064 s=$(md5sum $f | cut -c-32)
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000065 grep -q $s /system/addon.d/blacklist && rm -f $f
66 done
67 fi
68}
69
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010070check_whitelist() {
71 found=0
72 if [ -f /system/addon.d/whitelist ];then
73 ## forcefully keep any version-independent stuff
74 cd /$1/addon.d/
75 for f in *sh; do
Tom Marshall517e6912015-11-18 14:45:04 -080076 s=$(md5sum $f | cut -c-32)
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010077 grep -q $s /system/addon.d/whitelist
78 if [ $? -eq 0 ]; then
79 found=1
80 else
81 rm -f $f
82 fi
83 done
84 fi
85 return $found
86}
87
Warren Togamib1637c82012-03-03 22:37:42 -100088# Execute /system/addon.d/*.sh scripts with $1 parameter
89run_stage() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020090if [ -d /tmp/addon.d/ ]; then
91 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
92 $script $1
93 done
94fi
kmobse051cf32010-06-24 22:36:11 -050095}
96
kmobse051cf32010-06-24 22:36:11 -050097case "$1" in
Warren Togamib1637c82012-03-03 22:37:42 -100098 backup)
99 mkdir -p $C
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +0100100 if check_prereq; then
101 if check_whitelist system; then
102 exit 127
103 fi
104 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +0000105 check_blacklist system
Warren Togamib1637c82012-03-03 22:37:42 -1000106 preserve_addon_d
107 run_stage pre-backup
108 run_stage backup
109 run_stage post-backup
Warren Togamib1637c82012-03-03 22:37:42 -1000110 ;;
111 restore)
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +0100112 if check_prereq; then
113 if check_whitelist tmp; then
114 exit 127
115 fi
116 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +0000117 check_blacklist tmp
Warren Togamib1637c82012-03-03 22:37:42 -1000118 run_stage pre-restore
119 run_stage restore
120 run_stage post-restore
121 restore_addon_d
Warren Togamib1637c82012-03-03 22:37:42 -1000122 rm -rf $C
123 sync
124 ;;
125 *)
126 echo "Usage: $0 {backup|restore}"
127 exit 1
kmobse051cf32010-06-24 22:36:11 -0500128esac
129
130exit 0