blob: 9588c4a8cab07a2c47e3ee7631ff39945b750870 [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
LuK133757490e62018-12-05 20:51:21 +01007export S=$2
Rashed Abdel-Tawabb6b34212019-09-04 15:08:51 -07008export V=17.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() {
LuK133757490e62018-12-05 20:51:21 +010017 if [ -d $S/addon.d/ ]; then
Andreas Schneider9cfe9682015-09-03 11:04:58 +020018 mkdir -p /tmp/addon.d/
LuK133757490e62018-12-05 20:51:21 +010019 cp -a $S/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
LuK133757490e62018-12-05 20:51:21 +010039 mkdir -p $S/addon.d/
40 cp -a /tmp/addon.d/* $S/addon.d/
Andreas Schneider9cfe9682015-09-03 11:04:58 +020041 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.
LuK133757490e62018-12-05 20:51:21 +010048if [ ! -r $S/build.prop ]; then
Andreas Schneider9cfe9682015-09-03 11:04:58 +020049 return 0
50fi
LuK133757490e62018-12-05 20:51:21 +010051if ! grep -q "^ro.lineage.version=$V.*" $S/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() {
LuK133757490e62018-12-05 20:51:21 +010059 if [ -f $S/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)
LuK133757490e62018-12-05 20:51:21 +010065 grep -q $s $S/addon.d/blacklist && rm -f $f
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000066 done
67 fi
68}
69
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010070check_whitelist() {
71 found=0
LuK133757490e62018-12-05 20:51:21 +010072 if [ -f $S/addon.d/whitelist ];then
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010073 ## 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)
LuK133757490e62018-12-05 20:51:21 +010077 grep -q $s $S/addon.d/whitelist
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010078 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