Advertisement
Guest User

blah

a guest
May 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <unistd.h>
  5. #include <sys/stat.h>
  6. #include <dirent.h>
  7.  
  8.  
  9.  
  10. using namespace std;
  11.  
  12.  
  13.  
  14. int main(int argc, char *argv[]) {
  15.  
  16.     cout << endl << "--- ========= BCKUP%199 ========= ---" << endl << "\t-- running" << endl << endl;
  17.  
  18.  
  19.     int x=0;
  20.     int y=0;
  21.  
  22.     const int MAX = 2048;
  23.  
  24.     int is_regular_file(const char *path);
  25.  
  26.  
  27.     char ch='z';
  28.     char DESTINATION[MAX]; memset(DESTINATION,'\0',MAX);
  29.     char SOURCE[MAX]; memset(SOURCE,'\0',MAX);
  30.  
  31.     char directories[3][MAX] = { "/home/vt1/Documents", "/opt/BACKUP", 0 };
  32.  
  33.     char COMMAND[MAX]; memset(COMMAND,'\0', MAX);
  34.  
  35.     char tar_command[MAX]; memset(tar_command,'\0', MAX);
  36.     char zip7_command[MAX]; memset(zip7_command,'\0', MAX);
  37.  
  38.     strncpy(zip7_command, "7z a -bso0 -bsp0 '", (MAX-18));
  39.     strncpy(tar_command, "tar -zcf '", (MAX-10));
  40.  
  41.     char tar_extension[8] = ".tar.gz";
  42.     char zip7_extension[8] = ".7z";
  43.  
  44.     bool backup_dir_is_set=0;
  45.     bool source_dir_is_set=0;
  46.  
  47.     bool swV=0;
  48.     bool swT=0;
  49.     bool swX=0;
  50.     bool swQ=0;
  51.  
  52.     bool warn=0;
  53.  
  54.  
  55.  
  56.  
  57.     // displays the command line arguments
  58.  
  59.     if (swV) {
  60.         cout << "ARGV:" << "\tARGC: " << argc << endl << endl;
  61.  
  62.         for (int i=0; i<argc; i++) {
  63.             cout << "- " << *(argv+i) << endl;
  64.         }
  65.     }
  66.  
  67.     // regulates the command line switches
  68.     for (int i=0; i<argc; i++) {
  69.  
  70.         if (strlen(argv[i]) == 2 && *(argv+i)[0] == '-') {
  71.  
  72.             if (swV) {
  73.                 cout << "\t\tSUCCESS (" << argv[i] << ")" << endl;
  74.             }
  75.  
  76.             switch (argv[i][1]) {
  77.                 case 'q': // quiet
  78.                     swQ=1;
  79.  
  80.                     break;
  81.  
  82.                 case 'v': // verbos
  83.                     swV=1;
  84.  
  85.                     if (swV) {
  86.                         cout << "- verbose" << endl;
  87.                     }
  88.                     break;
  89.  
  90.                 case 't': // dry run
  91.                     swT=1;
  92.  
  93.                     if (swV) {
  94.                         cout << "- dry run ;; not actually backing up (-t)" << endl;
  95.                     }
  96.                     break;
  97.  
  98.                 case 'x': // override backup directoru
  99.                     swX=1;
  100.  
  101.                     if (((i+1) < argc) && swV) {
  102.                         cout << "- FOUND DIR OVERRIDE SWITCH: " << argv[i] << endl;
  103.                         cout << "\t-- changing backup dir" << endl;
  104.                     }
  105.                     break;
  106.  
  107.  
  108.                 default:
  109.  
  110.                     if (swV) {
  111.                         cout << "-- unknown option" << "\t" << argv[i] << endl;
  112.                     }                  
  113.                     break;             
  114.             }
  115.         }
  116.         else {
  117.             if ((i > 0) && strlen(argv[i]) >= 1 && *(argv+i)[0] != '-') {
  118.  
  119.                 if (swV) {
  120.                     cout << endl << "- looks like a directory" << endl;
  121.                     cout << "\t-- dir: " << argv[i];
  122.                 }
  123.  
  124.                 if (!source_dir_is_set && !swX) {
  125.  
  126.                     strncpy(SOURCE, argv[i], MAX);
  127.  
  128.                     if (swV) {
  129.                         cout << "\t-- set" << endl << endl;
  130.                     }
  131.  
  132.                     source_dir_is_set = 1;
  133.                 }
  134.                 else {
  135.  
  136.                     if ((i > 0) && swX && strlen(argv[i])>=1 && argv[i][0] != '-') {
  137.  
  138.                         if (!backup_dir_is_set) {
  139.                             strncpy(DESTINATION, *(argv+i), (MAX));
  140.                             cout << "\t-- set" << endl << endl;
  141.                             backup_dir_is_set = 1;
  142.                             swX=0;
  143.                         }
  144.  
  145.                     }
  146.                 }
  147.             }
  148.         }
  149.     } // end of FOR loop
  150.  
  151.  
  152.  
  153.     // check SOURCEDIR and BACKUPDIR
  154.     if (!source_dir_is_set) { warn=1; strncpy(SOURCE,directories[0],MAX); source_dir_is_set=1; }
  155.     if (!backup_dir_is_set) { strncpy(DESTINATION,directories[1],MAX); backup_dir_is_set=1; }
  156.  
  157.  
  158.     if (warn && !swQ) {
  159.         if (swV) {
  160.             cout << "WARNING: SOURCE DIRECTORY WAS NOT SPECIFIED" << endl << "SOURCE DIR SET TO DEFAULT LOCATION: (( " << SOURCE << " ))" << endl << endl;
  161.         }
  162.     }
  163.  
  164.     if (!swQ) {
  165.         cout << "SOURCE:\t\t" << SOURCE << endl;
  166.         cout << "DESTINATION:\t" << DESTINATION << endl << endl;
  167.     }
  168.  
  169.     if (swV) {
  170.         cout << endl << "- Starting Backup Routine" << endl << "\t-- Check SOURCE and DESTINATION" << endl << endl;
  171.     }
  172.  
  173.     if (swV) {
  174.         cout << "- changing into SOURCE directory" << endl;
  175.     }
  176.  
  177.     chdir(SOURCE);
  178.  
  179.     if (swV) {
  180.         cout << "- reading context of SOURCE directory" << endl << endl;
  181.     }
  182.  
  183.     // over here we go read the SOURCE directory
  184.         // #include <dirent.h>
  185.  
  186.     string path = SOURCE; //Put a valid path here for folder
  187.  
  188.     DIR *directory = opendir(path.c_str());
  189.     struct dirent *direntStruct;
  190.  
  191.     if (directory != NULL) {
  192.  
  193.         // generate system command in COMMAND[MAX]
  194.        
  195.         while (direntStruct = readdir(directory)) {
  196.  
  197.             if (strcmp(direntStruct->d_name,".") != 0 && strcmp(direntStruct->d_name,"..") != 0) {
  198.  
  199.                 if (!swQ) {
  200.                     cout << "- generate compression on files..." << endl;
  201.                     cout << "\t-- " << direntStruct->d_name << endl;
  202.                 }
  203.  
  204.                 strncpy(COMMAND, tar_command, (MAX - strlen(tar_command)));
  205.                 strncat(COMMAND, DESTINATION, (MAX - strlen(DESTINATION)));
  206.                 strncat(COMMAND, "/", 1);
  207.                 strncat(COMMAND, direntStruct->d_name, (MAX - strlen(direntStruct->d_name)));
  208.                 strncat(COMMAND, tar_extension, strlen(tar_extension));
  209.                 strncat(COMMAND, "' '", 3);
  210.                 //strncat(COMMAND, SOURCE, (MAX - strlen(SOURCE)));
  211.                 //strncat(COMMAND, "/", 1);
  212.                 strncat(COMMAND, direntStruct->d_name, strlen(direntStruct->d_name));
  213.                 strncat(COMMAND, "'", 1);
  214.  
  215.                 if (swV) {
  216.                     cout << "COMMAND:" << endl << "- " << COMMAND << endl << endl;
  217.                 }              
  218.  
  219.                 // use COMMAND and discard it
  220.                 if (!swT) {            
  221.                     system(COMMAND);
  222.                 }
  223.                 else { cout << "-- performing dry run" << endl; }
  224.  
  225.                 // cleanup
  226.                 memset(COMMAND,'\0', MAX);
  227.             }
  228.         }
  229.  
  230.         closedir(directory);
  231.     }
  232.    
  233.     if (!swQ) {
  234.         cout << endl << "---------------------" << endl << "- BACKUP IS COMPLETED" << endl;
  235.     }
  236.  
  237.     cout << endl;
  238.  
  239.     ch=0;
  240.  
  241. return 0;
  242. }
  243.  
  244.  
  245. // eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement