lixuewei / rt-n56u

Automatically exported from code.google.com/p/rt-n56u
0 stars 0 forks source link

Feature request (patch available): Storage restore from backup file #1482

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
We want to be able to spread our settings and storage scripts to fresh routers 
easyly.
So let's give users oportunity to restore Storage from backup file.

Here's the patch against your current trunk:
https://github.com/vanaf/rt-n56u/commit/a4238e5ba993706232adea567f51964297a0844b

From a4238e5ba993706232adea567f51964297a0844b Mon Sep 17 00:00:00 2001
From: Ivan Afonichev <iafonichev@griddynamics.com>
Date: Wed, 6 May 2015 23:13:28 -0700
Subject: [PATCH] Add storage restore from backup feature

---
 trunk/user/httpd/web_ex.c                          | 61 +++++++++++++++-------
 trunk/user/scripts/mtd_storage.sh                  | 37 ++++++-------
 trunk/user/www/dict/EN.footer                      |  3 ++
 trunk/user/www/dict/RU.dict                        |  3 ++
 .../Advanced_SettingBackup_Content.asp             | 39 +++++++++++++-
 5 files changed, 103 insertions(+), 40 deletions(-)

diff --git a/trunk/user/httpd/web_ex.c b/trunk/user/httpd/web_ex.c
index 9fde9f0..9bf271e 100644
--- a/trunk/user/httpd/web_ex.c
+++ b/trunk/user/httpd/web_ex.c
@@ -76,6 +76,8 @@
 #define PROFILE_HEADER_NEW "HDR2"
 #define PROFILE_FIFO_UPLOAD    "/tmp/settings_u.prf"
 #define PROFILE_FIFO_DOWNLOAD  "/tmp/settings_d.prf"
+#define STORAGE_FIFO_FILENAME   "/tmp/.storage_tar.bz2"
+

 static int apply_cgi_group(webs_t wp, int sid, struct variable *var, const char *groupName, int flag);
 static int nvram_generate_table(webs_t wp, char *serviceId, char *groupName);
@@ -3594,7 +3596,7 @@ checkcrc_end:
 }

 static int chk_image_err = 1;
-static int chk_nvram_err = 1;
+static int upload_err = 1;

 static void
 do_upgrade_post(const char *url, FILE *stream, int clen, char *boundary)
