blob: de7b79bf9bbf3457dc9fee70abe7abacfd8646ac [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
Davide Garberie40c3182019-09-24 20:18:11 +02007export SYSDEV="$(readlink -nf "$2")"
8export SYSFS="$3"
Michael Bestas42020e22020-01-03 02:03:00 +02009export V=17.1
kmobse051cf32010-06-24 22:36:11 -050010
Dan Pasanen524fe032018-01-26 10:26:47 -060011export ADDOND_VERSION=1
12
Tom Marshall65aff662014-12-12 11:56:04 -080013# Scripts in /system/addon.d expect to find backuptool.functions in /tmp
14cp -f /tmp/install/bin/backuptool.functions /tmp
15
Warren Togamib1637c82012-03-03 22:37:42 -100016# Preserve /system/addon.d in /tmp/addon.d
17preserve_addon_d() {
LuK133757490e62018-12-05 20:51:21 +010018 if [ -d $S/addon.d/ ]; then
Andreas Schneider9cfe9682015-09-03 11:04:58 +020019 mkdir -p /tmp/addon.d/
LuK133757490e62018-12-05 20:51:21 +010020 cp -a $S/addon.d/* /tmp/addon.d/
Dan Pasanen524fe032018-01-26 10:26:47 -060021
22 # Discard any scripts that aren't at least our version level
Davide Garberi23d677b2020-01-19 12:21:00 +010023 for f in /tmp/addon.d/*sh; do
Dan Pasanen524fe032018-01-26 10:26:47 -060024 SCRIPT_VERSION=$(grep "^# ADDOND_VERSION=" $f | cut -d= -f2)
25 if [ -z "$SCRIPT_VERSION" ]; then
26 SCRIPT_VERSION=1
27 fi
28 if [ $SCRIPT_VERSION -lt $ADDOND_VERSION ]; then
29 rm $f
30 fi
31 done
32
Andreas Schneider9cfe9682015-09-03 11:04:58 +020033 chmod 755 /tmp/addon.d/*.sh
34 fi
Warren Togamib1637c82012-03-03 22:37:42 -100035}
36
Andreas Schneider9cfe9682015-09-03 11:04:58 +020037# Restore /system/addon.d from /tmp/addon.d
Warren Togamib1637c82012-03-03 22:37:42 -100038restore_addon_d() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020039 if [ -d /tmp/addon.d/ ]; then
LuK133757490e62018-12-05 20:51:21 +010040 mkdir -p $S/addon.d/
41 cp -a /tmp/addon.d/* $S/addon.d/
Andreas Schneider9cfe9682015-09-03 11:04:58 +020042 rm -rf /tmp/addon.d/
43 fi
Warren Togamib1637c82012-03-03 22:37:42 -100044}
45
Chirayu Desai700fc8f2012-12-13 19:36:37 +053046# Proceed only if /system is the expected major and minor version
kmobse051cf32010-06-24 22:36:11 -050047check_prereq() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020048# If there is no build.prop file the partition is probably empty.
LuK133757490e62018-12-05 20:51:21 +010049if [ ! -r $S/build.prop ]; then
Andreas Schneider9cfe9682015-09-03 11:04:58 +020050 return 0
51fi
LuK133757490e62018-12-05 20:51:21 +010052if ! grep -q "^ro.lineage.version=$V.*" $S/build.prop; then
Adrian DC29fca642018-05-17 23:06:24 +020053 echo "Not backing up files from incompatible version: $V"
54 return 0
55fi
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010056return 1
kmobse051cf32010-06-24 22:36:11 -050057}
58
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000059check_blacklist() {
LuK133757490e62018-12-05 20:51:21 +010060 if [ -f $S/addon.d/blacklist -a -d /$1/addon.d/ ]; then
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000061 ## Discard any known bad backup scripts
Davide Garberi497a6ae2020-01-19 19:28:55 +010062 for f in /$1/addon.d/*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
Davide Garberie40c3182019-09-24 20:18:11 +020097determine_system_mount() {
98 if grep -q -e"^$SYSDEV" /proc/mounts; then
99 umount $(grep -e"^$SYSDEV" /proc/mounts | cut -d" " -f2)
100 fi
101
102 if [ -d /mnt/system ]; then
103 SYSMOUNT="/mnt/system"
104 elif [ -d /system_root ]; then
105 SYSMOUNT="/system_root"
106 else
107 SYSMOUNT="/system"
108 fi
109
110 export S=$SYSMOUNT/system
111}
112
113mount_system() {
114 mount -t $SYSFS $SYSDEV $SYSMOUNT -o rw,discard
115}
116
117unmount_system() {
118 umount $SYSMOUNT
119}
120
121determine_system_mount
122
kmobse051cf32010-06-24 22:36:11 -0500123case "$1" in
Warren Togamib1637c82012-03-03 22:37:42 -1000124 backup)
Davide Garberie40c3182019-09-24 20:18:11 +0200125 mount_system
Warren Togamib1637c82012-03-03 22:37:42 -1000126 mkdir -p $C
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +0100127 if check_prereq; then
Davide Garberi6b83f942020-01-19 17:44:11 +0100128 if check_whitelist $S; then
Davide Garberie40c3182019-09-24 20:18:11 +0200129 unmount_system
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +0100130 exit 127
131 fi
132 fi
Davide Garberi6b83f942020-01-19 17:44:11 +0100133 check_blacklist $S
Warren Togamib1637c82012-03-03 22:37:42 -1000134 preserve_addon_d
135 run_stage pre-backup
136 run_stage backup
137 run_stage post-backup
Davide Garberie40c3182019-09-24 20:18:11 +0200138 unmount_system
Warren Togamib1637c82012-03-03 22:37:42 -1000139 ;;
140 restore)
Davide Garberie40c3182019-09-24 20:18:11 +0200141 mount_system
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +0100142 if check_prereq; then
143 if check_whitelist tmp; then
Davide Garberie40c3182019-09-24 20:18:11 +0200144 unmount_system
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +0100145 exit 127
146 fi
147 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +0000148 check_blacklist tmp
Warren Togamib1637c82012-03-03 22:37:42 -1000149 run_stage pre-restore
150 run_stage restore
151 run_stage post-restore
152 restore_addon_d
Warren Togamib1637c82012-03-03 22:37:42 -1000153 rm -rf $C
154 sync
Davide Garberie40c3182019-09-24 20:18:11 +0200155 unmount_system
Warren Togamib1637c82012-03-03 22:37:42 -1000156 ;;
157 *)
158 echo "Usage: $0 {backup|restore}"
159 exit 1
kmobse051cf32010-06-24 22:36:11 -0500160esac
161
162exit 0