blob: 29f8818d7dba7b7d8212ed8ee9521b7d013312d9 [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"
Jan Altensendd29a472020-09-11 21:56:13 +02009export V=18.0
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
z3DD3r284ec832020-03-04 18:07:40 +030036 echo "Backup/restore is not possible. Partition is probably empty"
37 return 1
Andreas Schneider9cfe9682015-09-03 11:04:58 +020038fi
Jackeagled6811aa2019-09-24 08:26:40 +020039if ! grep -q "^ro.bliss.version=$V.*" $S/build.prop; then
z3DD3r284ec832020-03-04 18:07:40 +030040 echo "Backup/restore is not possible. Incompatible ROM version: $V"
41 return 2
Adrian DC29fca642018-05-17 23:06:24 +020042fi
z3DD3r284ec832020-03-04 18:07:40 +030043return 0
Ricardo Cerqueira4f4c30b2013-08-19 04:00:05 +010044}
45
Warren Togamib1637c82012-03-03 22:37:42 -100046# Execute /system/addon.d/*.sh scripts with $1 parameter
47run_stage() {
Andreas Schneider9cfe9682015-09-03 11:04:58 +020048if [ -d /tmp/addon.d/ ]; then
49 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
50 $script $1
51 done
52fi
kmobse051cf32010-06-24 22:36:11 -050053}
54
Davide Garberie40c3182019-09-24 20:18:11 +020055determine_system_mount() {
56 if grep -q -e"^$SYSDEV" /proc/mounts; then
57 umount $(grep -e"^$SYSDEV" /proc/mounts | cut -d" " -f2)
58 fi
59
60 if [ -d /mnt/system ]; then
61 SYSMOUNT="/mnt/system"
62 elif [ -d /system_root ]; then
63 SYSMOUNT="/system_root"
64 else
65 SYSMOUNT="/system"
66 fi
67
68 export S=$SYSMOUNT/system
69}
70
71mount_system() {
72 mount -t $SYSFS $SYSDEV $SYSMOUNT -o rw,discard
73}
74
75unmount_system() {
76 umount $SYSMOUNT
77}
78
79determine_system_mount
80
kmobse051cf32010-06-24 22:36:11 -050081case "$1" in
Warren Togamib1637c82012-03-03 22:37:42 -100082 backup)
Davide Garberie40c3182019-09-24 20:18:11 +020083 mount_system
z3DD3r284ec832020-03-04 18:07:40 +030084 if check_prereq; then
85 mkdir -p $C
86 preserve_addon_d
87 run_stage pre-backup
88 run_stage backup
89 run_stage post-backup
90 fi
Davide Garberie40c3182019-09-24 20:18:11 +020091 unmount_system
Warren Togamib1637c82012-03-03 22:37:42 -100092 ;;
93 restore)
Davide Garberie40c3182019-09-24 20:18:11 +020094 mount_system
z3DD3r284ec832020-03-04 18:07:40 +030095 if check_prereq; then
96 run_stage pre-restore
97 run_stage restore
98 run_stage post-restore
99 restore_addon_d
100 rm -rf $C
101 sync
102 fi
Davide Garberie40c3182019-09-24 20:18:11 +0200103 unmount_system
Warren Togamib1637c82012-03-03 22:37:42 -1000104 ;;
105 *)
106 echo "Usage: $0 {backup|restore}"
107 exit 1
kmobse051cf32010-06-24 22:36:11 -0500108esac
109
110exit 0