v-backup-user 29 KB

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