@@ -3725,15 +3727,24 @@ do_upgrade_cgi(const char *url, FILE *stream)
 static void
 do_upload_post(const char *url, FILE *stream, int clen, char *boundary)
 {
+
    FILE *fifo = NULL;
-   char upload_fifo[] = PROFILE_FIFO_UPLOAD;
+   char *upload_fifo = PROFILE_FIFO_UPLOAD;
    char buf[1024];
    int cnt, count, offset, ret, ch;
    long filelen;
    char valid_header = 0;

    ret = EINVAL;
-   chk_nvram_err = 1;
+   upload_err = 1;
+
+        char *action_mode = websGetVar(wp, "action_mode", "uploadSettings");
+
+   if (strcmp(action_mode, "uploadStorage")) {
+       upload_fifo = STORAGE_FIFO_FILENAME;
+       valid_header = 1;
+   }
+

    /* Look for our part */
    while (clen > 0) {
@@ -3781,11 +3792,12 @@ do_upload_post(const char *url, FILE *stream, int clen, 
char *boundary)
            offset = 0;
            cnt++;

-           ret = check_nvram_header(buf, &filelen);
-           if (ret != 0)
-               goto err;
-           
-           valid_header = 1;
+           if (valid_header != 1){
+               ret = check_nvram_header(buf, &filelen);
+               if (ret != 0)
+                   goto err;
+               valid_header = 1;
+           }
        }

        filelen -= count;
@@ -3822,22 +3834,33 @@ err:
            break;

    if ((ret == 0) && (valid_header))
-       chk_nvram_err = 0;
+       upload_err = 0;
 }

 static void
 do_upload_cgi(const char *url, FILE *stream)
 {
-   /* Reboot if successful */
-   if (chk_nvram_err == 0) {
-       doSystem("killall %s %s", "-q", "watchdog");
-       sleep(1);
-       websApply(stream, "Uploading.asp");
-       eval("nvram", "restore", PROFILE_FIFO_UPLOAD);
-       nvram_commit();
-       sys_reboot();
+        char *upload_file = PROFILE_FIFO_UPLOAD;
+
+        char *action_mode = websGetVar(wp, "action_mode", "uploadSettings");
+        if (strcmp(action_mode, "uploadStorage")) {
+       upload_file = STORAGE_FIFO_FILENAME; 
+   }
+
+   if (upload_err == 0) {
+       if (strcmp(action_mode, "uploadStorage")) {
+                   doSystem("/sbin/mtd_storage.sh %s", "restore");
+       }else{
+               /* Reboot if successful */
+           doSystem("killall %s %s", "-q", "watchdog");
+           sleep(1);
+           websApply(stream, "Uploading.asp");
+           eval("nvram", "restore", upload_file);
+           nvram_commit();
+           sys_reboot();
+       }
    } else {
-       unlink(PROFILE_FIFO_UPLOAD);
+       unlink(upload_file);
        websApply(stream, "UploadError.asp");
    }
 }
@@ -3858,7 +3881,7 @@ do_nvram_file(const char *url, FILE *stream)
 static void
 do_storage_file(const char *url, FILE *stream)
 {
-   const char *storage_file = "/tmp/.storage_tar.bz2";
+   const char *storage_file = STORAGE_FIFO_FILENAME;

    unlink(storage_file);
    if (get_login_safe()) {
diff --git a/trunk/user/scripts/mtd_storage.sh 
b/trunk/user/scripts/mtd_storage.sh
index 3e65daf..015cad2 100755
--- a/trunk/user/scripts/mtd_storage.sh
+++ b/trunk/user/scripts/mtd_storage.sh
@@ -25,15 +25,17 @@ func_get_mtd()

 func_load()
 {
-   local fsz
+   local fsz tbz2

    [ ! -d "$dir_storage" ] && mkdir -p -m 755 $dir_storage
    echo "Loading files from mtd partition \"$mtd_part_dev\""

+   tbz2="${tmp}.bz2"
+   cat $mtd_part_dev > $tbz2 2>/dev/null
    bzcat $mtd_part_dev > $tmp 2>/dev/null
    fsz=`stat -c %s $tmp 2>/dev/null`
    if [ -n "$fsz" ] && [ $fsz -gt 0 ] ; then
-       md5sum $tmp > $hsh
+       md5sum $tbz2 > $hsh
        tar xf $tmp -C $dir_storage 2>/dev/null
        echo "Done."
    else
@@ -49,26 +51,16 @@ func_save()
 {
    local fsz tbz2

-   [ -f "$ers" ] && return 1
-
-   [ ! -d "$dir_storage" ] && mkdir -p -m 755 $dir_storage
-   echo "Save files to mtd partition \"$mtd_part_dev\""
-
    tbz2="${tmp}.bz2"
-   rm -f $tmp
-   rm -f $tbz2
-   cd $dir_storage
-   find * -print0 | xargs -0 touch -c -h -t 201001010000.00
-   find * ! -type d -print0 | sort -z | xargs -0 tar -cf $tmp 2>/dev/null
-   cd - >>/dev/null
+   
+
    md5sum -c -s $hsh 2>/dev/null
    if [ $? -eq 0 ] ; then
        echo "Storage hash is not changed, skip write to mtd partition. Exit."
-       rm -f $tmp
-       return 0
+       rm -f $tbz2
+       exit 0
    fi
-   [ -f "$tmp" ] && md5sum $tmp > $hsh
-   bzip2 -9 $tmp 2>/dev/null
+   [ -f "$tbz2" ] && md5sum $tbz2 > $hsh
    fsz=`stat -c %s $tbz2 2>/dev/null`
    if [ -n "$fsz" ] && [ $fsz -gt 0 ] && [ $fsz -lt $mtd_part_size ] ; then
        mtd_write write $tbz2 $mtd_part_name
@@ -82,7 +74,6 @@ func_save()
        result=1
        echo "Error! Invalid storage data size: $fsz"
    fi
-   rm -f $tmp
    rm -f $tbz2
 }

@@ -475,8 +466,14 @@ load)
    ;;
 save)
    func_get_mtd
-   func_save
+   func_backup
+   [ $result -eq 0 ] && func_save
    ;;
+restore)
+   func_get_mtd
+   func_save
+   [ $result -eq 0 ] && func_load
+        ;;
 backup)
    func_backup
    ;;
@@ -492,7 +489,7 @@ fill)
    func_fill
    ;;
 *)
-   echo "Usage: $0 {load|save|backup|erase|reset|fill}"
+   echo "Usage: $0 {load|save|restore|backup|erase|reset|fill}"
    exit 1
    ;;
 esac
diff --git a/trunk/user/www/dict/EN.footer b/trunk/user/www/dict/EN.footer
index 2aa67c9..bd16015 100644
--- a/trunk/user/www/dict/EN.footer
+++ b/trunk/user/www/dict/EN.footer
@@ -124,6 +124,9 @@ APCliRole=Wireless Client Role:
 IPConnection_VSList_ftpport=FTP Server Port:
 CTL_Commit=Commit
 Commit_confirm=Save Settings (NVRAM) Permanently?
+Storage_upload_itemname=Restore Storage from a Backup file:
+Storage_upload_itemdesc=Specify the path and name of storage backup file. Then 
click [Upload] to write the storage to router.
+Storage_upload_hint=File format or path is invalid!
 Adm_Setting_nvram=Router Settings (NVRAM)
 Adm_Setting_store=Router Internal Storage (/etc/storage)
 Adm_Setting_store_backup=Storage Backup:
