v-backup-user 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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=$domain
  114. format_domain_idn
  115. get_domain_values 'web'
  116. # Backup web.conf
  117. cd $tmpdir/web/$domain/
  118. conf="$USER_DATA/web.conf"
  119. grep "DOMAIN='$domain'" $conf > vesta/web.conf
  120. # Backup vhost config
  121. tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.tpl"
  122. conf="$HOMEDIR/$user/conf/web/$WEB_SYSTEM.conf"
  123. get_web_config_lines $tpl_file $conf
  124. sed -n "$top_line,$bottom_line p" $conf > conf/$WEB_SYSTEM.conf
  125. # Backup ssl vhost
  126. if [ "$SSL" = 'yes' ]; then
  127. tpl_file="$WEBTPL/$WEB_SYSTEM/$WEB_BACKEND/$TPL.stpl"
  128. conf="$HOMEDIR/$user/conf/web/s$WEB_SYSTEM.conf"
  129. get_web_config_lines $tpl_file $conf
  130. sed -n "$top_line,$bottom_line p" $conf > conf/s$WEB_SYSTEM.conf
  131. fi
  132. # Backup proxy config
  133. if [ ! -z "$PROXY_SYSTEM" ] && [ ! -z "$PROXY" ]; then
  134. tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.tpl"
  135. conf="$HOMEDIR/$user/conf/web/$PROXY_SYSTEM.conf"
  136. get_web_config_lines $tpl_file $conf
  137. sed -n "$top_line,$bottom_line p" $conf > conf/$PROXY_SYSTEM.conf
  138. fi
  139. # Backup ssl proxy config
  140. if [ ! -z "$PROXY_SYSTEM" ] && [ "$SSL" = 'yes' ]; then
  141. tpl_file="$WEBTPL/$PROXY_SYSTEM/$PROXY.stpl"
  142. conf="$HOMEDIR/$user/conf/web/s$PROXY_SYSTEM.conf"
  143. get_web_config_lines $tpl_file $conf
  144. sed -n "$top_line,$bottom_line p" $conf > conf/s$PROXY_SYSTEM.conf
  145. fi
  146. # Backup custom config
  147. for sconfig in $(ls $HOMEDIR/$user/conf/web/|grep ".$domain.conf"); do
  148. cp $HOMEDIR/$user/conf/web/$sconfig conf/
  149. done
  150. # Backup ssl certificates
  151. if [ "$SSL" = 'yes' ] ; then
  152. cp $HOMEDIR/$user/conf/web/ssl.$domain.* conf/
  153. cp $USER_DATA/ssl/$domain.* vesta/
  154. fi
  155. # Define exclude arguments
  156. exlusion=$(echo -e "$WEB" |tr ',' '\n' |grep "^$domain:")
  157. set -f
  158. fargs=()
  159. fargs+=(--exclude='logs/*')
  160. if [ ! -z "$exlusion" ]; then
  161. xdirs="$(echo -e "$exlusion" |tr ':' '\n' |grep -v $domain)"
  162. for xpath in $xdirs; do
  163. fargs+=(--exclude=$xpath/*)
  164. echo "$(date "+%F %T") excluding directory $xpath"
  165. msg="$msg\n$(date "+%F %T") excluding directory $xpath"
  166. done
  167. fi
  168. set +f
  169. # Backup files
  170. cd $HOMEDIR/$user/web/$domain
  171. tar -cpf $tmpdir/web/$domain/domain_data.tar * ${fargs[@]}
  172. # Compress archive
  173. gzip -$BACKUP_GZIP $tmpdir/web/$domain/domain_data.tar
  174. done
  175. # Print total
  176. if [ "$i" -eq 1 ]; then
  177. echo -e "$(date "+%F %T") *** $i domain ***" |tee -a $BACKUP/$user.log
  178. else
  179. echo -e "$(date "+%F %T") *** $i domains ***"|tee -a $BACKUP/$user.log
  180. fi
  181. fi
  182. # DNS domains
  183. if [ ! -z "$DNS_SYSTEM" ] && [ "$DNS" != '*' ]; then
  184. echo -e "\n-- DNS --" |tee -a $BACKUP/$user.log
  185. mkdir $tmpdir/dns/
  186. # Parsing domain exclusions
  187. for domain in $(search_objects 'dns' 'SUSPENDED' "*" 'DOMAIN'); do
  188. exclusion=$(echo "$DNS" |tr ',' '\n' |grep "^$domain$")
  189. if [ -z "$exclusion" ]; then
  190. dns_list="$dns_list $domain"
  191. else
  192. echo "$(date "+%F %T") excluding $domain"
  193. msg="$msg\n$(date "+%F %T") excluding $domain"
  194. fi
  195. done
  196. dns_list=$(echo "$dns_list" |sed -e "s/ */\ /g" -e "s/^ //")
  197. i=0
  198. for domain in $dns_list; do
  199. ((i ++))
  200. echo -e "$(date "+%F %T") $domain" |tee -a $BACKUP/$user.log
  201. # Building directory tree
  202. mkdir -p $tmpdir/dns/$domain/conf
  203. mkdir -p $tmpdir/dns/$domain/vesta
  204. # Backup dns.conf
  205. cd $tmpdir/dns/$domain/
  206. conf="$USER_DATA/dns.conf"
  207. grep "DOMAIN='$domain'" $conf > vesta/dns.conf
  208. # Backup dns recods
  209. cp $USER_DATA/dns/$domain.conf vesta/$domain.conf
  210. if [ "$DNS_SYSTEM" != 'remote' ]; then
  211. cp $HOMEDIR/$user/conf/dns/$domain.db conf/$domain.db
  212. fi
  213. done
  214. # Print total
  215. if [ "$i" -eq 1 ]; then
  216. echo -e "$(date "+%F %T") *** $i domain ***" |tee -a $BACKUP/$user.log
  217. else
  218. echo -e "$(date "+%F %T") *** $i domains ***"|tee -a $BACKUP/$user.log
  219. fi
  220. fi
  221. # Mail domains
  222. if [ ! -z "$MAIL_SYSTEM" ] && [ "$MAIL" != '*' ]; then
  223. echo -e "\n-- MAIL --" |tee -a $BACKUP/$user.log
  224. mkdir $tmpdir/mail/
  225. # Parsing domain exclusions
  226. conf="$USER_DATA/mail.conf"
  227. for domain in $(search_objects 'mail' 'SUSPENDED' "*" 'DOMAIN'); do
  228. check_exl=$(echo "$MAIL" |tr ',' '\n' |grep "^$domain$")
  229. if [ -z "$check_exl" ]; then
  230. mail_list="$mail_list $domain"
  231. else
  232. echo "$(date "+%F %T") excluding $domain"|tee -a $BACKUP/$user.log
  233. fi
  234. done
  235. mail_list=$(echo "$mail_list" |sed -e "s/ */\ /g" -e "s/^ //")
  236. i=0
  237. for domain in $mail_list; do
  238. ((i ++))
  239. echo -e "$(date "+%F %T") $domain" |tee -a $BACKUP/$user.log
  240. mkdir -p $tmpdir/mail/$domain/conf
  241. mkdir -p $tmpdir/mail/$domain/vesta
  242. domain_idn=$domain
  243. format_domain_idn
  244. # Backup exim config
  245. if [[ "$MAIL_SYSTEM" =~ exim ]]; then
  246. cd $tmpdir/mail/$domain/
  247. cp $HOMEDIR/$user/conf/mail/$domain/* conf/
  248. fi
  249. # Backup mail.conf
  250. conf="$USER_DATA/mail.conf"
  251. grep "DOMAIN='$domain'" $conf > vesta/mail.conf
  252. cp $USER_DATA/mail/$domain.* vesta/
  253. if [ ! -z "$(ls $USER_DATA/mail/|grep *@$domain)" ]; then
  254. cp $USER_DATA/mail/*@$domain.* vesta/
  255. fi
  256. # Backup emails
  257. cd $HOMEDIR/$user/mail/$domain_idn
  258. for account in $(ls); do
  259. exclusion=$(echo "$MAIL" |tr ',' '\n' |grep "$domain:")
  260. exclusion=$(echo "$exclusion" |tr ':' '\n' |grep "^$account$")
  261. # Checking exlusions
  262. if [ -z "$exclusion" ] && [[ "$MAIL_SYSTEM" =~ exim ]]; then
  263. echo "$(date "+%F %T") + $account@$domain" |\
  264. tee -a $BACKUP/$user.log
  265. touch $tmpdir/mail/$domain/accounts.tar
  266. tar -rpf $tmpdir/mail/$domain/accounts.tar $account
  267. else
  268. echo "$(date "+%F %T") excluding mail account $account" |\
  269. tee -a $BACKUP/$user.log
  270. fi
  271. done
  272. # Compress archive
  273. if [ -e "$tmpdir/mail/$domain/accounts.tar" ]; then
  274. gzip -$BACKUP_GZIP $tmpdir/mail/$domain/accounts.tar
  275. fi
  276. done
  277. # Print total
  278. if [ "$i" -eq 1 ]; then
  279. echo -e "$(date "+%F %T") *** $i domain ***" |tee -a $BACKUP/$user.log
  280. else
  281. echo -e "$(date "+%F %T") *** $i domains ***"|tee -a $BACKUP/$user.log
  282. fi
  283. fi
  284. # Databases
  285. if [ ! -z "$DB_SYSTEM" ] && [ "$DB" != '*' ]; then
  286. echo -e "\n-- DB --" |tee -a $BACKUP/$user.log
  287. mkdir $tmpdir/db/
  288. # Parsing database exclusions
  289. for database in $(search_objects 'db' 'SUSPENDED' "*" 'DB'); do
  290. exclusion=$(echo "$DB" |tr ',' '\n' |grep "^$database$")
  291. if [ -z "$exclusion" ]; then
  292. db_list="$db_list $database"
  293. else
  294. echo "$(date "+%F %T") excluding $database" |\
  295. tee -a $BACKUP/$user.log
  296. fi
  297. done
  298. i=0
  299. conf="$USER_DATA/db.conf"
  300. db_list=$(echo "$db_list" |sed -e "s/ */\ /g" -e "s/^ //")
  301. for database in $db_list; do
  302. ((i ++))
  303. get_database_values
  304. echo -e "$(date "+%F %T") $database ($TYPE)" |tee -a $BACKUP/$user.log
  305. mkdir -p $tmpdir/db/$database/conf
  306. mkdir -p $tmpdir/db/$database/vesta
  307. cd $tmpdir/db/$database/
  308. grep "DB='$database'" $conf > vesta/db.conf
  309. dump="$tmpdir/db/$database/$database.$TYPE.sql"
  310. grants="$tmpdir/db/$database/conf/$database.$TYPE.$DBUSER"
  311. case $TYPE in
  312. mysql) dump_mysql_database ;;
  313. pgsql) dump_pgsql_database ;;
  314. esac
  315. # Compress dump
  316. gzip -$BACKUP_GZIP $dump
  317. done
  318. # Print total
  319. if [ "$i" -eq 1 ]; then
  320. echo -e "$(date "+%F %T") *** $i database ***" |\
  321. tee -a $BACKUP/$user.log
  322. else
  323. echo -e "$(date "+%F %T") *** $i databases ***"|\
  324. tee -a $BACKUP/$user.log
  325. fi
  326. fi
  327. # Cron jobs
  328. if [ ! -z "$CRON_SYSTEM" ] && [ "$CRON" != '*' ]; then
  329. echo -e "\n-- CRON --" |tee -a $BACKUP/$user.log
  330. mkdir $tmpdir/cron/
  331. # Backup cron.conf
  332. cp $USER_DATA/cron.conf $tmpdir/cron/
  333. cron_record=$(wc -l $USER_DATA/cron.conf|cut -f 1 -d ' ')
  334. if [ -e "/var/spool/cron/$user" ]; then
  335. cron_list="$cron_record"
  336. cp /var/spool/cron/$user $tmpdir/cron/
  337. fi
  338. # Print total
  339. if [ "$cron_record" -eq 1 ]; then
  340. echo -e "$(date "+%F %T") *** $cron_record job ***" |\
  341. tee -a $BACKUP/$user.log
  342. else
  343. echo -e "$(date "+%F %T") *** $cron_record jobs ***" |\
  344. tee -a $BACKUP/$user.log
  345. fi
  346. fi
  347. # User Directories
  348. if [ "$USER" != '*' ]; then
  349. echo -e "\n-- User Dir --" |tee -a $BACKUP/$user.log
  350. mkdir $tmpdir/user_dir
  351. cd $HOMEDIR/$user
  352. # Parsing directory exlusions
  353. USER=''
  354. if [ -e "$USER_DATA/backup-excludes.conf" ]; then
  355. source $USER_DATA/backup-excludes.conf
  356. fi
  357. fargs=()
  358. for xpath in $(echo "$USER" |tr ',' '\n'); do
  359. fargs+=(-not)
  360. fargs+=(-path)
  361. fargs+=("./$xpath*")
  362. echo "$(date "+%F %T") excluding directory $xpath" |\
  363. tee -a $BACKUP/$user.log
  364. done
  365. IFS=$'\n'
  366. set -f
  367. i=0
  368. for udir in $(ls -a |egrep -v "^conf$|^web$|^dns$|^mail$|^\.\.$|^\.$"); do
  369. exclusion=$(echo "$USER" |tr ',' '\n' |grep "^$udir$")
  370. if [ -z "$exclusion" ]; then
  371. ((i ++))
  372. udir_list="$udir_list $udir"
  373. echo -e "$(date "+%F %T") adding $udir" |tee -a $BACKUP/$user.log
  374. # Backup files and dirs
  375. tar -cpf $tmpdir/user_dir/$udir.tar $udir
  376. # Compress arhive
  377. gzip -$BACKUP_GZIP $tmpdir/user_dir/$udir.tar
  378. fi
  379. done
  380. set +f
  381. udir_list=$(echo "$udir_list" |sed -e "s/ */\ /g" -e "s/^ //")
  382. # Print total
  383. if [ "$i" -eq 1 ]; then
  384. echo -e "$(date "+%F %T") *** $i user directory ***" |\
  385. tee -a $BACKUP/$user.log
  386. else
  387. echo -e "$(date "+%F %T") *** $i directories ***" |\
  388. tee -a $BACKUP/$user.log
  389. fi
  390. fi
  391. # Get backup size
  392. size="$(du -shm $tmpdir |cut -f 1)"
  393. # Get current time
  394. end_time=$(date '+%s')
  395. time_n_date=$(date +'%T %F')
  396. time=$(echo "$time_n_date" |cut -f 1 -d \ )
  397. date=$(echo "$time_n_date" |cut -f 2 -d \ )
  398. # Defining local storage function
  399. local_backup(){
  400. rm -f $BACKUP/$user.$date.tar
  401. # Checking retention
  402. backup_list=$(ls -lrt $BACKUP/ |awk '{print $9}' |grep "^$user\.")
  403. backups_count=$(echo "$backup_list" |wc -l)
  404. if [ "$BACKUPS" -le "$backups_count" ]; then
  405. backups_rm_number=$((backups_count - BACKUPS))
  406. (( ++backups_rm_number))
  407. # Removing old backup
  408. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  409. backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar$//")
  410. echo -e "$(date "+%F %T") Rotated: $backup_date" |\
  411. tee -a $BACKUP/$user.log
  412. rm -f $BACKUP/$backup
  413. done
  414. fi
  415. # Checking disk space
  416. disk_usage=$(df $BACKUP |tail -n1 |tr ' ' '\n' |grep % |cut -f 1 -d %)
  417. if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
  418. rm -rf $tmpdir
  419. rm -f $BACKUP/$user.log
  420. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  421. echo "Not enough disk space" |$SENDMAIL -s "$subj" $email $notify
  422. check_result "$E_DISK" "Not enough dsk space"
  423. fi
  424. # Creating final tarball
  425. cd $tmpdir
  426. tar -cf $BACKUP/$user.$date.tar .
  427. chmod 640 $BACKUP/$user.$date.tar
  428. chown admin:$user $BACKUP/$user.$date.tar
  429. localbackup='yes'
  430. echo -e "$(date "+%F %T") Local: $BACKUP/$user.$date.tar" |\
  431. tee -a $BACKUP/$user.log
  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. if [ -z $BPATH ]; then
  486. ftmpdir="vst.bK76A9SUkt"
  487. else
  488. ftpc "mkdir $BPATH" > /dev/null 2>&1
  489. ftmpdir="$BPATH/vst.bK76A9SUkt"
  490. fi
  491. ftpc "mkdir $ftmpdir" "rm $ftmpdir"
  492. ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir" |grep -v Trying)
  493. if [ ! -z "$ftp_result" ] ; then
  494. error="Can't create ftp backup folder ftp://$HOST$BPATH"
  495. rm -rf $tmpdir
  496. rm -f $BACKUP/$user.log
  497. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  498. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  499. check_result "$E_FTP" "$error"
  500. fi
  501. # Checking retention
  502. if [ -z $BPATH ]; then
  503. backup_list=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.")
  504. else
  505. backup_list=$(ftpc "cd $BPATH" "ls" |awk '{print $9}' |grep "^$user\.")
  506. fi
  507. backups_count=$(echo "$backup_list" |wc -l)
  508. if [ "$backups_count" -ge "$BACKUPS" ]; then
  509. backups_rm_number=$((backups_count - BACKUPS + 1))
  510. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  511. backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar$//")
  512. echo -e "$(date "+%F %T") Rotated ftp backup: $backup_date" |\
  513. tee -a $BACKUP/$user.log
  514. if [ -z $BPATH ]; then
  515. ftpc "delete $backup"
  516. else
  517. ftpc "cd $BPATH" "delete $backup"
  518. fi
  519. done
  520. fi
  521. # Uploading backup archive
  522. if [ "$localbackup" = 'yes' ]; then
  523. cd $BACKUP
  524. if [ -z $BPATH ]; then
  525. ftpc "put $user.$date.tar"
  526. else
  527. ftpc "cd $BPATH" "put $user.$date.tar"
  528. fi
  529. else
  530. cd $tmpdir
  531. tar -cf $BACKUP/$user.$date.tar .
  532. cd $BACKUP/
  533. ftpc "cd $BPATH" "put $user.$date.tar"
  534. rm -f $user.$date.tar
  535. fi
  536. }
  537. # sftp command function
  538. sftpc() {
  539. expect -f "-" <<EOF "$@"
  540. set timeout 60
  541. set count 0
  542. spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
  543. -o Port=$PORT $USERNAME@$HOST
  544. expect {
  545. "password:" {
  546. send "$PASSWORD\r"
  547. exp_continue
  548. }
  549. -re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
  550. set count \$argc
  551. set output "Disconnected."
  552. set rc $E_FTP
  553. exp_continue
  554. }
  555. -re ".*denied.*(publickey|password)." {
  556. set output "Permission denied, wrong publickey or password."
  557. set rc $E_CONNECT
  558. }
  559. -re "\[0-9]*%" {
  560. exp_continue
  561. }
  562. "sftp>" {
  563. if {\$count < \$argc} {
  564. set arg [lindex \$argv \$count]
  565. send "\$arg\r"
  566. incr count
  567. } else {
  568. send "exit\r"
  569. set output "Disconnected."
  570. if {[info exists rc] != 1} {
  571. set rc $OK
  572. }
  573. }
  574. exp_continue
  575. }
  576. timeout {
  577. set output "Connection timeout."
  578. set rc $E_CONNECT
  579. }
  580. }
  581. if {[info exists output] == 1} {
  582. puts "\$output"
  583. }
  584. exit \$rc
  585. EOF
  586. }
  587. sftp_backup() {
  588. # Checking config
  589. if [ ! -e "$VESTA/conf/sftp.backup.conf" ]; then
  590. error="Can't open sftp.backup.conf"
  591. rm -rf $tmpdir
  592. rm -f $BACKUP/$user.log
  593. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  594. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  595. check_result "$E_NOTEXIST" "$error"
  596. fi
  597. # Parse config
  598. source $VESTA/conf/sftp.backup.conf
  599. # Set default port
  600. if [ -z "$(grep 'PORT=' $VESTA/conf/sftp.backup.conf)" ]; then
  601. PORT='22'
  602. fi
  603. # Checking variables
  604. if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
  605. error="Can't parse sftp backup configuration"
  606. rm -rf $tmpdir
  607. rm -f $BACKUP/$user.log
  608. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  609. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  610. check_result "$E_PARSING" "$error"
  611. fi
  612. # Debug info
  613. echo -e "$(date "+%F %T") Remote: sftp://$HOST/$BPATH/$user.$date.tar" |\
  614. tee -a $BACKUP/$user.log
  615. # Checking network connection and write permissions
  616. sftmpdir="$BPATH/vst.bK76A9SUkt"
  617. sftpc "mkdir $BPATH" > /dev/null 2>&1
  618. sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
  619. rc=$?
  620. if [[ "$rc" != 0 ]]; then
  621. case $rc in
  622. $E_CONNECT) error="Can't login to sftp host $HOST" ;;
  623. $E_FTP) error="Can't create temp folder on sftp $HOST" ;;
  624. esac
  625. rm -rf $tmpdir
  626. rm -f $BACKUP/$user.log
  627. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  628. sed -i "/ $user /d" $VESTA/data/queue/backup.pipe
  629. check_result "$rc" "$error"
  630. fi
  631. # Checking retention
  632. backup_list=$(sftpc "cd $BPATH" "ls -l" |awk '{print $9}'|grep "^$user\.")
  633. backups_count=$(echo "$backup_list" |wc -l)
  634. if [ "$backups_count" -ge "$BACKUPS" ]; then
  635. backups_rm_number=$((backups_count - BACKUPS + 1))
  636. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  637. backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar.*$//")
  638. echo -e "$(date "+%F %T") Rotated sftp backup: $backup_date" |\
  639. tee -a $BACKUP/$user.log
  640. sftpc "cd $BPATH" "rm $backup" > /dev/null 2>&1
  641. done
  642. fi
  643. # Uploading backup archive
  644. echo "$(date "+%F %T") Uploading $user.$date.tar"|tee -a $BACKUP/$user.log
  645. if [ "$localbackup" = 'yes' ]; then
  646. cd $BACKUP
  647. sftpc "cd $BPATH" "put $user.$date.tar" > /dev/null 2>&1
  648. else
  649. cd $tmpdir
  650. tar -cf $BACKUP/$user.$date.tar .
  651. cd $BACKUP/
  652. sftpc "cd $BPATH" "put $user.$date.tar" > /dev/null 2>&1
  653. rm -f $user.$date.tar
  654. fi
  655. }
  656. google_backup() {
  657. # Defining google settings
  658. source $VESTA/conf/google.backup.conf
  659. gsutil="$VESTA/3rdparty/gsutil/gsutil"
  660. export BOTO_CONFIG="$VESTA/conf/.google.backup.boto"
  661. # Debug info
  662. echo -e "$(date "+%F %T") Remote: gs://$BUCKET/$BPATH/$user.$date.tar"
  663. # Checking retention
  664. backup_list=$(${gsutil} ls gs://$BUCKET/$BPATH/$user.* 2>/dev/null)
  665. backups_count=$(echo "$backup_list" |wc -l)
  666. if [ "$backups_count" -ge "$BACKUPS" ]; then
  667. backups_rm_number=$((backups_count - BACKUPS + 1))
  668. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  669. echo -e "$(date "+%F %T") Roated gcp backup: $backup"
  670. $gsutil rm $backup > /dev/null 2>&1
  671. done
  672. fi
  673. # Uploading backup archive
  674. echo -e "$(date "+%F %T") Uploading $user.$date.tar ..."
  675. if [ "$localbackup" = 'yes' ]; then
  676. cd $BACKUP
  677. ${gsutil} cp $user.$date.tar gs://$BUCKET/$BPATH/ > /dev/null 2>&1
  678. else
  679. cd $tmpdir
  680. tar -cf $BACKUP/$user.$date.tar .
  681. cd $BACKUP/
  682. ${gsutil} cp $user.$date.tar gs://$BUCKET/$BPATH/ > /dev/null 2>&1
  683. rc=$?
  684. rm -f $user.$date.tar
  685. if [ "$rc" -ne 0 ]; then
  686. check_result "$E_CONNECT" "gsutil failed to upload $user.$date.tar"
  687. fi
  688. fi
  689. }
  690. echo -e "\n-- SUMMARY --" |tee -a $BACKUP/$user.log
  691. # Switching on backup system types
  692. for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\\n}"); do
  693. case $backup_type in
  694. local) local_backup ;;
  695. ftp) ftp_backup ;;
  696. sftp) sftp_backup ;;
  697. google) google_backup ;;
  698. esac
  699. done
  700. # Removing tmpdir
  701. rm -rf $tmpdir
  702. # Calculation run time
  703. run_time=$((end_time - start_time))
  704. run_time=$((run_time / 60))
  705. current_time=$(date "+%T")
  706. if [ "$run_time" -lt 1 ]; then
  707. run_time=1
  708. fi
  709. min=minutes
  710. if [ "$run_time" -eq 1 ]; then
  711. min=minute
  712. fi
  713. echo "$(date "+%F %T") Size: $size MB" |tee -a $BACKUP/$user.log
  714. echo "$(date "+%F %T") Runtime: $run_time $min" |tee -a $BACKUP/$user.log
  715. #----------------------------------------------------------#
  716. # Vesta #
  717. #----------------------------------------------------------#
  718. # Removing duplicate
  719. touch $USER_DATA/backup.conf
  720. sed -i "/$user.$date.tar/d" $USER_DATA/backup.conf
  721. # Registering new backup
  722. backup_str="BACKUP='$user.$date.tar'"
  723. backup_str="$backup_str TYPE='$BACKUP_SYSTEM' SIZE='$size'"
  724. backup_str="$backup_str WEB='${web_list// /,}'"
  725. backup_str="$backup_str DNS='${dns_list// /,}'"
  726. backup_str="$backup_str MAIL='${mail_list// /,}'"
  727. backup_str="$backup_str DB='${db_list// /,}'"
  728. backup_str="$backup_str CRON='$cron_list'"
  729. backup_str="$backup_str UDIR='${udir_list// /,}'"
  730. backup_str="$backup_str RUNTIME='$run_time' TIME='$time' DATE='$date'"
  731. echo "$backup_str" >> $USER_DATA/backup.conf
  732. # Removing old backups
  733. tail -n $BACKUPS $USER_DATA/backup.conf > $USER_DATA/backup.conf_
  734. mv -f $USER_DATA/backup.conf_ $USER_DATA/backup.conf
  735. chmod 660 $USER_DATA/backup.conf
  736. # Deleting task from queue
  737. sed -i "/v-backup-user $user /d" $VESTA/data/queue/backup.pipe
  738. U_BACKUPS=$(grep BACKUP $USER_DATA/backup.conf |wc -l)
  739. update_user_value "$user" '$U_BACKUPS' "$U_BACKUPS"
  740. # Send notification
  741. if [ -e "$BACKUP/$user.log" ]; then
  742. cd $BACKUP
  743. subj="$user → backup has been completed"
  744. email=$(get_user_value '$CONTACT')
  745. cat $BACKUP/$user.log |$SENDMAIL -s "$subj" $email $notify
  746. rm $BACKUP/$user.log
  747. fi
  748. # Logging
  749. log_event "$OK" "$ARGUMENTS"
  750. exit