kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 1 | #!/sbin/sh |
2 | # | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 3 | # Backup and restore addon /system files |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 4 | # |
5 | |||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 6 | export C=/tmp/backupdir |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 7 | export SYSDEV="$(readlink -nf "$2")" |
8 | export SYSFS="$3" | ||||
Michael Bestas | 42020e2 | 2020-01-03 02:03:00 +0200 | [diff] [blame] | 9 | export V=17.1 |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 10 | |
Tom Marshall | 65aff66 | 2014-12-12 11:56:04 -0800 | [diff] [blame] | 11 | # Scripts in /system/addon.d expect to find backuptool.functions in /tmp |
12 | cp -f /tmp/install/bin/backuptool.functions /tmp | ||||
13 | |||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 14 | # Preserve /system/addon.d in /tmp/addon.d |
15 | preserve_addon_d() { | ||||
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 16 | if [ -d $S/addon.d/ ]; then |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 17 | mkdir -p /tmp/addon.d/ |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 18 | cp -a $S/addon.d/* /tmp/addon.d/ |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 19 | chmod 755 /tmp/addon.d/*.sh |
20 | fi | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 21 | } |
22 | |||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 23 | # Restore /system/addon.d from /tmp/addon.d |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 24 | restore_addon_d() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 25 | if [ -d /tmp/addon.d/ ]; then |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 26 | mkdir -p $S/addon.d/ |
27 | cp -a /tmp/addon.d/* $S/addon.d/ | ||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 28 | rm -rf /tmp/addon.d/ |
29 | fi | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 30 | } |
31 | |||||
Chirayu Desai | 700fc8f | 2012-12-13 19:36:37 +0530 | [diff] [blame] | 32 | # Proceed only if /system is the expected major and minor version |
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 33 | check_prereq() { |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 34 | # If there is no build.prop file the partition is probably empty. |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 35 | if [ ! -r $S/build.prop ]; then |
z3DD3r | fa8a442 | 2020-03-04 12:42:10 +0300 | [diff] [blame^] | 36 | return 0 |
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 37 | fi |
LuK1337 | 57490e6 | 2018-12-05 20:51:21 +0100 | [diff] [blame] | 38 | if ! grep -q "^ro.lineage.version=$V.*" $S/build.prop; then |
Adrian DC | 29fca64 | 2018-05-17 23:06:24 +0200 | [diff] [blame] | 39 | echo "Not backing up files from incompatible version: $V" |
z3DD3r | fa8a442 | 2020-03-04 12:42:10 +0300 | [diff] [blame^] | 40 | return 0 |
Adrian DC | 29fca64 | 2018-05-17 23:06:24 +0200 | [diff] [blame] | 41 | fi |
z3DD3r | fa8a442 | 2020-03-04 12:42:10 +0300 | [diff] [blame^] | 42 | return 1 |
Ricardo Cerqueira | 4f4c30b | 2013-08-19 04:00:05 +0100 | [diff] [blame] | 43 | } |
44 | |||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 45 | # Execute /system/addon.d/*.sh scripts with $1 parameter |
46 | run_stage() { | ||||
Andreas Schneider | 9cfe968 | 2015-09-03 11:04:58 +0200 | [diff] [blame] | 47 | if [ -d /tmp/addon.d/ ]; then |
48 | for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do | ||||
49 | $script $1 | ||||
50 | done | ||||
51 | fi | ||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 52 | } |
53 | |||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 54 | determine_system_mount() { |
55 | if grep -q -e"^$SYSDEV" /proc/mounts; then | ||||
56 | umount $(grep -e"^$SYSDEV" /proc/mounts | cut -d" " -f2) | ||||
57 | fi | ||||
58 | |||||
59 | if [ -d /mnt/system ]; then | ||||
60 | SYSMOUNT="/mnt/system" | ||||
61 | elif [ -d /system_root ]; then | ||||
62 | SYSMOUNT="/system_root" | ||||
63 | else | ||||
64 | SYSMOUNT="/system" | ||||
65 | fi | ||||
66 | |||||
67 | export S=$SYSMOUNT/system | ||||
68 | } | ||||
69 | |||||
70 | mount_system() { | ||||
71 | mount -t $SYSFS $SYSDEV $SYSMOUNT -o rw,discard | ||||
72 | } | ||||
73 | |||||
74 | unmount_system() { | ||||
75 | umount $SYSMOUNT | ||||
76 | } | ||||
77 | |||||
78 | determine_system_mount | ||||
79 | |||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 80 | case "$1" in |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 81 | backup) |
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 82 | mount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 83 | mkdir -p $C |
z3DD3r | fa8a442 | 2020-03-04 12:42:10 +0300 | [diff] [blame^] | 84 | if ! check_prereq; then |
85 | unmount_system | ||||
86 | exit 127 | ||||
87 | end | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 88 | preserve_addon_d |
89 | run_stage pre-backup | ||||
90 | run_stage backup | ||||
91 | run_stage post-backup | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 92 | unmount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 93 | ;; |
94 | restore) | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 95 | mount_system |
z3DD3r | fa8a442 | 2020-03-04 12:42:10 +0300 | [diff] [blame^] | 96 | if ! check_prereq; then |
97 | unmount_system | ||||
98 | exit 127 | ||||
99 | end | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 100 | run_stage pre-restore |
101 | run_stage restore | ||||
102 | run_stage post-restore | ||||
103 | restore_addon_d | ||||
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 104 | rm -rf $C |
105 | sync | ||||
Davide Garberi | e40c318 | 2019-09-24 20:18:11 +0200 | [diff] [blame] | 106 | unmount_system |
Warren Togami | b1637c8 | 2012-03-03 22:37:42 -1000 | [diff] [blame] | 107 | ;; |
108 | *) | ||||
109 | echo "Usage: $0 {backup|restore}" | ||||
110 | exit 1 | ||||
kmobs | e051cf3 | 2010-06-24 22:36:11 -0500 | [diff] [blame] | 111 | esac |
112 | |||||
113 | exit 0 |