diff --git a/commands.c b/commands.c index a60366f..7617a8d 100644 --- a/commands.c +++ b/commands.c @@ -759,6 +759,45 @@ cmd_done(const char *name, void *cookie, int argc, const char *argv[], } +static int +cmd_backup_rom(const char *name, void *cookie, int argc, const char *argv[], + PermissionRequestList *permissions) +{ + UNUSED(cookie); + CHECK_WORDS(); + + char* backup_name = NULL; + switch(argc) + { + case 0: + break; + case 1: + backup_name = argv[0]; + break; + default: + LOGE("Command %s requires zero or one argument\n", name); + return 1; + } + + return do_nandroid_backup(backup_name); +} + +static int +cmd_restore_rom(const char *name, void *cookie, int argc, const char *argv[], + PermissionRequestList *permissions) +{ + UNUSED(cookie); + CHECK_WORDS(); + + if (argc != 1) { + LOGE("Command %s requires exactly one argument\n", name); + return 1; + } + + return do_nandroid_restore(argv[0]); +} + + /* * Function definitions */ @@ -1122,6 +1161,12 @@ register_update_commands(RecoveryCommandContext *ctx) ret = registerCommand("done", CMD_ARGS_WORDS, cmd_done, (void *)ctx); if (ret < 0) return ret; + ret = registerCommand("backup_rom", CMD_ARGS_WORDS, cmd_backup_rom, (void *)ctx); + if (ret < 0) return ret; + + ret = registerCommand("restore_rom", CMD_ARGS_WORDS, cmd_restore_rom, (void *)ctx); + if (ret < 0) return ret; + /* * Functions */ diff --git a/extendedcommands.c b/extendedcommands.c index 1c0a130..e9fd1a7 100644 --- a/extendedcommands.c +++ b/extendedcommands.c @@ -325,16 +325,36 @@ system(const char *command) return (pid == -1 ? -1 : pstat); } -void do_nandroid_backup() +int do_nandroid_backup(char* backup_name) { + char cmd[PATH_MAX]; + if (NULL == backup_name) + backup_name = ""; + sprintf(cmd, "/sbin/nandroid-mobile.sh backup /sdcard/clockworkmod/backup/ %s", backup_name); ui_print("Performing backup...\n"); - int ret = system("/sbin/nandroid-mobile.sh backup /sdcard/clockworkmod/backup/"); + int ret = system(cmd); if (ret != 0) { ui_print("Error while backing up! Error code: %d\n", ret); - return; + return ret; } ui_print("Backup complete.\n"); + return ret; +} + +int do_nandroid_restore(char* backup_path) +{ + char* command[PATH_MAX]; + sprintf(command, "nandroid-mobile.sh restore %s", backup_path); + ui_print("Performing restore...\n"); + int ret = system(command); + if (ret != 0) + { + ui_print("Error while restoring!\n"); + return ret; + } + ui_print("Restore complete.\n"); + return ret; } void show_nandroid_restore_menu() @@ -352,16 +372,7 @@ void show_nandroid_restore_menu() char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers); if (file == NULL) return; - char* command[PATH_MAX]; - sprintf(command, "nandroid-mobile.sh restore %s", file); - ui_print("Performing restore...\n"); - int ret = system(command); - if (ret != 0) - { - ui_print("Error while restoring!\n"); - return; - } - ui_print("Restore complete.\n"); + do_nandroid_restore(file); } void do_mount_usb_storage() @@ -456,5 +467,5 @@ int amend_main(int argc, char** argv) if (register_update_commands(&ctx)) { LOGE("Can't install update commands\n"); } - return run_and_remove_extendedcommand(argv[1]); -} + return run_script(argv[1]); +} \ No newline at end of file diff --git a/extendedcommands.h b/extendedcommands.h index 5629c41..12d6107 100644 --- a/extendedcommands.h +++ b/extendedcommands.h @@ -16,11 +16,17 @@ get_allow_toggle_display(); void ui_set_show_text(int value); -void +int do_nandroid_backup(); +int +do_nandroid_restore(); + void show_nandroid_restore_menu(); void -do_mount_usb_storage(); \ No newline at end of file +do_mount_usb_storage(); + +void +show_choose_zip_menu(); diff --git a/nandroid/nandroid-mobile.sh b/nandroid/nandroid-mobile.sh index 3f516d6..36cb836 100755 --- a/nandroid/nandroid-mobile.sh +++ b/nandroid/nandroid-mobile.sh @@ -171,7 +171,7 @@ case $1 in break ;; *) - echo "Usage: $0 {backup|restore} [/path/to/nandroid/backup/]" + echo "Usage: $0 {backup|restore} [/path/to/nandroid/backup/] [backupname]" echo "- backup will store a full system backup on /sdcard/nandroid" echo "- restore path will restore the last made backup for boot, system, recovery and data" exit 12 @@ -192,13 +192,18 @@ case $FAIL in 3) echo "Error mounting sdcard read-write"; umount /system /data /sdcard; exit 15;; esac -TIMESTAMP="`date +%Y-%m-%d-%H%M`" +if [ -z "$3" ] +then + BACKUPNAME="`date +%Y-%m-%d-%H%M`" +else + BACKUPNAME=$3 +fi BASEDIR=/sdcard/nandroid if [ ! -z "$2" ]; then BASEDIR=$2 fi -DESTDIR=$BASEDIR/$TIMESTAMP +DESTDIR=$BASEDIR/$BACKUPNAME if [ ! -d $DESTDIR ]; then mkdir -p $DESTDIR if [ ! -d $DESTDIR ]; then @@ -297,6 +302,8 @@ echo "done" # 8. echo "unmounting system, data and sdcard" +sync +sleep 2s umount /system umount /data umount /sdcard