v-backup-user 29 KB

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