it is now backup_HOST_rREV-HASH_YYYY-MM-DD_HHMM.tgz
HOST is without domain now, if fqdn was set
the extension changed form tar.gz to tgz
REV+HASH added for easier get of version
TIME added to allow more than 1 backup per day
added list of installed packages

From 37e9917a3f784b8f0338546129d4a319deaba013 Mon Sep 17 00:00:00 2001
From: Fritz D. Ansel <[email protected]>
Date: Mon, 2 Aug 2021 21:16:16 +0200
Subject: [PATCH] extend backup file name

it is now backup_HOST_rREV-HASH_YYYY-MM-DD_HHMM.tgz
HOST is without domain now, if fqdn was set
the extension changed form tar.gz to tgz
REV+HASH added for easier get of version
TIME added to allow more than 1 backup per day
added list of installed packages

Signed-off-by: Fritz D. Ansel <[email protected]>
---
 main.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/main.c b/main.c
index 95a62b8..c0aa15b 100644
--- a/main.c
+++ b/main.c
@@ -650,7 +650,7 @@ main_backup(int argc, char **argv)
                        return failure(500, errno, "Failed chdir('/')");
 
                execl("/sbin/sysupgrade", "/sbin/sysupgrade",
-                     "--create-backup", "-", NULL);
+                     "-k", "--create-backup", "-", NULL);
 
                return -1;
 
@@ -658,15 +658,31 @@ main_backup(int argc, char **argv)
                close(fds[1]);
 
                now = time(NULL);
-               strftime(datestr, sizeof(datestr) - 1, "%Y-%m-%d", 
localtime(&now));
+               strftime(datestr, sizeof(datestr) - 1, "%Y-%m-%d_%H%M", 
localtime(&now));
 
                if (gethostname(hostname, sizeof(hostname) - 1))
                        sprintf(hostname, "OpenWrt");
+               else {
+                       for (int i=0; i<(int)strlen(hostname); i++)
+                               if (hostname[i] == '.') {
+                                       hostname[i] = '\0';
+                                       break;
+                               }
+               }
+
+               char version[32] = "UNKNOWN";
+               FILE *fp;
+               if ((fp = fopen("/etc/openwrt_version", "r")) != NULL) {
+                       if (fgets(version, sizeof(version), fp) == version)
+                               if (version[strlen(version)-1] == '\n' )
+                                       version[strlen(version)-1] = '\0';
+                       fclose(fp);
+               }
 
                printf("Status: 200 OK\r\n");
                printf("Content-Type: application/x-targz\r\n");
                printf("Content-Disposition: attachment; "
-                      "filename=\"backup-%s-%s.tar.gz\"\r\n\r\n", hostname, 
datestr);
+                      "filename=\"backup_%s_%s_%s.tgz\"\r\n\r\n", hostname, 
version, datestr);
 
                fflush(stdout);
 
-- 
2.31.1

_______________________________________________
openwrt-devel mailing list
[email protected]
https://lists.openwrt.org/mailman/listinfo/openwrt-devel

Reply via email to