forum-msg-id-129424-edit

Original date:2016-02-16 12:12:11 Edited by: K_D_R Subject: Simple Backup with Time Stamp

I use this WEE. I need to add support for subdirectories and compressing the Archive folder(s) with tar.

-- 
--   backup.e 
-- 
--   backup current file, current directory,  
--   or a list of files saved to a text file with the file extension "prj" 
--   "wee.prj" for example. 
  
--  creates directory,"Archive", in current directory 
 
--  all files in current directory may be  copied to a  
--  subdirectory named with a timestamp, "dt". 
 
--  all files listed in a text file with the extension "prj" 
--  may be copied to  a subdirectory labeled "filename.prj" 
--  in a subdirectory labeled with a timestamp. 
 
--test  -- the current directory is: /home/kenneth/wee 
--backupfiles("backup.e")   -- the file "backup.e" is placed in /home/kenneth/wee/Archive/backup.e/dt 
--backupfiles()             -- all files in current directory placed in /home/kenneth/wee/Archive/dt 
--backupfiles("wee.prj")    -- all files listed in the file "wee.prj" placed /home/kenneth/wee/Archive/wee.prj/dt 
 
include std/filesys.e 
include std/sequence.e 
include std/text.e 
include std/error.e 
include std/console.e 
include std/sort.e 
include std/io.e 
 
enum PATH, FILES 
object TMP = {{},{}} 
sequence  todays_date, dt, BACKUPDIR 
object files ={} 
 
function load_directory_files(sequence path) 
      -- load files from current directory into sequence files 
      object file = "" 
       if chdir(path) then end if 
      TMP[PATH] = curdir() 
      TMP[FILES] = dir(curdir()) 
      if not atom(TMP[FILES])then 
            if length(TMP[FILES]) >=3 then 
                  for x = 3 to length(TMP[FILES]) do  
                  --  omit directories for now 
                        if not equal(TMP[FILES][x][D_ATTRIBUTES],  "d") then 
                              file = TMP[PATH] &TMP[FILES][x][D_NAME] 
                              file = abbreviate_path(file) 
                              files = append(files, file) 
                        end if 
                  end for 
            end if 
      end if 
      files = remove_item("ex.err", files) 
      return sort(files) 
end function 
 
 
global object brd = {0,""}, sourceDir = "" 
global function backupfiles(sequence file_or_list = "") 
      sequence msg ="" 
      object file = "" 
      atom BCF = 0 -- switch: 1 = backup current file 
       
      if equal(file_or_list, "") then 
--          backup all files in the current directory: backupfiles() 
            files = load_directory_files(sourceDir) 
            BACKUPDIR = sourceDir &SLASH &"Archive"  -- sourceDir set in ui_gtk.e 
              
      elsif equal("prj", fileext(file_or_list)) then   
--          backup files listed in a backup file list,  
--          which has a "prj" file extension: backupfiles("wee.prj")  
            BACKUPDIR = dirname(file_or_list) & SLASH & "Archive" 
            sourceDir = dirname(file_or_list) 
            BACKUPDIR = BACKUPDIR & SLASH & filebase(file_or_list) &"."& fileext(file_or_list) 
            files = read_lines(file_or_list) 
       
      else  
--          Backup the current file: backupfiles("wee.exw") 
            BACKUPDIR = dirname(file_or_list)& SLASH & "Archive" 
            BACKUPDIR = BACKUPDIR & SLASH & filebase(file_or_list) &"."& fileext(file_or_list) 
            BCF = 1 
            files = file_or_list 
      end if 
       
      if not atom(files) then 
            if file_exists(BACKUPDIR) or create_directory(BACKUPDIR) then 
            end if  
            todays_date= date() 
            dt =            sprintf("%04d",todays_date[1]+1900) &       -- year 
            sprintf("%02d",todays_date[2]) &                                     -- month 
            sprintf("%02d",todays_date[3])&                                      -- day 
            sprintf("%02d",todays_date[4])&                                      -- hour 
            sprintf("%02d",todays_date[5])                                         -- minute 
            BACKUPDIR = BACKUPDIR &SLASH& dt 
            if file_exists(BACKUPDIR) or create_directory(BACKUPDIR) then 
                  if BCF then  -- backup the current file 
                         if copy_file(file_or_list,  BACKUPDIR, 1 ) then  
                               msg &= sprintf("\n\tThe original  source code file, \"%s\", \n\thas been archived as: \"%s\"\n\n", {file_or_list, BACKUPDIR}) 
                              brd =  {1, msg} 
                        end if 
                  else 
--                        backup all files in the current directory, or backup files listed in "*.prj" file 
                        for x=1 to length(files) do 
                              printf(1, "%s\n", {files[x]}) 
                              if copy_file(sourceDir&SLASH&files[x],  BACKUPDIR, 1 ) then 
                              msg &= sprintf("\n\tThe original  source code file, \"%s\", \n\thas been archived as: \"%s\"\n\n", {files[x], BACKUPDIR}) 
                              brd = {1, msg} 
                              end if 
                        end for 
                  end if 
            else 
                  msg &= "\nBackup operation failed!\n" 
                  brd = {0, msg} 
            end if 
      else 
                  msg &= "\nBackup operation failed!\n" 
                  brd = {0, msg} 
      end if 
       
      return brd 
end function 
 
 
-- mods to ui_gtk.e 
add(runmenu, --< in routine, insert: 
create(GtkSeparatorMenuItem), 
createmenuitem("Backup Current File or Project", "RunStart"), 
createmenuitem("Backup All Files in Current Directory", "RunStart"), 
-- 
 
-- 
--    
include backup.e --< include before function RunStart(--- 
function RunStart(atom ctl)  --< in routine    
-- insert: 
 
      elsif equal(lbl, "Backup Current File or Project") then 
            brd = backupfiles(file_name ) 
            printf(1, "%s", {brd[2]}) 
             
       elsif equal(lbl, "Backup All Files in Current Directory")  then    
            sourceDir = dirname(file_name) 
            brd = backupfiles() 
            printf(1, "%s", {brd[2]}) 
Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu