⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 init.c

📁 Linux系统备份源代码 可基于用户自定义策略实现系统、应用数据备份
💻 C
📖 第 1 页 / 共 5 页
字号:
/***************************************************************************    file                 : init.c    begin                : Sun Jan 30 2000    copyright            : (C) 2000 by Henrik Witt-Hansen    email                : bean@daisy.net ***************************************************************************//*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * ***************************************************************************//*base configuration*/#ifdef HAVE_CONFIG_H#include <config.h>#endif/*standard includes*/#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <errno.h>#include <sys/stat.h>#include <pwd.h>#include <dirent.h>#include <time.h>/*local headers*/#include "sitback.h" /*local prototypes*/int  __parse_args(int argc,char **argv);int  __parse_timestring(char *timestring);int  __parse_script();int  __add_file(char *file);void __free_filelist();int  __add_time(int dayofweek,int hour,int min);void __free_timelist();int  __test_software();int  __test_config_sanity();char *__get_local_user();void __dump_config();int __get_homedir();struct passwd *__getpwuid(uid_t uid); /* please read the function description */int __set_lock();void __clear_lock();/*Configuration*/char          conf__script_name[51]="";     /*max 50 characters*/char          conf__script_title[129]="";   /*max 128 characters*/int           conf__mode=SINGLE;            /*default dont be a daemon*/int           conf__operation=BACKUP;       /*default is BACKUP operation*/char          conf__allow_multiple=0;       /*Multiple volumes (the -M tar switch)*/int           conf__volume_length=0;        /*volume length (the -L tar switch)*/char          conf__org_archive[257]="";    /* Original target device or filename*/char          conf__archive[257]="";        /*target device or filename*/int           conf__compress=0;             /*Use compression (gzip)*/Tfilelist     conf__filelist=NULL;          /*pointer to somewhere in the filelist*/int           conf__verify=1;               /*Verify archive*/char          conf__report_printer[257]="";char          conf__smb_user[21]="";char          conf__smb_passwd[21]="";long unsigned conf__archive_size=0;         /*aprox. archive-size, uncompressed*/char          conf__archive_name[257]="";char          conf__report_name[257]="";int           conf__ignore_time_info=0;char          conf__archive_id[257]="";char          conf__archive_oldid[257]="";Ttimelist     conf__timelist=NULL;int           conf__update_archiveinfo=0;int           conf__skip_old_archiveinfo=0;char          conf__tar_action='c';int           conf__verbose=0;int           conf__mt_device=0;int           conf__non_rewind=0;int           conf__retry=1;  /*default is no retry*/char          conf__tar[30]="";char          conf__gzip[30]="";char          conf__zip[30]="";char          conf__bzip2[30]="";char          conf__Zcompress[30]="";char          conf__mt[60]="";char         *conf__compressapp=NULL;int           conf__compressor=0;char          conf__start_mark[25]="";int           conf__special_files=0;char          conf__ps[30]="";int           conf__curses=1;char          conf__before[1024]="";char          conf__after[1024]="";char         *conf__exclude_files=NULL;char          conf__rsh[256]="rsh";char          conf__rhost[257]="";char          conf__ruser[257]="";int           conf__nice=0;char         *conf__homedir=NULL;int           conf__force_mt=0;int           conf__force_not_mt=0;int           conf__force_rewind=0;int           conf__force_non_rewind=0;int           conf__follow_symlinks=0;int           conf__keep_lock=0;char          conf__report_mail[256]="";char          conf__smtp_host[256]="";char          conf__smtp_user[350]="";char          conf__smtp_pass[350]="";int           conf__type=TAR;int           conf__ignore_bad_symlinks=0;char          conf__mkisofs[50]="";char          conf__cdrecord[50]="";char          conf__speed[256]="";int           conf__fixate=0;char          conf__buffersize[256]="";int           conf__eject=0;int           conf__cd_on_the_fly=0;char          conf__temp[257]="";time_t        conf__time_started;unsigned      conf__archive_count=0;int           conf__unattended=0;char          conf__scriptdir[257]="";int           conf__skip_lock=1;int           conf__add_to_root=0;int           conf__no_info=0;int           conf__bad_symlinks=0;extern int errno;/*Magnetic tape devices, these must all be erased and retensioned  before starting the backup*/char mt_devices[]="/dev/ftape /dev/rawft0 /dev/nrawft0 /dev/rft0 /dev/nrft0 /dev/qft0 /dev/nqft0 \                   /dev/rawft1 /dev/nrawft1 /dev/rft1 /dev/nrft1 /dev/qft1 /dev/nqft1 \                   /dev/rawft2 /dev/nrawft2 /dev/rft2 /dev/nrft2 /dev/qft2 /dev/nqft2 \                   /dev/rawft3 /dev/nrawft3 /dev/rft3 /dev/nrft3 /dev/qft3 /dev/nqft3 \                   /dev/ntpqic11 /dev/ntpqic120 /dev/ntpqic150 /dev/ntpqic24 \                   /dev/nzqft0 /dev/nzqft1 /dev/nzqft2 /dev/nzqft3";/*Non rewinding devices, these must all be rewinded after use*/char non_rewinding[]="/dev/nrawft0 /dev/nrft0 /dev/nqft0 \                      /dev/nst0 /dev/nst1 /dev/nst2 /dev/nst3 /dev/nst4 /dev/nst5 /dev/nst6 /dev/nst7 \                      /dev/nrt0 /dev/nrt1 /dev/nrt2 /dev/nrt3 /dev/nrt4 /dev/nrt5 /dev/nrt6 /dev/nrt7 \                      /dev/ntpqic11 /dev/ntpqic120 /dev/ntpqic150 /dev/ntpqic24 \                      /dev/nzqft0 /dev/nzqft1 /dev/nzqft2 /dev/nzqft3";/********************************************************************  int init(int argc,char **argv)  Initialize application. Read command-line  and parse backup/restore script  arguments:    Standard main() arguments  return:    0 on success    -1 on error********************************************************************/int Init(int argc, char *argv[]){  int rc;  /*  Get our home directory (where we put temp. files) */  if(__get_homedir()==-1)  {    printf("Invalid home directory..\n");    errcode=42;    return -1;  }  /*initialize log-file.*/  log("Sitback %s startup",VERSION);  /*Parse the input arguments*/  if((rc=__parse_args(argc,argv))==-1)    return errcode;  /*we now know if we are supposed to be a daemon or not.    If not, start the best available interface*/  if(conf__mode!=DAEMON)  {    /* do we want the nice ncurses interface, or only raw text ?? */    if(conf__curses)    {      if(UI__Init(argc,argv)==-1)        printf("Failed to start ncurses interface. Fallback to text mode\n");    }    if(conf__operation==BACKUP)    {      UI__ShowBackup();      UI__Message("Initializing\n");    }  }  /*Next, read the backup/restore script*/  if(__parse_script())  {    UI__OkDlg("\nUnable to continue..");    return -1;  }  /*Make sure we have the software that is needed*/  debug("Testing software\n");  if(__test_software()==-1)  {    /*errcode and error message has been handled by the function*/    UI__OkDlg("\nUnable to continue..");    return -1;  }  /*make sanity check's and set implied flags.*/  debug("Testing configuration sanity\n");  if(__test_config_sanity()==-1)  {    /*errcode and error message has been handled by the function*/    UI__OkDlg("\nUnable to continue");    return -1;  }  /* dump configuration if debugging is on */  if(enable_dbg)    __dump_config();  /* adjust nice level */  nice(conf__nice);  /* check for a lock file */  if(__set_lock())  {    debug("Lockfile exists...\n");    log("~/.sitback/!lock exists\n");    UI__OkDlg("\nFound an existing lockfile ~/.sitback/.lock\nIf you do not have another sitback running,\nremove the lockfile manually, then restart sitback\n");    errcode=88;    return -1;  }  /*No problems*/  return 0;}/********************************************************************  int __parse_args(int argc,char **argv)  Parse command line arguments  arguments:    Standard main() arguments  return:    number of arguments handled    -1 on error    number of arguments - 1 on success (first argument is      the commandline and always present)********************************************************************/int __parse_args(int argc,char **argv){  int __argc=0;  /*incremented for each arguments handled*/  char prev_switch=0;  char *suffix;  long int val;  unsigned int count;  char *p;  for(__argc=1;__argc<argc;__argc++)  {    debug("checking argument %u\n",__argc);    /*either a command-switch or command-data*/    if(argv[__argc][0]=='-' && (prev_switch==0 || prev_switch=='m' || prev_switch=='c'))    {      /*reset if used*/      prev_switch=0;      debug("argument %u is switch '%c'\n",__argc,argv[__argc][1]);      /*what action ??*/      switch(argv[__argc][1])      {        case '\0': /*ignore*/ break;        case '-':        {          if(!strcasecmp(argv[__argc],"--scriptdir"))          {            /* Next option must be the script directory */            prev_switch='s'; /* Let s symbolize this option */            debug("Expecting next argument to be scriptdir\n");            break;          }          if(!strcasecmp(argv[__argc],"--version"))          {            /* remove the lockfile */            __clear_lock();            exit(0);          }          else if(!strcasecmp(argv[__argc],"--help"))          {            /*special handling for this switch*/            printf("\n\n                   Sitback - The SImple Tar BACKup utility");            printf("\n                                 ^^     ^   ^^^^\n\n");            printf("Backup operation:     sitback [options] scriptname\n\n");            printf("   -a  <archivename|devicename>  :  Use this archive or device instead\n");            printf("                                    of ARCHIVE from the script\n");            printf("   -b                            :  Include special files in archive\n");            printf("   -c [bzip2|gzip|compress|zip]  :  Compress archive.\n");            printf("                                    If you want to control wich compression\n");            printf("                                    tool is used, give the tool-name here.\n");            printf("   -d                            :  Run as daemon instead of just a single run\n");            printf("   -e                            :  Do not verify the archive after backup.\n");            printf("   -h                            :  Which 'rsh' to use for accessing remote\n");            printf("                                    devices. Usually this is either 'rsh' or\n");            printf("                                    'ssh' but other remote shells can be used\n");            printf("                                    as long as they work the same way as rsh\n");            printf("   -i archive-id                 :  Set the archive id to this string. (no\n");            printf("                                    blank spaces is allowed). The archive-id is\n");            printf("                                    updated or added, if no current archive\n");            printf("                                    exists, f.ex. on a tape, a archive is\n");            printf("                                    created. In any case, operation is stopped\n");            printf("                                    after updating the archive id so that the.\n");            printf("                                    archive is empty apart from the info\n");            printf("   -j <script or command>        :  Script or command to run before backup\n");            printf("   -k <script or command>        :  Script or command to run after backup\n");            printf("   -m [count][K|M]               :  Create multiple volumes.\n");            printf("                                    If a byte-count is given, volumes will be\n");            printf("                                    no larger than #count# bytes.\n");            printf("                                    'K' or 'M' may be appended to indicate that\n");            printf("                                    count is in KB or MB. When no modifier is\n");            printf("                                    appended, the count should be an equal\n");            printf("                                    number of 1024B blocks.\n");            printf("                                    If no length is given, multiple volumes is\n");            printf("                                    only created if the first device has no\n");            printf("                                    more space on it.. The user will then be\n");            printf("                                    prompted for a new tape/disk. Multiple\n");            printf("                                    volumes is not allowed in daemon mode.\n");            printf("                                    Verification of backup is not possible if\n");            printf("                                    compression is used. The -e switch is \n");            printf("                                    silently appended.\n");            printf("   -n                            :  Ignore the time-string, run now.\n");            printf("   -o                            :  Follow symlinks. (include the content they\n");            printf("                                    link to instead of the symlink itself)\n");            printf("   -p                            :  Increase 'nice' level (decrease priority)\n");            printf("                                    by this amount, allowed range for regular\n");            printf("                                    users: 0 to 19. if running as 'root', the\n");            printf("                                    allowed range is -20 to 19. Errors is\n");            printf("                                    silently corrected\n");            printf("   -q                            :  Copy files instead of creating an archive.\n");            printf("   -s                            :  Do not try to resolve archive-info before\n");            printf("                                    backup or update of archive-info..  This\n");            printf("                                    may result in database inconsistency\n");            printf("                                    such as incorrect content-list's or\n");            printf("                                    archive-id's no longer existing. Use only\n");            printf("                                    if you want to set archive-info such as a\n");            printf("                                    new id on a blank tape.\n\n");            printf("   -t timestring                 :  Use this single time-string instead of TIME\n");            printf("                                    from the script. The time-string has the\n");            printf("                                    format...  'day of week number;hour;minute'\n");            printf("                                    Day of week number is 0(sun) to 6(sat).\n");            printf("                                    Ex. \"1,3,5;02;30\",  mon, wed, fri, at 02.30.\n");            printf("   -u                            :  Update archive files that have changed, do\n");            printf("                                    not recreate the archive from scratch.\n");            printf("   -x                            :  A list of files or directorys to exclude\n");            printf("                                    from the backup. File-/directorynames must\n");            printf("                                    be separated by commas and no blankspaces.\n");            printf("   -y [number]                   :  Retry operation if there is an error.\n");            printf("                                    This might be a good idea if you are using\n");            printf("                                    an older buggy or dirty drive.\n");            printf("                                    Default is no retry (0), but retry may be\n");            printf("                                    set to a number in the range 0;9\n");            printf("   -v                            :  Archive to CDR device\n");            printf("   -w                            :  Archive to CDRW device (blank before write)\n");            printf("   -z                            :  Archive to iso file\n");            printf("   --norewind                    :  Archive|Device is non-rewinding\n");            printf("   --rewind                      :  Archive|Device is rewinding\n");            printf("   --nomt                        :  Archive|Device is not a magnetic tape\n");            printf("   --mt                          :  Archive|Device is a magnetic tape\n");            printf("\n\n\n");            printf("Restore operation:  sitback -r <scriptname>\n\n");            printf("   Restore has not yet been implemented. But the archives is regular tar\n");            printf("   archives, so you may use 'tar -x -P -f archivename|devicename to restore\n");            printf("   the content of the archive.\n");            printf("\n\n\n");            printf("List operation:     sitback -l [scriptname]\n\n");            printf("   List operation has not yet been implemented.\n");            printf("\n\n\n");            printf("Other options:      sitback option [data]\n\n");            printf("   -f                            :  Disable curses based interface..\n");            printf("   -g                            :  Enable debug messages ('%s')\n",DEBUG_DESTINATION);            printf("  --help                         :  Show this help screen. (You may want\n");            printf("                                    to do..  'sitback -h | more')\n");            printf("  --scriptdir <dir>              :  Look for the script in this directory\n");            printf("  --lock                         :  Prevent parallel sitback instances\n");            printf("  --version                      :  Show version.\n");            printf("  --verbose                      :  Make any operation more verbose\n\n");            /* remove the lockfile */            __clear_lock();            exit(0);          }          else if(!strcmp(&argv[__argc][2],"nonrewind"))          {            if(conf__force_rewind)            {              printf("Already forcing device to be rewinding..\n");              errcode=87;              return -1;            }            conf__force_non_rewind=1;          }          else if(!strcmp(&argv[__argc][2],"rewind"))          {            if(conf__force_non_rewind)            {              printf("Already forcing device to be non-rewinding..\n");              errcode=87;              return -1;            }            conf__force_rewind=1;          }          else if(!strcmp(&argv[__argc][2],"nomt"))          {            if(conf__force_mt)            {              printf("Already forcing device to be an mt-device..\n");              errcode=87;              return -1;            }            conf__force_not_mt=1;          }          else if(!strcmp(&argv[__argc][2],"mt"))          {            if(conf__force_not_mt)            {              printf("Already forcing device not to be an mt-device..\n");              errcode=87;              return -1;            }            conf__force_mt=1;          }          else if(!strcmp(&argv[__argc][2],"verbose"))          {            conf__verbose=1;          }          else if(!strcmp(&argv[__argc][2],"lock"))          {            conf__skip_lock=0;          }          else          {            errcode=1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -