v-backup-user 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Importing system variables
  10. source /etc/profile
  11. # Argument definition
  12. user=$1
  13. notify=${2-no}
  14. # Includes
  15. source $VESTA/func/main.sh
  16. source $VESTA/func/domain.sh
  17. source $VESTA/func/db.sh
  18. source $VESTA/conf/vesta.conf
  19. #----------------------------------------------------------#
  20. # Verifications #
  21. #----------------------------------------------------------#
  22. check_args '1' "$#" 'USER [NOTIFY]'
  23. is_format_valid 'user'
  24. is_system_enabled "$BACKUP_SYSTEM" 'BACKUP_SYSTEM'
  25. is_object_valid 'user' 'USER' "$user"
  26. is_object_unsuspended 'user' 'USER' "$user"
  27. is_backup_enabled
  28. #----------------------------------------------------------#
  29. # Action #
  30. #----------------------------------------------------------#
  31. # Set backup directory if undefined
  32. if [ -z "$BACKUP" ]; then
  33. BACKUP=/backup
  34. fi
  35. mkdir -p $BACKUP
  36. # Get current time
  37. start_time=$(date '+%s')
  38. # Set notification email and subject
  39. subj="$user → backup failed"
  40. email=$(grep CONTACT $VESTA/data/users/admin/user.conf |cut -f 2 -d \')
  41. # Checking load average
  42. la=$(cat /proc/loadavg |cut -f 1 -d ' ' |cut -f 1 -d '.')
  43. i=0
  44. while [ "$la" -ge "$BACKUP_LA_LIMIT" ]; do
  45. echo -e "$(date "+%F %T") Load Average $la"
  46. sleep 60
  47. if [ "$i" -ge "15" ]; then
  48. la_error="LoadAverage $la is above threshold"
  49. echo "$la_error" |$SENDMAIL -s "$subj" $email $notify
  50. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  51. check_result $E_LA "$la_error"
  52. fi
  53. la=$(cat /proc/loadavg |cut -f 1 -d ' ' |cut -f 1 -d '.')
  54. (( ++i))
  55. done
  56. # Creating temporary directory
  57. tmpdir=$(mktemp -p $BACKUP -d)
  58. if [ "$?" -ne 0 ]; then
  59. echo "Can't create tmp dir $tmpdir" |$SENDMAIL -s "$subj" $email $notify
  60. check_result $E_NOTEXIST "can't create tmp dir"
  61. fi
  62. # Backup sys configs
  63. echo "-- SYSTEM --" |tee $BACKUP/$user.log
  64. mkdir $tmpdir/vesta
  65. echo -e "$(date "+%F %T") $user.conf" |tee -a $BACKUP/$user.log
  66. cp -r $USER_DATA/user.conf $tmpdir/vesta/
  67. cp -r $USER_DATA/ssl $tmpdir/vesta/
  68. if [ -e "$USER_DATA/stats.log" ]; then
  69. echo -e "$(date "+%F %T") stats.log" |tee -a $BACKUP/$user.log
  70. cp -r $USER_DATA/stats.log $tmpdir/vesta/
  71. fi
  72. if [ -e "$USER_DATA/history.log" ]; then
  73. echo -e "$(date "+%F %T") history.log" |tee -a $BACKUP/$user.log
  74. cp -r $USER_DATA/history.log $tmpdir/vesta/
  75. fi
  76. if [ -e "$USER_DATA/backup-excludes.conf" ]; then
  77. echo -e "$(date "+%F %T") backup-excludes.conf" |tee -a $BACKUP/$user.log
  78. cp -r $USER_DATA/backup-excludes.conf $tmpdir/vesta/
  79. fi
  80. # Backup PAM
  81. mkdir $tmpdir/pam
  82. echo -e "$(date "+%F %T") pam" |tee -a $BACKUP/$user.log
  83. grep "^$user:" /etc/passwd > $tmpdir/pam/passwd
  84. grep "^$user:" /etc/shadow > $tmpdir/pam/shadow
  85. grep "^$user:" /etc/group > $tmpdir/pam/group
  86. echo
  87. # Parsing excludes
  88. if [ -e "$USER_DATA/backup-excludes.conf" ]; then
  89. source $USER_DATA/backup-excludes.conf
  90. fi
  91. # WEB domains
  92. if [ ! -z "$WEB_SYSTEM" ] && [ "$WEB" != '*' ]; then
  93. echo -e "\n-- WEB --" |tee -a $BACKUP/$user.log
  94. mkdir $tmpdir/web/
  95. # Parsing domain exclusions
  96. conf="$USER_DATA/web.conf"
  97. for domain in $(search_objects 'web' 'SUSPENDED' "*" 'DOMAIN'); do
  98. exclusion=$(echo -e "$WEB" |tr ',' '\n' |grep "^$domain$")
  99. if [ -z "$exclusion" ]; then
  100. web_list="$web_list $domain"
  101. else
  102. echo "$(date "+%F %T") excluding $domain"|tee -a $BACKUP/$user.log
  103. fi
  104. done
  105. web_list=$(echo "$web_list" |sed -e "s/ */\ /g" -e "s/^ //")
  106. i=0
  107. for domain in $web_list; do
  108. ((i ++))
  109. echo -e "$(date "+%F %T") $domain" |tee -a $BACKUP/$user.log
  110. mkdir -p $tmpdir/web/$domain/conf
  111. mkdir -p $tmpdir/web/$domain/vesta
  112. # Get domain variables
  113. domain_idn=$(idn -t --quiet -a "$domain")
  114. get_domain_values 'web'
  115. # Backup web.conf
  116. cd $tmpdir/web/$domain/
  117. conf="$USER_DATA/web.conf"
  118. grep "DOMAIN='$domain'" $conf > vesta/web.conf
  119. # Backup vhost config
  120. tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.tpl"
  121. conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
  122. get_web_config_lines $tpl_file $conf
  123. sed -n "$top_line,$bottom_line p" $conf > conf/$WEB_SYSTEM.conf
  124. # Backup ssl vhost
  125. if [ "$SSL" = 'yes' ]; then
  126. tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.stpl"
  127. conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
  128. get_web_config_lines $tpl_file $conf
  129. sed -n "$top_line,$bottom_line p" $conf > conf/s$WEB_SYSTEM.conf
  130. fi
  131. # Backup proxy config
  132. if [ ! -z "$PROXY_SYSTEM" ] && [ ! -z "$PROXY" ]; then
  133. tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.tpl"
  134. conf="$HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf"
  135. get_web_config_lines $tpl_file $conf
  136. sed -n "$top_line,$bottom_line p" $conf > conf/$PROXY_SYSTEM.conf
  137. fi
  138. # Backup ssl proxy config
  139. if [ ! -z "$PROXY_SYSTEM" ] && [ "$SSL" = 'yes' ]; then
  140. tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.stpl"
  141. conf="$HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf"
  142. get_web_config_lines $tpl_file $conf
  143. sed -n "$top_line,$bottom_line p" $conf > conf/s$PROXY_SYSTEM.conf
  144. fi
  145. # Backup custom config
  146. for sconfig in $(ls $HOMEDIR/$user/conf/web/|grep ".$domain.conf"); do
  147. cp $HOMEDIR/$user/conf/web/$sconfig conf/
  148. done
  149. # Backup ssl certificates
  150. if [ "$SSL" = 'yes' ] ; then
  151. cp $HOMEDIR/$user/conf/web/ssl.$domain.* conf/
  152. cp $USER_DATA/ssl/$domain.* vesta/
  153. fi
  154. # Define exclude arguments
  155. exlusion=$(echo -e "$WEB" |tr ',' '\n' |grep "^$domain:")
  156. set -f
  157. fargs=()
  158. fargs+=(--exclude='logs/*')
  159. if [ ! -z "$exlusion" ]; then
  160. xdirs="$(echo -e "$exlusion" |tr ':' '\n' |grep -v $domain)"
  161. for xpath in $xdirs; do
  162. fargs+=(--exclude=$xpath/*)
  163. echo "$(date "+%F %T") excluding directory $xpath"
  164. msg="$msg\n$(date "+%F %T") excluding directory $xpath"
  165. done
  166. fi
  167. set +f
  168. # Backup files
  169. cd $HOMEDIR/$user/web/$domain
  170. tar -cpf $tmpdir/web/$domain/domain_data.tar * ${fargs[@]}
  171. # Compress archive
  172. gzip -$BACKUP_GZIP $tmpdir/web/$domain/domain_data.tar
  173. done
  174. # Print total
  175. if [ "$i" -eq 1 ]; then
  176. echo -e "$(date "+%F %T") *** $i domain ***" |tee -a $BACKUP/$user.log
  177. else
  178. echo -e "$(date "+%F %T") *** $i domains ***"|tee -a $BACKUP/$user.log
  179. fi
  180. fi
  181. # DNS domains
  182. if [ ! -z "$DNS_SYSTEM" ] && [ "$DNS" != '*' ]; then
  183. echo -e "\n-- DNS --" |tee -a $BACKUP/$user.log
  184. mkdir $tmpdir/dns/
  185. # Parsing domain exclusions
  186. for domain in $(search_objects 'dns' 'SUSPENDED' "*" 'DOMAIN'); do
  187. exclusion=$(echo "$DNS" |tr ',' '\n' |grep "^$domain$")
  188. if [ -z "$exclusion" ]; then
  189. dns_list="$dns_list $domain"
  190. else
  191. echo "$(date "+%F %T") excluding $domain"
  192. msg="$msg\n$(date "+%F %T") excluding $domain"
  193. fi
  194. done
  195. dns_list=$(echo "$dns_list" |sed -e "s/ */\ /g" -e "s/^ //")
  196. i=0
  197. for domain in $dns_list; do
  198. ((i ++))
  199. echo -e "$(date "+%F %T") $domain" |tee -a $BACKUP/$user.log
  200. # Building directory tree
  201. mkdir -p $tmpdir/dns/$domain/conf
  202. mkdir -p $tmpdir/dns/$domain/vesta
  203. # Backup dns.conf
  204. cd $tmpdir/dns/$domain/
  205. conf="$USER_DATA/dns.conf"
  206. grep "DOMAIN='$domain'" $conf > vesta/dns.conf
  207. # Backup dns recods
  208. cp $USER_DATA/dns/$domain.conf vesta/$domain.conf
  209. if [ "$DNS_SYSTEM" != 'remote' ]; then
  210. cp $HOMEDIR/$user/conf/dns/$domain.db conf/$domain.db
  211. fi
  212. done
  213. # Print total
  214. if [ "$i" -eq 1 ]; then
  215. echo -e "$(date "+%F %T") *** $i domain ***" |tee -a $BACKUP/$user.log
  216. else
  217. echo -e "$(date "+%F %T") *** $i domains ***"|tee -a $BACKUP/$user.log
  218. fi
  219. fi
  220. # Mail domains
  221. if [ ! -z "$MAIL_SYSTEM" ] && [ "$MAIL" != '*' ]; then
  222. echo -e "\n-- MAIL --" |tee -a $BACKUP/$user.log
  223. mkdir $tmpdir/mail/
  224. # Parsing domain exclusions
  225. conf="$USER_DATA/mail.conf"
  226. for domain in $(search_objects 'mail' 'SUSPENDED' "*" 'DOMAIN'); do
  227. check_exl=$(echo "$MAIL" |tr ',' '\n' |grep "^$domain$")
  228. if [ -z "$check_exl" ]; then
  229. mail_list="$mail_list $domain"
  230. else
  231. echo "$(date "+%F %T") excluding $domain"|tee -a $BACKUP/$user.log
  232. fi
  233. done
  234. mail_list=$(echo "$mail_list" |sed -e "s/ */\ /g" -e "s/^ //")
  235. i=0
  236. for domain in $mail_list; do
  237. ((i ++))
  238. echo -e "$(date "+%F %T") $domain" |tee -a $BACKUP/$user.log
  239. mkdir -p $tmpdir/mail/$domain/conf
  240. mkdir -p $tmpdir/mail/$domain/vesta
  241. domain_idn=$(idn -t --quiet -a "$domain")
  242. # Backup exim config
  243. if [[ "$MAIL_SYSTEM" =~ exim ]]; then
  244. cd $tmpdir/mail/$domain/
  245. cp $HOMEDIR/$user/conf/mail/$domain/* conf/
  246. fi
  247. # Backup mail.conf
  248. conf="$USER_DATA/mail.conf"
  249. grep "DOMAIN='$domain'" $conf > vesta/mail.conf
  250. cp $USER_DATA/mail/$domain.* vesta/
  251. if [ ! -z "$(ls $USER_DATA/mail/|grep *@$domain)" ]; then
  252. cp $USER_DATA/mail/*@$domain.* vesta/
  253. fi
  254. # Backup emails
  255. cd $HOMEDIR/$user/mail/$domain_idn
  256. for account in $(ls); do
  257. exclusion=$(echo "$MAIL" |tr ',' '\n' |grep "$domain:")
  258. exclusion=$(echo "$exclusion" |tr ':' '\n' |grep "^$account$")
  259. # Checking exlusions
  260. if [ -z "$exclusion" ] && [[ "$MAIL_SYSTEM" =~ exim ]]; then
  261. echo "$(date "+%F %T") + $account@$domain" |\
  262. tee -a $BACKUP/$user.log
  263. touch $tmpdir/mail/$domain/accounts.tar
  264. tar -rpf $tmpdir/mail/$domain/accounts.tar $account
  265. else
  266. echo "$(date "+%F %T") excluding mail account $account" |\
  267. tee -a $BACKUP/$user.log
  268. fi
  269. done
  270. # Compress archive
  271. if [ -e "$tmpdir/mail/$domain/accounts.tar" ]; then
  272. gzip -$BACKUP_GZIP $tmpdir/mail/$domain/accounts.tar
  273. fi
  274. done
  275. # Print total
  276. if [ "$i" -eq 1 ]; then
  277. echo -e "$(date "+%F %T") *** $i domain ***" |tee -a $BACKUP/$user.log
  278. else
  279. echo -e "$(date "+%F %T") *** $i domains ***"|tee -a $BACKUP/$user.log
  280. fi
  281. fi
  282. # Databases
  283. if [ ! -z "$DB_SYSTEM" ] && [ "$DB" != '*' ]; then
  284. echo -e "\n-- DB --" |tee -a $BACKUP/$user.log
  285. mkdir $tmpdir/db/
  286. # Parsing database exclusions
  287. for database in $(search_objects 'db' 'SUSPENDED' "*" 'DB'); do
  288. exclusion=$(echo "$DB" |tr ',' '\n' |grep "^$database$")
  289. if [ -z "$exclusion" ]; then
  290. db_list="$db_list $database"
  291. else
  292. echo "$(date "+%F %T") excluding $database" |\
  293. tee -a $BACKUP/$user.log
  294. fi
  295. done
  296. i=0
  297. conf="$USER_DATA/db.conf"
  298. db_list=$(echo "$db_list" |sed -e "s/ */\ /g" -e "s/^ //")
  299. for database in $db_list; do
  300. ((i ++))
  301. get_database_values
  302. echo -e "$(date "+%F %T") $database ($TYPE)" |tee -a $BACKUP/$user.log
  303. mkdir -p $tmpdir/db/$database/conf
  304. mkdir -p $tmpdir/db/$database/vesta
  305. cd $tmpdir/db/$database/
  306. grep "DB='$database'" $conf > vesta/db.conf
  307. dump="$tmpdir/db/$database/$database.$TYPE.sql"
  308. grants="$tmpdir/db/$database/conf/$database.$TYPE.$DBUSER"
  309. case $TYPE in
  310. mysql) dump_mysql_database ;;
  311. pgsql) dump_pgsql_database ;;
  312. esac
  313. # Compress dump
  314. gzip -$BACKUP_GZIP $dump
  315. done
  316. # Print total
  317. if [ "$i" -eq 1 ]; then
  318. echo -e "$(date "+%F %T") *** $i database ***" |\
  319. tee -a $BACKUP/$user.log
  320. else
  321. echo -e "$(date "+%F %T") *** $i databases ***"|\
  322. tee -a $BACKUP/$user.log
  323. fi
  324. fi
  325. # Cron jobs
  326. if [ ! -z "$CRON_SYSTEM" ] && [ "$CRON" != '*' ]; then
  327. echo -e "\n-- CRON --" |tee -a $BACKUP/$user.log
  328. mkdir $tmpdir/cron/
  329. # Backup cron.conf
  330. cp $USER_DATA/cron.conf $tmpdir/cron/
  331. cron_record=$(wc -l $USER_DATA/cron.conf|cut -f 1 -d ' ')
  332. if [ -e "/var/spool/cron/$user" ]; then
  333. cron_list="$cron_record"
  334. cp /var/spool/cron/$user $tmpdir/cron/
  335. fi
  336. # Print total
  337. if [ "$cron_record" -eq 1 ]; then
  338. echo -e "$(date "+%F %T") *** $cron_record job ***" |\
  339. tee -a $BACKUP/$user.log
  340. else
  341. echo -e "$(date "+%F %T") *** $cron_record jobs ***" |\
  342. tee -a $BACKUP/$user.log
  343. fi
  344. fi
  345. # User Directories
  346. if [ "$USER" != '*' ]; then
  347. echo -e "\n-- User Dir --" |tee -a $BACKUP/$user.log
  348. mkdir $tmpdir/user_dir
  349. cd $HOMEDIR/$user
  350. # Parsing directory exlusions
  351. USER=''
  352. if [ -e "$USER_DATA/backup-excludes.conf" ]; then
  353. source $USER_DATA/backup-excludes.conf
  354. fi
  355. fargs=()
  356. for xpath in $(echo "$USER" |tr ',' '\n'); do
  357. fargs+=(-not)
  358. fargs+=(-path)
  359. fargs+=("./$xpath*")
  360. echo "$(date "+%F %T") excluding directory $xpath" |\
  361. tee -a $BACKUP/$user.log
  362. done
  363. IFS=$'\n'
  364. set -f
  365. i=0
  366. for udir in $(ls -a |egrep -v "conf|web|dns|mail|^\.\.$|^\.$"); do
  367. exclusion=$(echo "$USER" |tr ',' '\n' |grep "^$udir$")
  368. if [ -z "$exclusion" ]; then
  369. ((i ++))
  370. udir_list="$udir_list $udir"
  371. echo -e "$(date "+%F %T") adding $udir" |tee -a $BACKUP/$user.log
  372. # Backup files and dirs
  373. tar -cpf $tmpdir/user_dir/$udir.tar $udir
  374. # Compress arhive
  375. gzip -$BACKUP_GZIP $tmpdir/user_dir/$udir.tar
  376. fi
  377. done
  378. set +f
  379. udir_list=$(echo "$udir_list" |sed -e "s/ */\ /g" -e "s/^ //")
  380. # Print total
  381. if [ "$i" -eq 1 ]; then
  382. echo -e "$(date "+%F %T") *** $i user directory ***" |\
  383. tee -a $BACKUP/$user.log
  384. else
  385. echo -e "$(date "+%F %T") *** $i directories ***" |\
  386. tee -a $BACKUP/$user.log
  387. fi
  388. fi
  389. # Get backup size
  390. size="$(du -shm $tmpdir |cut -f 1)"
  391. # Get current time
  392. end_time=$(date '+%s')
  393. time_n_date=$(date +'%T %F')
  394. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  395. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  396. # Defining local storage function
  397. local_backup(){
  398. rm -f $BACKUP/$user.$date.tar
  399. # Checking retention
  400. backup_list=$(ls -lrt $BACKUP/ |awk '{print $9}' |grep "^$user\.")
  401. backups_count=$(echo "$backup_list" |wc -l)
  402. if [ "$BACKUPS" -le "$backups_count" ]; then
  403. backups_rm_number=$((backups_count - BACKUPS))
  404. (( ++backups_rm_number))
  405. # Removing old backup
  406. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  407. backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar$//")
  408. echo -e "$(date "+%F %T") Roated: $backup_date" |\
  409. tee -a $BACKUP/$user.log
  410. rm -f $BACKUP/$backup
  411. done
  412. fi
  413. # Checking disk space
  414. disk_usage=$(df $BACKUP |tail -n1 |tr ' ' '\n' |grep % |cut -f 1 -d %)
  415. if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
  416. rm -rf $tmpdir
  417. rm -f $BACKUP/$user.log
  418. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  419. echo "Not enough disk space" |$SENDMAIL -s "$subj" $email $notify
  420. check_result "$E_DISK" "Not enough dsk space"
  421. fi
  422. # Creating final tarball
  423. cd $tmpdir
  424. tar -cf $BACKUP/$user.$date.tar .
  425. chmod 640 $BACKUP/$user.$date.tar
  426. chown admin:$user $BACKUP/$user.$date.tar
  427. localbackup='yes'
  428. echo -e "$(date "+%F %T") Local: $BACKUP/$user.$date.tar" |\
  429. tee -a $BACKUP/$user.log
  430. U_BACKUPS=$(ls $BACKUP/ |grep "^$user." |wc -l)
  431. update_user_value "$user" '$U_BACKUPS' "$U_BACKUPS"
  432. }
  433. # Defining ftp command function
  434. ftpc() {
  435. /usr/bin/ftp -n $HOST $PORT <<EOF
  436. quote USER $USERNAME
  437. quote PASS $PASSWORD
  438. binary
  439. $1
  440. $2
  441. $3
  442. quit
  443. EOF
  444. }
  445. # Defining ftp storage function
  446. ftp_backup() {
  447. # Checking config
  448. if [ ! -e "$VESTA/conf/ftp.backup.conf" ]; then
  449. error="ftp.backup.conf doesn't exist"
  450. rm -rf $tmpdir
  451. rm -f $BACKUP/$user.log
  452. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  453. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  454. check_result "$E_NOTEXIST" "$error"
  455. fi
  456. # Parse config
  457. source $VESTA/conf/ftp.backup.conf
  458. # Set default port
  459. if [ -z "$(grep 'PORT=' $VESTA/conf/ftp.backup.conf)" ]; then
  460. PORT='21'
  461. fi
  462. # Checking variables
  463. if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
  464. error="Can't parse ftp backup configuration"
  465. rm -rf $tmpdir
  466. rm -f $BACKUP/$user.log
  467. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  468. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  469. check_result "$E_PARSING" "$error"
  470. fi
  471. # Debug info
  472. echo -e "$(date "+%F %T") Remote: ftp://$HOST$BPATH/$user.$date.tar"
  473. # Checking ftp connection
  474. fconn=$(ftpc)
  475. ferror=$(echo $fconn |grep -i -e failed -e error -e "Can't" -e "not conn")
  476. if [ ! -z "$ferror" ]; then
  477. error="Error: can't login to ftp ftp://$USERNAME@$HOST"
  478. rm -rf $tmpdir
  479. rm -f $BACKUP/$user.log
  480. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  481. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  482. check_result "$E_CONNECT" "$error"
  483. fi
  484. # Check ftp permissions
  485. ftpc "mkdir $BPATH" > /dev/null 2>&1
  486. ftmpdir="$BPATH/vst.bK76A9SUkt"
  487. ftpc "mkdir $ftmpdir" "rm $ftmpdir"
  488. ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir" |grep -v Trying)
  489. if [ ! -z "$ftp_result" ] ; then
  490. error="Can't create ftp backup folder ftp://$HOST$BPATH"
  491. rm -rf $tmpdir
  492. rm -f $BACKUP/$user.log
  493. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  494. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  495. check_result "$E_FTP" "$error"
  496. fi
  497. # Checking retention
  498. backup_list=$(ftpc "cd $BPATH" "ls" |awk '{print $9}' |grep "^$user\.")
  499. backups_count=$(echo "$backup_list" |wc -l)
  500. if [ "$backups_count" -ge "$BACKUPS" ]; then
  501. backups_rm_number=$((backups_count - BACKUPS + 1))
  502. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  503. backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar$//")
  504. echo -e "$(date "+%F %T") Roated ftp backup: $backup_date" |\
  505. tee -a $BACKUP/$user.log
  506. ftpc "cd $BPATH" "delete $backup"
  507. done
  508. fi
  509. # Uploading backup archive
  510. if [ "$localbackup" = 'yes' ]; then
  511. cd $BACKUP
  512. ftpc "cd $BPATH" "put $user.$date.tar"
  513. else
  514. cd $tmpdir
  515. tar -cf $BACKUP/$user.$date.tar .
  516. cd $BACKUP/
  517. ftpc "cd $BPATH" "put $user.$date.tar"
  518. rm -f $user.$date.tar
  519. fi
  520. }
  521. # sftp command function
  522. sftpc() {
  523. expect -f "-" <<EOF "$@"
  524. set timeout 60
  525. set count 0
  526. spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
  527. -o Port=$PORT $USERNAME@$HOST
  528. expect {
  529. "password:" {
  530. send "$PASSWORD\r"
  531. exp_continue
  532. }
  533. -re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
  534. set count \$argc
  535. set output "Disconnected."
  536. set rc $E_FTP
  537. exp_continue
  538. }
  539. -re ".*denied.*(publickey|password)." {
  540. set output "Permission denied, wrong publickey or password."
  541. set rc $E_CONNECT
  542. }
  543. -re "\[0-9]*%" {
  544. exp_continue
  545. }
  546. "sftp>" {
  547. if {\$count < \$argc} {
  548. set arg [lindex \$argv \$count]
  549. send "\$arg\r"
  550. incr count
  551. } else {
  552. send "exit\r"
  553. set output "Disconnected."
  554. if {[info exists rc] != 1} {
  555. set rc $OK
  556. }
  557. }
  558. exp_continue
  559. }
  560. timeout {
  561. set output "Connection timeout."
  562. set rc $E_CONNECT
  563. }
  564. }
  565. if {[info exists output] == 1} {
  566. puts "\$output"
  567. }
  568. exit \$rc
  569. EOF
  570. }
  571. sftp_backup() {
  572. # Checking config
  573. if [ ! -e "$VESTA/conf/sftp.backup.conf" ]; then
  574. error="Can't open sftp.backup.conf"
  575. rm -rf $tmpdir
  576. rm -f $BACKUP/$user.log
  577. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  578. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  579. check_result "$E_NOTEXIST" "$error"
  580. fi
  581. # Parse config
  582. source $VESTA/conf/sftp.backup.conf
  583. # Set default port
  584. if [ -z "$(grep 'PORT=' $VESTA/conf/sftp.backup.conf)" ]; then
  585. PORT='22'
  586. fi
  587. # Checking variables
  588. if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
  589. error="Can't parse sftp backup configuration"
  590. rm -rf $tmpdir
  591. rm -f $BACKUP/$user.log
  592. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  593. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  594. check_result "$E_PARSING" "$error"
  595. fi
  596. # Debug info
  597. echo -e "$(date "+%F %T") Remote: sftp://$HOST/$BPATH/$user.$date.tar" |\
  598. tee -a $BACKUP/$user.log
  599. # Checking network connection and write permissions
  600. sftmpdir="$BPATH/vst.bK76A9SUkt"
  601. sftpc "mkdir $BPATH" > /dev/null 2>&1
  602. sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
  603. rc=$?
  604. if [[ "$rc" != 0 ]]; then
  605. case $rc in
  606. $E_CONNECT) error="Can't login to sftp host $HOST" ;;
  607. $E_FTP) error="Can't create temp folder on sftp $HOST" ;;
  608. esac
  609. rm -rf $tmpdir
  610. rm -f $BACKUP/$user.log
  611. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  612. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  613. check_result "$rc" "$error"
  614. fi
  615. # Checking retention
  616. backup_list=$(sftpc "cd $BPATH" "ls -l" |awk '{print $9}'|grep "^$user\.")
  617. backups_count=$(echo "$backup_list" |wc -l)
  618. if [ "$backups_count" -ge "$BACKUPS" ]; then
  619. backups_rm_number=$((backups_count - BACKUPS + 1))
  620. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  621. backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar.*$//")
  622. echo -e "$(date "+%F %T") Roated sftp backup: $backup_date" |\
  623. tee -a $BACKUP/$user.log
  624. sftpc "cd $BPATH" "rm $backup" > /dev/null 2>&1
  625. done
  626. fi
  627. # Uploading backup archive
  628. echo "$(date "+%F %T") Uploading $user.$date.tar"|tee -a $BACKUP/$user.log
  629. if [ "$localbackup" = 'yes' ]; then
  630. cd $BACKUP
  631. sftpc "cd $BPATH" "put $user.$date.tar" > /dev/null 2>&1
  632. else
  633. cd $tmpdir
  634. tar -cf $BACKUP/$user.$date.tar .
  635. cd $BACKUP/
  636. sftpc "cd $BPATH" "put $user.$date.tar" > /dev/null 2>&1
  637. rm -f $user.$date.tar
  638. fi
  639. }
  640. echo -e "\n-- SUMMARY --" |tee -a $BACKUP/$user.log
  641. # Switching on backup system types
  642. for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\\n}"); do
  643. case $backup_type in
  644. local) local_backup ;;
  645. ftp) ftp_backup ;;
  646. sftp) sftp_backup ;;
  647. esac
  648. done
  649. # Removing tmpdir
  650. rm -rf $tmpdir
  651. # Calculation run time
  652. run_time=$((end_time - start_time))
  653. run_time=$((run_time / 60))
  654. current_time=$(date "+%T")
  655. if [ "$run_time" -lt 1 ]; then
  656. run_time=1
  657. fi
  658. min=miutes
  659. if [ "$run_time" -eq 1 ]; then
  660. min=minute
  661. fi
  662. echo "$(date "+%F %T") Size: $size Mb" |tee -a $BACKUP/$user.log
  663. echo "$(date "+%F %T") Runtime: $run_time $min" |tee -a $BACKUP/$user.log
  664. #----------------------------------------------------------#
  665. # Vesta #
  666. #----------------------------------------------------------#
  667. # Removing duplicate
  668. touch $USER_DATA/backup.conf
  669. sed -i "/$user.$date.tar/d" $USER_DATA/backup.conf
  670. # Registering new backup
  671. backup_str="BACKUP='$user.$date.tar'"
  672. backup_str="$backup_str TYPE='$BACKUP_SYSTEM' SIZE='$size'"
  673. backup_str="$backup_str WEB='${web_list// /,}'"
  674. backup_str="$backup_str DNS='${dns_list// /,}'"
  675. backup_str="$backup_str MAIL='${mail_list// /,}'"
  676. backup_str="$backup_str DB='${db_list// /,}'"
  677. backup_str="$backup_str CRON='$cron_list'"
  678. backup_str="$backup_str UDIR='${udir_list// /,}'"
  679. backup_str="$backup_str RUNTIME='$run_time' TIME='$time' DATE='$date'"
  680. echo "$backup_str" >> $USER_DATA/backup.conf
  681. # Removing old backups
  682. tail -n $BACKUPS $USER_DATA/backup.conf > $USER_DATA/backup.conf_
  683. mv -f $USER_DATA/backup.conf_ $USER_DATA/backup.conf
  684. chmod 660 $USER_DATA/backup.conf
  685. # Deleting task from queue
  686. sed -i "/v-backup-user $user /d" $VESTA/data/queue/backup.pipe
  687. # Send notification
  688. if [ -e "$BACKUP/$user.log" ]; then
  689. subj="$user → backup has been completed"
  690. email=$(get_user_value '$CONTACT')
  691. cat $BACKUP/$user.log #|$SENDMAIL -s "$subj" $email $notify
  692. rm $BACKUP/$user.log
  693. fi
  694. # Logging
  695. log_event "$OK" "$ARGUMENTS"
  696. exit