blob: 7f880d8af5021ef83887750b0d9f2aba6b33b3b3 [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
Jackeaglec2092eb2020-04-08 07:14:44 +02007export V=12.6
Davide Garberifb8319c2019-09-24 20:18:11 +02008export SYSDEV="$(readlink -nf "$2")"
9export SYSFS="$3"
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 Garberi29a1e432020-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
z3DD3rcd77d652020-03-04 18:07:40 +030050 echo "Backup/restore is not possible. Partition is probably empty"
51 return 1
Andreas Schneider9cfe9682015-09-03 11:04:58 +020052fi
pimpmaneaton2a090f22019-11-02 13:26:45 -060053if ! grep -q "^ro.bliss.version=$V.*" $S/build.prop; then
z3DD3rcd77d652020-03-04 18:07:40 +030054 echo "Backup/restore is not possible. Incompatible ROM version: $V"
55 return 2
Adrian DC29fca642018-05-17 23:06:24 +020056fi
z3DD3rcd77d652020-03-04 18:07:40 +030057return 0
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010058}
59
Warren Togamib1637c82012-03-03 22:37:42 -100060# Execute /system/addon.d/*.sh scripts with $1 parameter
61run_stage() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020062if [ -d /tmp/addon.d/ ]; then
63 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
64 $script $1
65 done
66fi
kmobse051cf32010-06-24 22:36:11 -050067}
68
Davide Garberifb8319c2019-09-24 20:18:11 +020069determine_system_mount() {
70 if grep -q -e"^$SYSDEV" /proc/mounts; then
71 umount $(grep -e"^$SYSDEV" /proc/mounts | cut -d" " -f2)
72 fi
73
74 if [ -d /mnt/system ]; then
75 SYSMOUNT="/mnt/system"
76 elif [ -d /system_root ]; then
77 SYSMOUNT="/system_root"
78 else
79 SYSMOUNT="/system"
80 fi
81
82 export S=$SYSMOUNT/system
83}
84
85mount_system() {
86 mount -t $SYSFS $SYSDEV $SYSMOUNT -o rw,discard
87}
88
89unmount_system() {
90 umount $SYSMOUNT
91}
92
93determine_system_mount
94
kmobse051cf32010-06-24 22:36:11 -050095case "$1" in
Warren Togamib1637c82012-03-03 22:37:42 -100096 backup)
Davide Garberifb8319c2019-09-24 20:18:11 +020097 mount_system
z3DD3rcd77d652020-03-04 18:07:40 +030098 if check_prereq; then
99 mkdir -p $C
100 preserve_addon_d
101 run_stage pre-backup
102 run_stage backup
103 run_stage post-backup
104 fi
Davide Garberifb8319c2019-09-24 20:18:11 +0200105 unmount_system
Warren Togamib1637c82012-03-03 22:37:42 -1000106 ;;
107 restore)
Davide Garberifb8319c2019-09-24 20:18:11 +0200108 mount_system
z3DD3rcd77d652020-03-04 18:07:40 +0300109 if check_prereq; then
110 run_stage pre-restore
111 run_stage restore
112 run_stage post-restore
113 restore_addon_d
114 rm -rf $C
115 sync
116 fi
Davide Garberifb8319c2019-09-24 20:18:11 +0200117 unmount_system
Warren Togamib1637c82012-03-03 22:37:42 -1000118 ;;
119 *)
120 echo "Usage: $0 {backup|restore}"
121 exit 1
kmobse051cf32010-06-24 22:36:11 -0500122esac
123
124exit 0