diff --git a/trunk/user/www/dict/RU.dict b/trunk/user/www/dict/RU.dict
index e168e62..a7cab9b 100644
--- a/trunk/user/www/dict/RU.dict
+++ b/trunk/user/www/dict/RU.dict
@@ -138,6 +138,9 @@ APCliRole=Роль беспроводного 
клиента:
 IPConnection_VSList_ftpport=Порт FTP-сервера:
 CTL_Commit=Записать
 Commit_confirm=Записать настройки маршрутизатора (NVRAM) во флеш-память?
+Storage_upload_itemname=Восстановление хранилища из 
файла резервной копии:
+Storage_upload_itemdesc=Укажите путь и имя файла 
резервной копии хранилища. Затем нажмите 
«Загрузить», чтобы загрузить хранилище в 
маршрутизатор.
+Storage_upload_hint=Недопустимый формат или путь 
файла!
 Adm_Setting_nvram=Настройки маршрутизатора (NVRAM)
 Adm_Setting_store=Хранилище файлов (/etc/storage)
 Adm_Setting_store_backup=Резервная копия хранилища:
diff --git 
a/trunk/user/www/n56u_ribbon_fixed/Advanced_SettingBackup_Content.asp 
b/trunk/user/www/n56u_ribbon_fixed/Advanced_SettingBackup_Content.asp
index 45efd7d..a0a1507 100644
--- a/trunk/user/www/n56u_ribbon_fixed/Advanced_SettingBackup_Content.asp
+++ b/trunk/user/www/n56u_ribbon_fixed/Advanced_SettingBackup_Content.asp
@@ -45,7 +45,9 @@ function initial(){
        showhide_div('row_nv_restore2', 1);
        showhide_div('row_st_reset', 1);
        showhide_div('row_st_backup', 1);
-   }
+                showhide_div('row_st_restore1', 1);
+                showhide_div('row_st_restore2', 1);
+   }

    if (support_mtd_rwfs())
        showhide_div('tbl_rwfs', 1);
@@ -164,6 +166,29 @@ function uploadSetting(){
    }
 }

+function uploadStorage(){
+        document.form.file.name = "file_nv"; 
+        var file_obj = document.form.file_st;
+        file_obj.name="file";
+        if(file_obj.value == ""){
+                alert("<#JS_fieldblank#>");
+                file_obj.focus();
+        }
+        else if(file_obj.value.length < 6 ||
+                                        file_obj.value.lastIndexOf(".TBZ") < 0 
||
+                                        file_obj.value.lastIndexOf(".TBZ") != 
(file_obj.value.length)-4){
+                alert("<#Storage_upload_hint#>");
+                file_obj.focus();
+        }
+        else{
+                disableCheckChangedStatus();
+                switch_form_action(1, "uploadStorage");
+                document.form.submit();
+        }
+}
+
+
+
 $j.fn.fileName = function() {
    var $this = $j(this),
    $val = $this.val(),
@@ -369,6 +394,18 @@ $j.fn.fileName = function() {
                                                 <input name="st_action2" class="btn btn-info" style="width: 219px;" onclick="saveStorage();" type="button" value="<#CTL_onlysave#>"/>
                                             </td>
                                         </tr>
+                                        <tr id="row_st_restore1" 
style="display:none">
+                                            <th><a class="help_tooltip" 
href="javascript:void(0);" 
onmouseover="openTooltip(this,19,3)"><#Storage_upload_itemname#></a></th>
+                                            <td>
+                                                <input name="file_st" 
type="file" size="36" />
+                                            </td>
+                                        </tr>
+                                        <tr id="row_st_restore2" 
style="display:none">
+                                            <th style="border-top: 0 none; 
padding-top: 0px;"></th>
+                                            <td style="border-top: 0 none; 
padding-top: 0px;">
+                                                <input name="uploadbutton" 
class="btn btn-info" style="width: 219px;" onclick="uploadStorage();" 
type="button" value="<#CTL_upload#>"/>
+                                            </td>
+                                        </tr>
                                         <tr>
                                             <th width="50%"><#Adm_Setting_store_stats#></th>
                                             <td align="left">
-- 
2.1.0

Original issue reported on code.google.com by ivan.afo...@gmail.com on 7 May 2015 at 7:16

GoogleCodeExporter commented 9 years ago
Thanks.
Reworked variant is applied in commit

http://code.google.com/p/rt-n56u/source/detail?r=69382fa95b5582c4017fe09ddbefed7
737023f82

Original comment by andy.pad...@gmail.com on 14 May 2015 at 6:33

GoogleCodeExporter commented 9 years ago

Original comment by andy.pad...@gmail.com on 1 Jun 2015 at 2:34