summaryrefslogtreecommitdiff
path: root/andbackup.sh
diff options
context:
space:
mode:
Diffstat (limited to 'andbackup.sh')
-rwxr-xr-xandbackup.sh109
1 files changed, 68 insertions, 41 deletions
diff --git a/andbackup.sh b/andbackup.sh
index 9bb3326..52a24cc 100755
--- a/andbackup.sh
+++ b/andbackup.sh
@@ -1,4 +1,4 @@
-#!/system/xbin/bash
+#!/system/bin/sh
#
# Andbackup performs Android backups and restores.
# Copyright (C) 2016 Aaron Ball <nullspoon@oper.io>
@@ -21,22 +21,48 @@ set -u
backups=/sdcard/andbackup
-function backup_app {
+log() {
+ logtype=${1:-}
+ logmsg=${2:-}
+ logdate=$(date '+%T %F')
+
+ printf "%s %s %s\n" "${logdate}" "${logtype}" "${logmsg}"
+}
+
+# Some useful macros
+linfo() {
+ log "info " "${1:-}"
+}
+
+lerror() {
+ log "error" "${1:-}"
+}
+
+lwarn() {
+ log "warn " "${1:-}"
+}
+
+lfatal() {
+ log "fatal" "${1:-}"
+}
+
+
+backup_app() {
local app=${1:-}
# Make sure app is specified
if [[ -z ${app} ]]; then
- echo "Please specify an app to backup."
+ lerror "Please specify an app to backup."
return 1
fi
# Make sure app is installed
if [[ $(dumpsys package ${app} | wc -l) -eq 0 ]]; then
- echo "Package ${app} appears to not be installed."
+ lerror "Package ${app} appears to not be installed."
return 1
fi
- echo "Backing up ${app}"
+ linfo "Backing up ${app}"
# Stop the application if it is running
am force-stop ${app}
@@ -53,7 +79,7 @@ function backup_app {
if [[ -f ${code}/base.apk ]]; then
cp ${code}/base.apk ${backups}/${app}/base.apk
else
- echo "${app} apk file could not be found. Skipping apk installer backup."
+ linfo "${app} apk file could not be found. Skipping apk installer backup."
fi
# Copy the user data
@@ -65,7 +91,7 @@ function backup_app {
fi
# Compress the backup
- echo "Compressing userdata for ${app}"
+ linfo "Compressing userdata for ${app}"
cd ${backups}/${app}/
tar -c data | gzip -c > data.tar.gz
rm -rf data
@@ -75,33 +101,33 @@ function backup_app {
# function list_apps {
# cd ${userdata}
# for dir in *; do
-# echo ${dir%-*}
+# linfo ${dir%-*}
# done
# }
-function restore_app {
+restore_app() {
local app=${1:-}
# Make sure app is specified
if [[ -z ${app} ]]; then
- echo "Please specify an app to restore."
+ lerror "Please specify an app to restore."
return 1
fi
# Check that backup exists to be restored
if [[ ! -d ${backups}/${app} ]]; then
- echo "No backup for ${app} exists."
+ lerror "No backup for ${app} exists."
return 1
fi
- echo "Restoring ${app}"
+ linfo "Restoring ${app}"
# Install app if it is not yet installed
if [[ ! -f "${backups}/${app}/base.apk" ]]; then
- echo "Installer for ${app} not found. Only restoring data."
+ linfo "Installer for ${app} not found. Only restoring data."
elif [[ $(dumpsys package ${app} | grep -c userId) -eq 0 ]]; then
- echo "Installer detected but package ${app} is not installed. Installing"
+ linfo "Installer detected but package ${app} is not installed. Installing"
pm install ${backups}/${app}/base.apk
fi
@@ -114,7 +140,7 @@ function restore_app {
local data=$(dumpsys package ${app} | grep dataDir | cut -d'=' -f2 | head -n1)
# Decompress backup
- echo "Decompressing user data for ${app}."
+ linfo "Decompressing user data for ${app}."
cd ${backups}/${app}/
gzip -d -c data.tar.gz | tar -x
@@ -123,16 +149,17 @@ function restore_app {
# Fix data permissions
chown -R ${owner}:${owner} ${data}
# Fix selinux labels
- restorecon -R ${data}
+ linfo "Restoring SELinux contexs for ${app}"
+ restorecon -R ${data} 2>/dev/null
# Cleanup the extracted data so restores don't take too much space
rm -rf ${backups}/${app}/data
}
-function backup_apps {
+backup_apps() {
local apps=${@}
- [[ -z ${apps[@]:-} ]] && echo "At least one app is required." && return 1
+ [[ -z ${apps[@]:-} ]] && lerror "At least one app is required." && return 1
for app in ${apps[@]}; do
backup_app ${app}
@@ -140,9 +167,9 @@ function backup_apps {
}
-function restore_apps {
+restore_apps() {
local apps=${@}
- [[ -z ${apps[@]:-} ]] && echo "At least one app is required." && return 1
+ [[ -z ${apps[@]:-} ]] && log error "At least one app is required." && return 1
for app in ${apps[@]}; do
restore_app ${app}
@@ -150,9 +177,9 @@ function restore_apps {
}
-function list_backup_apps {
+list_backup_apps() {
local list=${1:-}
- [[ -z ${list} ]] && echo "A backup list is required." && return 1
+ [[ -z ${list} ]] && printf "A backup list is required." && return 1
for app in $(cat ${list}); do
backup_app ${app}
@@ -160,9 +187,9 @@ function list_backup_apps {
}
-function list_restore_apps {
+list_restore_apps() {
local list=${1}
- [[ -z ${list} ]] && echo "A backup list is required." && return 1
+ [[ -z ${list} ]] && printf "A backup list is required." && return 1
for app in $(cat ${list}); do
restore_app ${app}
@@ -170,36 +197,36 @@ function list_restore_apps {
}
-function main {
+main() {
local cmd=${1:-}
shift
# Ensure root is running this script
- if [[ $(id -u) -gt 0 ]]; then
- echo "Script must be run as root (uid 0)."
- exit 1
+ if [ $(id -u) -gt 0 ]; then
+ lerror "Script must be run as root (uid 0)."
+ return 1
fi
- if [[ ${cmd} == 'backup' ]]; then
+ if [ "${cmd}" = 'backup' ]; then
backup_apps ${@}
- elif [[ ${cmd} == 'listbackup' ]]; then
+ elif [ "${cmd}" = 'listbackup' ]; then
list_backup_apps ${@}
- elif [[ ${cmd} == 'restore' ]]; then
+ elif [ "${cmd}" = 'restore' ]; then
restore_apps ${@}
- elif [[ ${cmd} == 'listrestore' ]]; then
+ elif [ "${cmd}" = 'listrestore' ]; then
list_restore_apps ${@}
- elif [[ ${cmd} == 'ls' || ${cmd} == 'list' ]]; then
+ elif [ "${cmd}" = 'ls' ] || [ "${cmd}" = 'list' ]; then
list_apps
else
- echo "Please specify a command"
- echo -e "Available commands are...\n"\
- " backup\n"\
- " listbackup\n"\
- " restore\n"\
- " listrestore\n"\
- " list\n"
- exit 1
+ printf "Please specify a command\n"
+ printf "Available commands are...\n"
+ printf " backup\n"
+ printf " listbackup\n"
+ printf " restore\n"
+ printf " listrestore\n"
+ printf " list\n"
+ return 1
fi
}

Generated by cgit