blob: 4a99e42302867fe51942f18c0de2afc9603a06e5 [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
Tom Marshall65aff662014-12-12 11:56:04 -080011# Scripts in /system/addon.d expect to find backuptool.functions in /tmp
12cp -f /tmp/install/bin/backuptool.functions /tmp
13
Warren Togamib1637c82012-03-03 22:37:42 -100014# Preserve /system/addon.d in /tmp/addon.d
15preserve_addon_d() {
LuK133757490e62018-12-05 20:51:21 +010016 if [ -d $S/addon.d/ ]; then
Andreas Schneider9cfe9682015-09-03 11:04:58 +020017 mkdir -p /tmp/addon.d/
LuK133757490e62018-12-05 20:51:21 +010018 cp -a $S/addon.d/* /tmp/addon.d/
Andreas Schneider9cfe9682015-09-03 11:04:58 +020019 chmod 755 /tmp/addon.d/*.sh
20 fi
Warren Togamib1637c82012-03-03 22:37:42 -100021}
22
Andreas Schneider9cfe9682015-09-03 11:04:58 +020023# Restore /system/addon.d from /tmp/addon.d
Warren Togamib1637c82012-03-03 22:37:42 -100024restore_addon_d() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020025 if [ -d /tmp/addon.d/ ]; then
LuK133757490e62018-12-05 20:51:21 +010026 mkdir -p $S/addon.d/
27 cp -a /tmp/addon.d/* $S/addon.d/
Andreas Schneider9cfe9682015-09-03 11:04:58 +020028 rm -rf /tmp/addon.d/
29 fi
Warren Togamib1637c82012-03-03 22:37:42 -100030}
31
Chirayu Desai700fc8f2012-12-13 19:36:37 +053032# Proceed only if /system is the expected major and minor version
kmobse051cf32010-06-24 22:36:11 -050033check_prereq() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020034# If there is no build.prop file the partition is probably empty.
LuK133757490e62018-12-05 20:51:21 +010035if [ ! -r $S/build.prop ]; then
Andreas Schneider9cfe9682015-09-03 11:04:58 +020036 return 0
37fi
LuK133757490e62018-12-05 20:51:21 +010038if ! grep -q "^ro.lineage.version=$V.*" $S/build.prop; then
Adrian DC29fca642018-05-17 23:06:24 +020039 echo "Not backing up files from incompatible version: $V"
40 return 0
41fi
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010042return 1
kmobse051cf32010-06-24 22:36:11 -050043}
44
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000045check_blacklist() {
LuK133757490e62018-12-05 20:51:21 +010046 if [ -f $S/addon.d/blacklist -a -d /$1/addon.d/ ]; then
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000047 ## Discard any known bad backup scripts
Davide Garberi497a6ae2020-01-19 19:28:55 +010048 for f in /$1/addon.d/*sh; do
Gabriele M4fe35092017-06-03 18:08:39 +020049 [ -f $f ] || continue
Tom Marshall517e6912015-11-18 14:45:04 -080050 s=$(md5sum $f | cut -c-32)
LuK133757490e62018-12-05 20:51:21 +010051 grep -q $s $S/addon.d/blacklist && rm -f $f
Ricardo Cerqueira129989b2012-12-19 01:11:21 +000052 done
53 fi
54}
55
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010056check_whitelist() {
57 found=0
LuK133757490e62018-12-05 20:51:21 +010058 if [ -f $S/addon.d/whitelist ];then
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010059 ## forcefully keep any version-independent stuff
60 cd /$1/addon.d/
61 for f in *sh; do
Tom Marshall517e6912015-11-18 14:45:04 -080062 s=$(md5sum $f | cut -c-32)
LuK133757490e62018-12-05 20:51:21 +010063 grep -q $s $S/addon.d/whitelist
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010064 if [ $? -eq 0 ]; then
65 found=1
66 else
67 rm -f $f
68 fi
69 done
70 fi
71 return $found
72}
73
Warren Togamib1637c82012-03-03 22:37:42 -100074# Execute /system/addon.d/*.sh scripts with $1 parameter
75run_stage() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020076if [ -d /tmp/addon.d/ ]; then
77 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
78 $script $1
79 done
80fi
kmobse051cf32010-06-24 22:36:11 -050081}
82
Davide Garberie40c3182019-09-24 20:18:11 +020083determine_system_mount() {
84 if grep -q -e"^$SYSDEV" /proc/mounts; then
85 umount $(grep -e"^$SYSDEV" /proc/mounts | cut -d" " -f2)
86 fi
87
88 if [ -d /mnt/system ]; then
89 SYSMOUNT="/mnt/system"
90 elif [ -d /system_root ]; then
91 SYSMOUNT="/system_root"
92 else
93 SYSMOUNT="/system"
94 fi
95
96 export S=$SYSMOUNT/system
97}
98
99mount_system() {
100 mount -t $SYSFS $SYSDEV $SYSMOUNT -o rw,discard
101}
102
103unmount_system() {
104 umount $SYSMOUNT
105}
106
107determine_system_mount
108
kmobse051cf32010-06-24 22:36:11 -0500109case "$1" in
Warren Togamib1637c82012-03-03 22:37:42 -1000110 backup)
Davide Garberie40c3182019-09-24 20:18:11 +0200111 mount_system
Warren Togamib1637c82012-03-03 22:37:42 -1000112 mkdir -p $C
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +0100113 if check_prereq; then
Davide Garberi6b83f942020-01-19 17:44:11 +0100114 if check_whitelist $S; then
Davide Garberie40c3182019-09-24 20:18:11 +0200115 unmount_system
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +0100116 exit 127
117 fi
118 fi
Davide Garberi6b83f942020-01-19 17:44:11 +0100119 check_blacklist $S
Warren Togamib1637c82012-03-03 22:37:42 -1000120 preserve_addon_d
121 run_stage pre-backup
122 run_stage backup
123 run_stage post-backup
Davide Garberie40c3182019-09-24 20:18:11 +0200124 unmount_system
Warren Togamib1637c82012-03-03 22:37:42 -1000125 ;;
126 restore)
Davide Garberie40c3182019-09-24 20:18:11 +0200127 mount_system
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +0100128 if check_prereq; then
129 if check_whitelist tmp; then
Davide Garberie40c3182019-09-24 20:18:11 +0200130 unmount_system
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +0100131 exit 127
132 fi
133 fi
Ricardo Cerqueira129989b2012-12-19 01:11:21 +0000134 check_blacklist tmp
Warren Togamib1637c82012-03-03 22:37:42 -1000135 run_stage pre-restore
136 run_stage restore
137 run_stage post-restore
138 restore_addon_d
Warren Togamib1637c82012-03-03 22:37:42 -1000139 rm -rf $C
140 sync
Davide Garberie40c3182019-09-24 20:18:11 +0200141 unmount_system
Warren Togamib1637c82012-03-03 22:37:42 -1000142 ;;
143 *)
144 echo "Usage: $0 {backup|restore}"
145 exit 1
kmobse051cf32010-06-24 22:36:11 -0500146esac
147
148exit 0