blob: 3178842cc1002e32bc4a3fa03a8fb1e15b629905 [file] [log] [blame]
Andrew Dodd0c209012013-09-28 15:34:25 -04001#!/sbin/sh
2#
3# Backup and restore addon /system files
4#
5
6export C=/tmp/backupdir
Davide Garberi9717dfa2019-09-24 20:18:11 +02007export SYSDEV="$(readlink -nf "$2")"
8export SYSFS="$3"
micky3877d554ae2023-07-20 16:21:18 +02009export V=13
Andrew Dodd0c209012013-09-28 15:34:25 -040010
Marko Man2d218732017-09-12 02:24:39 +020011DEBUG=0
12
Andrew Dodd0c209012013-09-28 15:34:25 -040013# Preserve /system/addon.d in /tmp/addon.d
14preserve_addon_d() {
maxwen1c2e98d2017-09-15 17:13:48 +020015 rm -rf /tmp/addon.d/
Andrew Dodd0c209012013-09-28 15:34:25 -040016 mkdir -p /tmp/addon.d/
LuK1337ae517382020-01-20 16:09:50 +000017 cp -a $S/addon.d/* /tmp/addon.d/
Andrew Dodd0c209012013-09-28 15:34:25 -040018 chmod 755 /tmp/addon.d/*.sh
19}
20
21# Restore /system/addon.d in /tmp/addon.d
22restore_addon_d() {
LuK1337ae517382020-01-20 16:09:50 +000023 cp -a /tmp/addon.d/* $S/addon.d/
Andrew Dodd0c209012013-09-28 15:34:25 -040024 rm -rf /tmp/addon.d/
25}
26
maxwen1c2e98d2017-09-15 17:13:48 +020027# Restore only if backup has the expected major and minor version
Andrew Dodd0c209012013-09-28 15:34:25 -040028check_prereq() {
z3DD3r31285e52020-03-04 18:07:40 +030029 # If there is no build.prop file the partition is probably empty.
30if [ ! -r $S/build.prop ]; then
31 echo "Backup/restore is not possible. Partition is probably empty"
z3DD3r8ca31e62020-03-04 12:42:10 +030032 return 1
z3DD3r31285e52020-03-04 18:07:40 +030033fi
34 if ( ! grep -q "^ro.build.version.release=$V.*" /tmp/build.prop ); then
35 echo "Backup/restore is not possible. Incompatible ROM version: $V"
36 return 2
37 fi
38 return 0
Andrew Dodd0c209012013-09-28 15:34:25 -040039}
40
Andrew Dodd0c209012013-09-28 15:34:25 -040041# Execute /system/addon.d/*.sh scripts with $1 parameter
42run_stage() {
maxwen1c2e98d2017-09-15 17:13:48 +020043 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
44 if [ $DEBUG -eq 1 ]; then
45 echo run_stage $script $1
46 fi
47 $script $1
48 done
Andrew Dodd0c209012013-09-28 15:34:25 -040049}
50
Davide Garberi9717dfa2019-09-24 20:18:11 +020051determine_system_mount() {
52 if grep -q -e"^$SYSDEV" /proc/mounts; then
53 umount $(grep -e"^$SYSDEV" /proc/mounts | cut -d" " -f2)
54 fi
55
56 if [ -d /mnt/system ]; then
57 SYSMOUNT="/mnt/system"
58 elif [ -d /system_root ]; then
59 SYSMOUNT="/system_root"
60 else
61 SYSMOUNT="/system"
62 fi
63
64 export S=$SYSMOUNT/system
65}
66
67mount_system() {
68 mount -t $SYSFS $SYSDEV $SYSMOUNT -o rw,discard
69}
70
71unmount_system() {
72 umount $SYSMOUNT
73}
74
75determine_system_mount
76
Andrew Dodd0c209012013-09-28 15:34:25 -040077case "$1" in
78 backup)
maxwen1c2e98d2017-09-15 17:13:48 +020079 # make sure we dont start with any leftovers
80 rm -rf $C
LuK1337ae517382020-01-20 16:09:50 +000081 cp $S/bin/backuptool.functions /tmp
82 cp $S/build.prop /tmp
Davide Garberi9717dfa2019-09-24 20:18:11 +020083 mount_system
z3DD3r31285e52020-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 Garberi9717dfa2019-09-24 20:18:11 +020091 unmount_system
Andrew Dodd0c209012013-09-28 15:34:25 -040092 ;;
93 restore)
LuK1337ae517382020-01-20 16:09:50 +000094 cp $S/bin/backuptool.functions /tmp
Davide Garberi9717dfa2019-09-24 20:18:11 +020095 mount_system
z3DD3r31285e52020-03-04 18:07:40 +030096 if check_prereq; then
97 run_stage pre-restore
98 run_stage restore
99 run_stage post-restore
100 restore_addon_d
101 rm -rf $C
102 sync
103 fi
Davide Garberi9717dfa2019-09-24 20:18:11 +0200104 unmount_system
Andrew Dodd0c209012013-09-28 15:34:25 -0400105 ;;
106 *)
107 echo "Usage: $0 {backup|restore}"
108 exit 1
109esac
110
111exit 0