Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 29.81 KB | None | 0 0
  1. #!/bin/bash
  2. # info: backup system user with all its objects
  3. # options: USER NOTIFY
  4. #
  5. # The call is used for backing up user with all its domains and databases.
  6.  
  7. if ! [ -x "$(command -v pigz)" ]; then
  8.   GZIP="$(which pigz) -9 -p 16"
  9.   else
  10.   GZIP="$(which gzip)"
  11. fi
  12.  
  13. #----------------------------------------------------------#
  14. #                    Variable&Function                     #
  15. #----------------------------------------------------------#
  16.  
  17. # Importing system variables
  18. source /etc/profile
  19.  
  20. # Argument definition
  21. user=$1
  22. notify=${2-no}
  23.  
  24. # Includes
  25. source $VESTA/func/main.sh
  26. source $VESTA/func/domain.sh
  27. source $VESTA/func/db.sh
  28. source $VESTA/conf/vesta.conf
  29.  
  30.  
  31. #----------------------------------------------------------#
  32. #                    Verifications                         #
  33. #----------------------------------------------------------#
  34.  
  35. check_args '1' "$#" 'USER [NOTIFY]'
  36. is_format_valid 'user'
  37. is_system_enabled "$BACKUP_SYSTEM" 'BACKUP_SYSTEM'
  38. is_object_valid 'user' 'USER' "$user"
  39. is_object_unsuspended 'user' 'USER' "$user"
  40. is_backup_enabled
  41.  
  42.  
  43. #----------------------------------------------------------#
  44. #                       Action                             #
  45. #----------------------------------------------------------#
  46.  
  47. # Set backup directory if undefined
  48. if [ -z "$BACKUP" ]; then
  49.     BACKUP=/backup
  50. fi
  51. mkdir -p $BACKUP
  52.  
  53. # Get current time
  54. start_time=$(date '+%s')
  55.  
  56. # Set notification email and subject
  57. subj="$user → backup failed"
  58. email=$(grep CONTACT $VESTA/data/users/admin/user.conf |cut -f 2 -d \')
  59.  
  60. # Checking load average
  61. la=$(cat /proc/loadavg |cut -f 1 -d ' ' |cut -f 1 -d '.')
  62. i=0
  63. while [ "$la" -ge "$BACKUP_LA_LIMIT" ]; do
  64.     echo -e "$(date "+%F %T") Load Average $la"
  65.     sleep 60
  66.     if [ "$i" -ge "15" ]; then
  67.         la_error="LoadAverage $la is above threshold"
  68.         echo "$la_error" |$SENDMAIL -s "$subj" $email $notify
  69.         sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  70.         check_result $E_LA "$la_error"
  71.     fi
  72.     la=$(cat /proc/loadavg |cut -f 1 -d ' ' |cut -f 1 -d '.')
  73.     (( ++i))
  74. done
  75.  
  76. # Creating temporary directory
  77. tmpdir=$(mktemp -p $BACKUP -d)
  78.  
  79. if [ "$?" -ne 0 ]; then
  80.     echo "Can't create tmp dir $tmpdir" |$SENDMAIL -s "$subj" $email $notify
  81.     check_result $E_NOTEXIST "can't create tmp dir"
  82. fi
  83.  
  84. # Backup sys configs
  85. echo "-- SYSTEM --" |tee $BACKUP/$user.log
  86. mkdir $tmpdir/vesta
  87.  
  88. echo -e "$(date "+%F %T") $user.conf" |tee -a $BACKUP/$user.log
  89. cp -r $USER_DATA/user.conf $tmpdir/vesta/
  90. cp -r $USER_DATA/ssl $tmpdir/vesta/
  91.  
  92. if [ -e "$USER_DATA/stats.log" ]; then
  93.     echo -e "$(date "+%F %T") stats.log" |tee -a $BACKUP/$user.log
  94.     cp -r $USER_DATA/stats.log $tmpdir/vesta/
  95. fi
  96.  
  97. if [ -e "$USER_DATA/history.log" ]; then
  98.     echo -e "$(date "+%F %T") history.log" |tee -a $BACKUP/$user.log
  99.     cp -r $USER_DATA/history.log $tmpdir/vesta/
  100. fi
  101.  
  102. if [ -e "$USER_DATA/backup-excludes.conf" ]; then
  103.     echo -e "$(date "+%F %T") backup-excludes.conf" |tee -a $BACKUP/$user.log
  104.     cp -r $USER_DATA/backup-excludes.conf $tmpdir/vesta/
  105. fi
  106.  
  107. # Backup PAM
  108. mkdir $tmpdir/pam
  109. echo -e "$(date "+%F %T") pam" |tee -a $BACKUP/$user.log
  110. grep "^$user:" /etc/passwd > $tmpdir/pam/passwd
  111. grep "^$user:" /etc/shadow > $tmpdir/pam/shadow
  112. grep "^$user:" /etc/group > $tmpdir/pam/group
  113. echo
  114.  
  115. # Parsing excludes
  116. if [ -e "$USER_DATA/backup-excludes.conf" ]; then
  117.     source $USER_DATA/backup-excludes.conf
  118. fi
  119.  
  120. # WEB domains
  121. if [ ! -z "$WEB_SYSTEM" ] && [ "$WEB" != '*' ]; then
  122.     echo -e "\n-- WEB --" |tee -a $BACKUP/$user.log
  123.     mkdir $tmpdir/web/
  124.  
  125.     # Parsing domain exclusions
  126.     conf="$USER_DATA/web.conf"
  127.     for domain in $(search_objects 'web' 'SUSPENDED' "*" 'DOMAIN'); do
  128.         exclusion=$(echo -e "$WEB" |tr ',' '\n' |grep "^$domain$")
  129.         if [ -z "$exclusion" ]; then
  130.             web_list="$web_list $domain"
  131.         else
  132.             echo "$(date "+%F %T") excluding $domain"|tee -a $BACKUP/$user.log
  133.         fi
  134.     done
  135.     web_list=$(echo "$web_list" |sed -e "s/  */\ /g" -e "s/^ //")
  136.  
  137.     i=0
  138.  
  139.     for domain in $web_list; do
  140.         ((i ++))
  141.         echo -e "$(date "+%F %T") $domain" |tee -a $BACKUP/$user.log
  142.         mkdir -p $tmpdir/web/$domain/conf
  143.         mkdir -p $tmpdir/web/$domain/vesta
  144.  
  145.         # Get domain variables
  146.         domain_idn=$domain
  147.         format_domain_idn
  148.         get_domain_values 'web'
  149.  
  150.         # Backup web.conf
  151.         cd $tmpdir/web/$domain/
  152.         conf="$USER_DATA/web.conf"
  153.         grep "DOMAIN='$domain'" $conf > vesta/web.conf
  154.  
  155.         # Backup vhost config
  156.         conf=$HOMEDIR/$user/conf/web/$domain.$WEB_SYSTEM.conf
  157.         if [ -e "$conf" ]; then
  158.             cp $conf conf/$WEB_SYSTEM.conf
  159.         else
  160.             # old style configs
  161.             tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.tpl"
  162.             conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
  163.             get_web_config_lines $tpl_file $conf
  164.             sed -n "$top_line,$bottom_line p" $conf > conf/$WEB_SYSTEM.conf
  165.         fi
  166.  
  167.         # Backup ssl vhost
  168.         if [ "$SSL" = 'yes' ]; then
  169.             conf=$HOMEDIR/$user/conf/web/$domain.$WEB_SYSTEM.ssl.conf
  170.             if [ -e "$conf" ]; then
  171.                 cp $conf conf/$WEB_SYSTEM.ssl.conf
  172.             else
  173.                 tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.stpl"
  174.                 conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
  175.                 get_web_config_lines $tpl_file $conf
  176.                 sed -n "$top_line,$bottom_line p" $conf > \
  177.                     conf/s$WEB_SYSTEM.conf
  178.             fi
  179.         fi
  180.  
  181.         # Backup proxy config
  182.         if [ ! -z "$PROXY_SYSTEM" ] && [ ! -z "$PROXY" ]; then
  183.             conf=$HOMEDIR/$user/conf/web/$domain.$PROXY_SYSTEM.conf
  184.             if [ -e "$conf" ]; then
  185.                 cp $conf conf/$PROXY_SYSTEM.conf
  186.             else
  187.                 tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.tpl"
  188.                 conf="$HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf"
  189.                 get_web_config_lines $tpl_file $conf
  190.                 sed -n "$top_line,$bottom_line p" $conf > \
  191.                     conf/$PROXY_SYSTEM.conf
  192.             fi
  193.         fi
  194.  
  195.         # Backup ssl proxy config
  196.         if [ ! -z "$PROXY_SYSTEM" ] && [ "$SSL" = 'yes' ]; then
  197.             conf=$HOMEDIR/$user/conf/web/$domain.$PROXY_SYSTEM.ssl.conf
  198.             if [ -e "$conf" ]; then
  199.                 cp $conf conf/$PROXY_SYSTEM.ssl.conf
  200.             else
  201.                 tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.stpl"
  202.                 conf="$HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf"
  203.                 get_web_config_lines $tpl_file $conf
  204.                 sed -n "$top_line,$bottom_line p" $conf >\
  205.                     conf/s$PROXY_SYSTEM.conf
  206.             fi
  207.         fi
  208.  
  209.         # Backup custom config / backup LE config
  210.         for sconfig in $(ls $HOMEDIR/$user/conf/web/|grep ".$domain.conf"); do
  211.             cp $HOMEDIR/$user/conf/web/$sconfig conf/
  212.         done
  213.  
  214.         # Backup ssl certificates
  215.         if [ "$SSL" = 'yes' ] ; then
  216.             cp $HOMEDIR/$user/conf/web/ssl.$domain.* conf/
  217.             cp $USER_DATA/ssl/$domain.* vesta/
  218.         fi
  219.  
  220.         # Define exclude arguments
  221.         exlusion=$(echo -e "$WEB" |tr ',' '\n' |grep "^$domain:")
  222.         set -f
  223.         fargs=()
  224.         fargs+=(--exclude='logs/*')
  225.         if [ ! -z "$exlusion" ]; then
  226.             xdirs="$(echo -e "$exlusion" |tr ':' '\n' |grep -v $domain)"
  227.             for xpath in $xdirs; do
  228.                 fargs+=(--exclude=$xpath/*)
  229.                 echo "$(date "+%F %T") excluding directory $xpath"
  230.                 msg="$msg\n$(date "+%F %T") excluding directory $xpath"
  231.             done
  232.         fi
  233.         set +f
  234.  
  235.         # Backup files
  236.         cd $HOMEDIR/$user/web/$domain
  237.         tar -cpf $tmpdir/web/$domain/domain_data.tar * ${fargs[@]}
  238.  
  239.         # Compress archive
  240.         $GZIP -$BACKUP_GZIP $tmpdir/web/$domain/domain_data.tar
  241.     done
  242.  
  243.     # Print total
  244.     if [ "$i" -eq 1 ]; then
  245.         echo -e "$(date "+%F %T") *** $i domain ***" |tee -a $BACKUP/$user.log
  246.     else
  247.         echo -e "$(date "+%F %T") *** $i domains ***"|tee -a $BACKUP/$user.log
  248.     fi
  249. fi
  250.  
  251. # DNS domains
  252. if [ ! -z "$DNS_SYSTEM" ] && [ "$DNS" != '*' ]; then
  253.     echo -e "\n-- DNS --" |tee -a $BACKUP/$user.log
  254.     mkdir $tmpdir/dns/
  255.  
  256.     # Parsing domain exclusions
  257.     for domain in $(search_objects 'dns' 'SUSPENDED' "*" 'DOMAIN'); do
  258.         exclusion=$(echo "$DNS" |tr ',' '\n' |grep "^$domain$")
  259.         if [ -z "$exclusion" ]; then
  260.             dns_list="$dns_list $domain"
  261.         else
  262.             echo "$(date "+%F %T") excluding $domain"
  263.             msg="$msg\n$(date "+%F %T") excluding $domain"
  264.         fi
  265.     done
  266.     dns_list=$(echo "$dns_list" |sed -e "s/  */\ /g" -e "s/^ //")
  267.  
  268.     i=0
  269.     for domain in $dns_list; do
  270.         ((i ++))
  271.         echo -e "$(date "+%F %T") $domain" |tee -a $BACKUP/$user.log
  272.  
  273.         # Building directory tree
  274.         mkdir -p $tmpdir/dns/$domain/conf
  275.         mkdir -p $tmpdir/dns/$domain/vesta
  276.  
  277.         # Backup dns.conf
  278.         cd $tmpdir/dns/$domain/
  279.         conf="$USER_DATA/dns.conf"
  280.         grep "DOMAIN='$domain'" $conf > vesta/dns.conf
  281.  
  282.         # Backup dns recods
  283.         cp $USER_DATA/dns/$domain.conf vesta/$domain.conf
  284.         if [ "$DNS_SYSTEM" != 'remote' ]; then
  285.             cp $HOMEDIR/$user/conf/dns/$domain.db conf/$domain.db
  286.         fi
  287.     done
  288.  
  289.     # Print total
  290.     if [ "$i" -eq 1 ]; then
  291.         echo -e "$(date "+%F %T") *** $i domain ***" |tee -a $BACKUP/$user.log
  292.     else
  293.         echo -e "$(date "+%F %T") *** $i domains ***"|tee -a $BACKUP/$user.log
  294.     fi
  295. fi
  296.  
  297. # Mail domains
  298. if [ ! -z "$MAIL_SYSTEM" ] && [ "$MAIL" != '*' ]; then
  299.     echo -e "\n-- MAIL --" |tee -a $BACKUP/$user.log
  300.     mkdir $tmpdir/mail/
  301.  
  302.     # Parsing domain exclusions
  303.     conf="$USER_DATA/mail.conf"
  304.     for domain in $(search_objects 'mail' 'SUSPENDED' "*" 'DOMAIN'); do
  305.         check_exl=$(echo "$MAIL" |tr ',' '\n' |grep "^$domain$")
  306.         if [ -z "$check_exl" ]; then
  307.             mail_list="$mail_list $domain"
  308.         else
  309.             echo "$(date "+%F %T") excluding $domain"|tee -a $BACKUP/$user.log
  310.         fi
  311.     done
  312.     mail_list=$(echo "$mail_list" |sed -e "s/  */\ /g" -e "s/^ //")
  313.  
  314.     i=0
  315.     for domain in $mail_list; do
  316.         ((i ++))
  317.         echo -e "$(date "+%F %T") $domain" |tee -a $BACKUP/$user.log
  318.         mkdir -p $tmpdir/mail/$domain/conf
  319.         mkdir -p $tmpdir/mail/$domain/vesta
  320.         domain_idn=$domain
  321.         format_domain_idn
  322.  
  323.         # Backup exim config
  324.         if [[ "$MAIL_SYSTEM" =~ exim ]]; then
  325.             cd $tmpdir/mail/$domain/
  326.             cp $HOMEDIR/$user/conf/mail/$domain/* conf/
  327.         fi
  328.  
  329.         # Backup mail.conf
  330.         conf="$USER_DATA/mail.conf"
  331.         grep "DOMAIN='$domain'" $conf > vesta/mail.conf
  332.         cp $USER_DATA/mail/$domain.* vesta/
  333.         if [ ! -z "$(ls $USER_DATA/mail/|grep *@$domain)" ]; then
  334.             cp $USER_DATA/mail/*@$domain.* vesta/
  335.         fi
  336.  
  337.         # Backup emails
  338.         cd $HOMEDIR/$user/mail/$domain_idn
  339.         for account in $(ls); do
  340.             exclusion=$(echo "$MAIL" |tr ',' '\n' |grep "$domain:")
  341.             exclusion=$(echo "$exclusion" |tr ':' '\n' |grep "^$account$")
  342.  
  343.             # Checking exlusions
  344.             if [ -z "$exclusion" ] && [[ "$MAIL_SYSTEM" =~ exim ]]; then
  345.                 echo "$(date "+%F %T") + $account@$domain" |\
  346.                     tee -a $BACKUP/$user.log
  347.                 touch $tmpdir/mail/$domain/accounts.tar
  348.                 tar -rpf $tmpdir/mail/$domain/accounts.tar $account
  349.             else
  350.                 echo "$(date "+%F %T") excluding mail account $account" |\
  351.                     tee -a $BACKUP/$user.log
  352.             fi
  353.         done
  354.  
  355.         # Compress archive
  356.         if [ -e "$tmpdir/mail/$domain/accounts.tar" ]; then
  357.             $GZIP -$BACKUP_GZIP $tmpdir/mail/$domain/accounts.tar
  358.         fi
  359.     done
  360.  
  361.     # Print total
  362.     if [ "$i" -eq 1 ]; then
  363.         echo -e "$(date "+%F %T") *** $i domain ***" |tee -a $BACKUP/$user.log
  364.     else
  365.         echo -e "$(date "+%F %T") *** $i domains ***"|tee -a $BACKUP/$user.log
  366.     fi
  367. fi
  368.  
  369.  
  370. # Databases
  371. if [ ! -z "$DB_SYSTEM" ] && [ "$DB" != '*' ]; then
  372.     echo -e "\n-- DB --" |tee -a $BACKUP/$user.log
  373.     mkdir $tmpdir/db/
  374.  
  375.     # Parsing database exclusions
  376.     for database in $(search_objects 'db' 'SUSPENDED' "*" 'DB'); do
  377.         exclusion=$(echo "$DB" |tr ',' '\n' |grep "^$database$")
  378.         if [ -z "$exclusion" ]; then
  379.             db_list="$db_list $database"
  380.         else
  381.             echo "$(date "+%F %T") excluding $database" |\
  382.                 tee -a $BACKUP/$user.log
  383.         fi
  384.     done
  385.  
  386.     i=0
  387.     conf="$USER_DATA/db.conf"
  388.     db_list=$(echo "$db_list" |sed -e "s/  */\ /g" -e "s/^ //")
  389.     for database in $db_list; do
  390.         ((i ++))
  391.         get_database_values
  392.  
  393.         echo -e "$(date "+%F %T") $database ($TYPE)" |tee -a $BACKUP/$user.log
  394.         mkdir -p $tmpdir/db/$database/conf
  395.         mkdir -p $tmpdir/db/$database/vesta
  396.  
  397.         cd $tmpdir/db/$database/
  398.         grep "DB='$database'" $conf > vesta/db.conf
  399.  
  400.         dump="$tmpdir/db/$database/$database.$TYPE.sql"
  401.         grants="$tmpdir/db/$database/conf/$database.$TYPE.$DBUSER"
  402.         case $TYPE in
  403.             mysql) dump_mysql_database ;;
  404.             pgsql) dump_pgsql_database ;;
  405.         esac
  406.  
  407.         # Compress dump
  408.         $GZIP -$BACKUP_GZIP $dump
  409.     done
  410.  
  411.     # Print total
  412.     if [ "$i" -eq 1 ]; then
  413.         echo -e "$(date "+%F %T") *** $i database ***" |\
  414.             tee -a $BACKUP/$user.log
  415.     else
  416.         echo -e "$(date "+%F %T") *** $i databases ***"|\
  417.             tee -a $BACKUP/$user.log
  418.     fi
  419. fi
  420.  
  421. # Cron jobs
  422. if [ ! -z "$CRON_SYSTEM" ] && [ "$CRON" != '*' ]; then
  423.     echo -e "\n-- CRON --" |tee -a $BACKUP/$user.log
  424.     mkdir $tmpdir/cron/
  425.  
  426.     # Backup cron.conf
  427.     cp $USER_DATA/cron.conf $tmpdir/cron/
  428.     cron_record=$(wc -l $USER_DATA/cron.conf|cut -f 1 -d ' ')
  429.  
  430.     if [ -e "/var/spool/cron/$user" ]; then
  431.         cron_list="$cron_record"
  432.         cp /var/spool/cron/$user $tmpdir/cron/
  433.     fi
  434.  
  435.     # Print total
  436.     if [ "$cron_record" -eq 1 ]; then
  437.         echo -e "$(date "+%F %T") *** $cron_record job ***" |\
  438.             tee -a $BACKUP/$user.log
  439.     else
  440.         echo -e "$(date "+%F %T") *** $cron_record jobs ***" |\
  441.             tee -a $BACKUP/$user.log
  442.     fi
  443. fi
  444.  
  445. # User Directories
  446. if [ "$USER" != '*' ]; then
  447.     echo -e "\n-- User Dir --" |tee -a $BACKUP/$user.log
  448.     mkdir $tmpdir/user_dir
  449.     cd $HOMEDIR/$user
  450.  
  451.     # Parsing directory exlusions
  452.     USER=''
  453.     if [ -e "$USER_DATA/backup-excludes.conf" ]; then
  454.         source $USER_DATA/backup-excludes.conf
  455.     fi
  456.     fargs=()
  457.     for xpath in $(echo "$USER" |tr ',' '\n'); do
  458.         fargs+=(-not)
  459.         fargs+=(-path)
  460.         fargs+=("./$xpath*")
  461.         echo "$(date "+%F %T") excluding directory $xpath" |\
  462.             tee -a $BACKUP/$user.log
  463.     done
  464.  
  465.     IFS=$'\n'
  466.     set -f
  467.     i=0
  468.  
  469.     for udir in $(ls -a |egrep -v "^conf$|^web$|^dns$|^mail$|^\.\.$|^\.$"); do
  470.         exclusion=$(echo "$USER" |tr ',' '\n' |grep "^$udir$")
  471.         if [ -z "$exclusion" ]; then
  472.             ((i ++))
  473.             udir_list="$udir_list $udir"
  474.             echo -e "$(date "+%F %T") adding $udir" |tee -a $BACKUP/$user.log
  475.  
  476.             # Backup files and dirs
  477.             tar -cpf $tmpdir/user_dir/$udir.tar $udir
  478.  
  479.             # Compress arhive
  480.             $GZIP -$BACKUP_GZIP $tmpdir/user_dir/$udir.tar
  481.         fi
  482.     done
  483.     set +f
  484.     udir_list=$(echo "$udir_list" |sed -e "s/  */\ /g" -e "s/^ //")
  485.  
  486.     # Print total
  487.     if [ "$i" -eq 1 ]; then
  488.         echo -e "$(date "+%F %T") *** $i user directory ***" |\
  489.             tee -a $BACKUP/$user.log
  490.     else
  491.         echo -e "$(date "+%F %T") *** $i directories ***" |\
  492.             tee -a $BACKUP/$user.log
  493.     fi
  494. fi
  495.  
  496. # Get backup size
  497. size="$(du -shm $tmpdir |cut -f 1)"
  498.  
  499. # Get current time
  500. end_time=$(date '+%s')
  501. time_n_date=$(date +'%T %F')
  502. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  503. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  504. backup_new_date=$(date +"%Y-%m-%d_%H-%M-%S")
  505.  
  506. # Defining local storage function
  507. local_backup(){
  508.  
  509.     rm -f $BACKUP/$user.$backup_new_date.tar
  510.  
  511.     # Checking retention
  512.     backup_list=$(ls -lrt $BACKUP/ |awk '{print $9}' |grep "^$user\." | grep ".tar")
  513.     backups_count=$(echo "$backup_list" |wc -l)
  514.     if [ "$BACKUPS" -le "$backups_count" ]; then
  515.         backups_rm_number=$((backups_count - BACKUPS))
  516.  
  517.         # Removing old backup
  518.         for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  519.             backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar$//")
  520.             echo -e "$(date "+%F %T") Rotated: $backup_date" |\
  521.                 tee -a $BACKUP/$user.log
  522.             rm -f $BACKUP/$backup
  523.         done
  524.     fi
  525.  
  526.     # Checking disk space
  527.     disk_usage=$(df $BACKUP |tail -n1 |tr ' ' '\n' |grep % |cut -f 1 -d %)
  528.     if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
  529.         rm -rf $tmpdir
  530.         rm -f $BACKUP/$user.log
  531.         sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  532.         echo "Not enough disk space" |$SENDMAIL -s "$subj" $email $notify
  533.         check_result "$E_DISK" "Not enough dsk space"
  534.     fi
  535.  
  536.     # Creating final tarball
  537.     cd $tmpdir
  538.     tar -cf $BACKUP/$user.$backup_new_date.tar .
  539.     chmod 640 $BACKUP/$user.$backup_new_date.tar
  540.     chown admin:$user $BACKUP/$user.$backup_new_date.tar
  541.     localbackup='yes'
  542.     echo -e "$(date "+%F %T") Local: $BACKUP/$user.$backup_new_date.tar" |\
  543.         tee -a $BACKUP/$user.log
  544. }
  545.  
  546.  
  547. # Defining ftp command function
  548. ftpc() {
  549.     /usr/bin/ftp -n $HOST $PORT <<EOF
  550.     quote USER $USERNAME
  551.     quote PASS $PASSWORD
  552.     binary
  553.     $1
  554.     $2
  555.     $3
  556.     quit
  557. EOF
  558. }
  559.  
  560. # Defining ftp storage function
  561. ftp_backup() {
  562.     # Checking config
  563.     if [ ! -e "$VESTA/conf/ftp.backup.conf" ]; then
  564.         error="ftp.backup.conf doesn't exist"
  565.         rm -rf $tmpdir
  566.         rm -f $BACKUP/$user.log
  567.         echo "$error" |$SENDMAIL -s "$subj" $email $notify
  568.         sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  569.         check_result "$E_NOTEXIST" "$error"
  570.     fi
  571.  
  572.     # Parse config
  573.     source $VESTA/conf/ftp.backup.conf
  574.  
  575.     # Set default port
  576.     if [ -z "$(grep 'PORT=' $VESTA/conf/ftp.backup.conf)" ]; then
  577.         PORT='21'
  578.     fi
  579.  
  580.     # Checking variables
  581.     if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
  582.         error="Can't parse ftp backup configuration"
  583.         rm -rf $tmpdir
  584.         rm -f $BACKUP/$user.log
  585.         echo "$error" |$SENDMAIL -s "$subj" $email $notify
  586.         sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  587.         check_result "$E_PARSING" "$error"
  588.     fi
  589.  
  590.     # Debug info
  591.     echo -e "$(date "+%F %T") Remote: ftp://$HOST$BPATH/$user.$backup_new_date.tar"
  592.  
  593.     # Checking ftp connection
  594.     fconn=$(ftpc)
  595.     ferror=$(echo $fconn |grep -i -e failed -e error -e "Can't" -e "not conn")
  596.     if [ ! -z "$ferror" ]; then
  597.         error="Error: can't login to ftp ftp://$USERNAME@$HOST"
  598.         rm -rf $tmpdir
  599.         rm -f $BACKUP/$user.log
  600.         echo "$error" |$SENDMAIL -s "$subj" $email $notify
  601.         sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  602.         check_result "$E_CONNECT" "$error"
  603.     fi
  604.  
  605.     # Check ftp permissions
  606.     if [ -z $BPATH ]; then
  607.             ftmpdir="vst.bK76A9SUkt"
  608.         else
  609.             ftpc "mkdir $BPATH" > /dev/null 2>&1
  610.             ftmpdir="$BPATH/vst.bK76A9SUkt"
  611.     fi
  612.     ftpc "mkdir $ftmpdir" "rm $ftmpdir"
  613.     ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir" |grep -v Trying)
  614.     if [ ! -z "$ftp_result" ] ; then
  615.         error="Can't create ftp backup folder ftp://$HOST$BPATH"
  616.         rm -rf $tmpdir
  617.         rm -f $BACKUP/$user.log
  618.         echo "$error" |$SENDMAIL -s "$subj" $email $notify
  619.         sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  620.         check_result "$E_FTP" "$error"
  621.     fi
  622.  
  623.     # Checking retention
  624.     if [ -z $BPATH ]; then
  625.         backup_list=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.")
  626.     else
  627.         backup_list=$(ftpc "cd $BPATH" "ls" |awk '{print $9}' |grep "^$user\.")
  628.     fi
  629.     backups_count=$(echo "$backup_list" |wc -l)
  630.     if [ "$backups_count" -ge "$BACKUPS" ]; then
  631.         backups_rm_number=$((backups_count - BACKUPS))
  632.         for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  633.             backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar$//")
  634.             echo -e "$(date "+%F %T") Rotated ftp backup: $backup_date" |\
  635.                 tee -a $BACKUP/$user.log
  636.             if [ -z $BPATH ]; then
  637.                 ftpc "delete $backup"
  638.             else
  639.                 ftpc "cd $BPATH" "delete $backup"
  640.             fi
  641.         done
  642.     fi
  643.  
  644.     # Uploading backup archive
  645.     if [ "$localbackup" = 'yes' ]; then
  646.         cd $BACKUP
  647.         if [ -z $BPATH ]; then
  648.             ftpc "put $user.$backup_new_date.tar"
  649.          else
  650.             ftpc "cd $BPATH" "put $user.$backup_new_date.tar"
  651.         fi
  652.     else
  653.         cd $tmpdir
  654.         tar -cf $BACKUP/$user.$backup_new_date.tar .
  655.         cd $BACKUP/
  656.         if [ -z $BPATH ]; then
  657.             ftpc "put $user.$backup_new_date.tar"
  658.         else
  659.             ftpc "cd $BPATH" "put $user.$backup_new_date.tar"
  660.         fi
  661.         rm -f $user.$backup_new_date.tar
  662.     fi
  663. }
  664.  
  665. # sftp command function
  666. sftpc() {
  667.     expect -f "-" <<EOF "$@"
  668.         set timeout 60
  669.         set count 0
  670.         spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
  671.             -o Port=$PORT $USERNAME@$HOST
  672.         expect {
  673.             "password:" {
  674.                 send "$PASSWORD\r"
  675.                 exp_continue
  676.             }
  677.  
  678.             -re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
  679.                 set count \$argc
  680.                 set output "Disconnected."
  681.                 set rc $E_FTP
  682.                 exp_continue
  683.             }
  684.  
  685.             -re ".*denied.*(publickey|password)." {
  686.                 set output "Permission denied, wrong publickey or password."
  687.                 set rc $E_CONNECT
  688.             }
  689.  
  690.             -re "\[0-9]*%" {
  691.                 exp_continue
  692.             }
  693.  
  694.             "sftp>" {
  695.                 if {\$count < \$argc} {
  696.                     set arg [lindex \$argv \$count]
  697.                     send "\$arg\r"
  698.                     incr count
  699.                 } else {
  700.                     send "exit\r"
  701.                     set output "Disconnected."
  702.                     if {[info exists rc] != 1} {
  703.                         set rc $OK
  704.                     }
  705.                 }
  706.                 exp_continue
  707.             }
  708.  
  709.             timeout {
  710.                 set output "Connection timeout."
  711.                 set rc $E_CONNECT
  712.             }
  713.         }
  714.  
  715.         if {[info exists output] == 1} {
  716.             puts "\$output"
  717.         }
  718.  
  719.     exit \$rc
  720. EOF
  721. }
  722.  
  723. sftp_backup() {
  724.  
  725.     # Checking config
  726.     if [ ! -e "$VESTA/conf/sftp.backup.conf" ]; then
  727.         error="Can't open sftp.backup.conf"
  728.         rm -rf $tmpdir
  729.         rm -f $BACKUP/$user.log
  730.         echo "$error" |$SENDMAIL -s "$subj" $email $notify
  731.         sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  732.         check_result "$E_NOTEXIST" "$error"
  733.     fi
  734.  
  735.     # Parse config
  736.     source $VESTA/conf/sftp.backup.conf
  737.  
  738.     # Set default port
  739.     if [ -z "$(grep 'PORT=' $VESTA/conf/sftp.backup.conf)" ]; then
  740.         PORT='22'
  741.     fi
  742.  
  743.     # Checking variables
  744.     if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
  745.         error="Can't parse sftp backup configuration"
  746.         rm -rf $tmpdir
  747.         rm -f $BACKUP/$user.log
  748.         echo "$error" |$SENDMAIL -s "$subj" $email $notify
  749.         sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  750.         check_result "$E_PARSING" "$error"
  751.     fi
  752.  
  753.     # Debug info
  754.     echo -e "$(date "+%F %T") Remote: sftp://$HOST/$BPATH/$user.$backup_new_date.tar" |\
  755.         tee -a $BACKUP/$user.log
  756.  
  757.     # Checking network connection and write permissions
  758.     if [ -z $BPATH ]; then
  759.         sftmpdir="vst.bK76A9SUkt"
  760.     else
  761.         sftmpdir="$BPATH/vst.bK76A9SUkt"
  762.     fi
  763.     sftpc "mkdir $BPATH" > /dev/null 2>&1
  764.     sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
  765.     rc=$?
  766.     if [[ "$rc" != 0 ]]; then
  767.         case $rc in
  768.             $E_CONNECT) error="Can't login to sftp host $HOST" ;;
  769.             $E_FTP) error="Can't create temp folder on sftp $HOST" ;;
  770.         esac
  771.         rm -rf $tmpdir
  772.         rm -f $BACKUP/$user.log
  773.         echo "$error" |$SENDMAIL -s "$subj" $email $notify
  774.         sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  775.         check_result "$rc" "$error"
  776.     fi
  777.  
  778.     # Checking retention
  779.     if [ -z $BPATH ]; then
  780.         backup_list=$(sftpc "ls -l" |awk '{print $9}'|grep "^$user\.")
  781.     else
  782.         backup_list=$(sftpc "cd $BPATH" "ls -l" |awk '{print $9}'|grep "^$user\.")
  783.     fi
  784.     backups_count=$(echo "$backup_list" |wc -l)
  785.     if [ "$backups_count" -ge "$BACKUPS" ]; then
  786.         backups_rm_number=$((backups_count - BACKUPS))
  787.         for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  788.             backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar.*$//")
  789.             echo -e "$(date "+%F %T") Rotated sftp backup: $backup_date" |\
  790.                 tee -a $BACKUP/$user.log
  791.             if [ -z $BPATH ]; then
  792.                 sftpc "rm $backup" > /dev/null 2>&1
  793.             else
  794.                 sftpc "cd $BPATH" "rm $backup" > /dev/null 2>&1
  795.             fi
  796.         done
  797.     fi
  798.  
  799.     # Uploading backup archive
  800.     echo "$(date "+%F %T") Uploading $user.$backup_new_date.tar"|tee -a $BACKUP/$user.log
  801.     if [ "$localbackup" = 'yes' ]; then
  802.         cd $BACKUP
  803.         if [ -z $BPATH ]; then
  804.             sftpc "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  805.         else
  806.             sftpc "cd $BPATH" "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  807.         fi
  808.     else
  809.         cd $tmpdir
  810.         tar -cf $BACKUP/$user.$backup_new_date.tar .
  811.         cd $BACKUP/
  812.         if [ -z $BPATH ]; then
  813.             sftpc "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  814.         else
  815.             sftpc "cd $BPATH" "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  816.         fi
  817.         rm -f $user.$backup_new_date.tar
  818.     fi
  819. }
  820.  
  821. google_backup() {
  822.  
  823.     # Defining google settings
  824.     source $VESTA/conf/google.backup.conf
  825.     gsutil="$VESTA/3rdparty/gsutil/gsutil"
  826.     export BOTO_CONFIG="$VESTA/conf/.google.backup.boto"
  827.  
  828.     # Debug info
  829.     echo -e "$(date "+%F %T") Remote: gs://$BUCKET/$BPATH/$user.$backup_new_date.tar"
  830.  
  831.     # Checking retention
  832.     backup_list=$(${gsutil} ls gs://$BUCKET/$BPATH/$user.* 2>/dev/null)
  833.     backups_count=$(echo "$backup_list" |wc -l)
  834.     if [ "$backups_count" -ge "$BACKUPS" ]; then
  835.         backups_rm_number=$((backups_count - BACKUPS))
  836.         for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  837.             echo -e "$(date "+%F %T") Roated gcp backup: $backup"
  838.             $gsutil rm $backup > /dev/null 2>&1
  839.         done
  840.     fi
  841.  
  842.     # Uploading backup archive
  843.     echo -e "$(date "+%F %T") Uploading $user.$backup_new_date.tar ..."
  844.     if [ "$localbackup" = 'yes' ]; then
  845.         cd $BACKUP
  846.         ${gsutil} cp $user.$backup_new_date.tar gs://$BUCKET/$BPATH/ > /dev/null 2>&1
  847.     else
  848.         cd $tmpdir
  849.         tar -cf $BACKUP/$user.$backup_new_date.tar .
  850.         cd $BACKUP/
  851.         ${gsutil} cp $user.$backup_new_date.tar gs://$BUCKET/$BPATH/ > /dev/null 2>&1
  852.         rc=$?
  853.         rm -f $user.$backup_new_date.tar
  854.         if [ "$rc" -ne 0 ]; then
  855.             check_result "$E_CONNECT" "gsutil failed to upload $user.$backup_new_date.tar"
  856.         fi
  857.     fi
  858. }
  859.  
  860.  
  861. echo -e "\n-- SUMMARY --" |tee -a $BACKUP/$user.log
  862.  
  863. # Switching on backup system types
  864. for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\\n}"); do
  865.     case $backup_type in
  866.         local) local_backup ;;
  867.         ftp)   ftp_backup ;;
  868.         sftp)  sftp_backup ;;
  869.         google) google_backup ;;
  870.     esac
  871. done
  872.  
  873. # Removing tmpdir
  874. rm -rf $tmpdir
  875.  
  876. # Calculation run time
  877. run_time=$((end_time - start_time))
  878. run_time=$((run_time / 60))
  879. current_time=$(date "+%T")
  880. if [ "$run_time" -lt 1 ]; then
  881.     run_time=1
  882. fi
  883. min=minutes
  884. if [ "$run_time" -eq 1 ]; then
  885.     min=minute
  886. fi
  887.  
  888. echo "$(date "+%F %T") Size: $size MB" |tee -a $BACKUP/$user.log
  889. echo "$(date "+%F %T") Runtime: $run_time $min" |tee -a $BACKUP/$user.log
  890.  
  891.  
  892. #----------------------------------------------------------#
  893. #                       Vesta                              #
  894. #----------------------------------------------------------#
  895.  
  896. # Removing duplicate
  897. touch $USER_DATA/backup.conf
  898. sed -i "/$user.$backup_new_date.tar/d" $USER_DATA/backup.conf
  899.  
  900. # Registering new backup
  901. backup_str="BACKUP='$user.$backup_new_date.tar'"
  902. backup_str="$backup_str TYPE='$BACKUP_SYSTEM' SIZE='$size'"
  903. backup_str="$backup_str WEB='${web_list// /,}'"
  904. backup_str="$backup_str DNS='${dns_list// /,}'"
  905. backup_str="$backup_str MAIL='${mail_list// /,}'"
  906. backup_str="$backup_str DB='${db_list// /,}'"
  907. backup_str="$backup_str CRON='$cron_list'"
  908. backup_str="$backup_str UDIR='${udir_list// /,}'"
  909. backup_str="$backup_str RUNTIME='$run_time' TIME='$time' DATE='$date'"
  910. echo "$backup_str" >> $USER_DATA/backup.conf
  911.  
  912. # Removing old backups
  913. tail -n $BACKUPS $USER_DATA/backup.conf > $USER_DATA/backup.conf_
  914. mv -f $USER_DATA/backup.conf_ $USER_DATA/backup.conf
  915. chmod 660 $USER_DATA/backup.conf
  916.  
  917. # Deleting task from queue
  918. sed -i "/v-backup-user $user /d" $VESTA/data/queue/backup.pipe
  919.  
  920. U_BACKUPS=$(grep BACKUP $USER_DATA/backup.conf |wc -l)
  921. update_user_value "$user" '$U_BACKUPS' "$U_BACKUPS"
  922.  
  923. # Send notification
  924. if [ -e "$BACKUP/$user.log" ]; then
  925.     cd $BACKUP
  926.     subj="$user → backup has been completed"
  927.     email=$(get_user_value '$CONTACT')
  928.     cat $BACKUP/$user.log |$SENDMAIL -s "$subj" $email $notify
  929.     rm $BACKUP/$user.log
  930. fi
  931.  
  932. # Logging
  933. log_event "$OK" "$ARGUMENTS"
  934.  
  935. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement