v-backup-user 29 KB

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