v_backup_sys_user 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. #!/bin/bash
  2. # info: backup system user with all its objects
  3. #----------------------------------------------------------#
  4. # Variable&Function #
  5. #----------------------------------------------------------#
  6. # Argument defenition
  7. user=$1
  8. output=$2
  9. # Importing variables
  10. source $VESTA/conf/vars.conf
  11. source $V_FUNC/shared.func
  12. source $V_FUNC/domain.func
  13. source $V_FUNC/db.func
  14. source $V_CONF/vesta.conf
  15. #----------------------------------------------------------#
  16. # Verifications #
  17. #----------------------------------------------------------#
  18. # Checking arg number
  19. check_args '1' "$#" 'user [output]'
  20. # Checking argument format
  21. format_validation 'user'
  22. # Checking backup system is enabled
  23. is_system_enabled 'backup'
  24. # Checking user
  25. is_user_valid
  26. # Checking user backups
  27. is_backup_enabled
  28. # Checking load averages
  29. la=$(cat /proc/loadavg |cut -f 1 -d ' '|cut -f 1 -d '.')
  30. i=0
  31. while [ "$la" -ge "$V_BACKUP_LA_LIMIT" ]; do
  32. if [ -z "$output" ]; then
  33. echo "$(date +%m-%d-%y" "%H:%M:%S) Load Average $la"
  34. echo
  35. fi
  36. sleep 60
  37. if [ "$i" -ge "15" ]; then
  38. echo "Error: LA is too high"
  39. log_event 'debug' "$E_LOAD_AVERAGE $V_EVENT"
  40. exit $E_LOAD_AVERAGE
  41. fi
  42. (( ++i))
  43. done
  44. #----------------------------------------------------------#
  45. # Action #
  46. #----------------------------------------------------------#
  47. # Get current time
  48. start_time=$(date '+%s')
  49. sleep 131
  50. # Creating temporary random directory
  51. tmpdir=$(mktemp -p $V_TMP -d)
  52. # Prinitng status
  53. if [ -z "$output" ]; then
  54. echo "$(date +%m-%d-%y" "%H:%M:%S) System backup for user $user"
  55. echo "TMPDIR is $tmpdir"
  56. echo
  57. fi
  58. # Addding backup and vesta version
  59. echo "1.0" > $tmpdir/backup_version
  60. echo "$VERSION" > $tmpdir/vesta_version
  61. # Vesta
  62. if [ -z "$output" ]; then
  63. echo "-- VESTA --"
  64. fi
  65. mkdir $tmpdir/vesta
  66. # Backingup vesta configs
  67. if [ -z "$output" ]; then
  68. echo -e "\t$(date +%H:%M:%S) user.conf"
  69. fi
  70. cp -r $V_USERS/$user/user.conf $tmpdir/vesta/
  71. if [ -e "$V_USERS/$user/billing.log" ]; then
  72. if [ -z "$output" ]; then
  73. echo -e "\t$(date +%H:%M:%S) billing.log"
  74. fi
  75. cp -r $V_USERS/$user/billing.log $tmpdir/vesta/
  76. fi
  77. if [ -e "$V_USERS/$user/history.log" ]; then
  78. if [ -z "$output" ]; then
  79. echo -e "\t$(date +%H:%M:%S) history.log"
  80. fi
  81. cp -r $V_USERS/$user/history.log $tmpdir/vesta/
  82. fi
  83. if [ -e "$V_USERS/$user/backup.excludes" ]; then
  84. if [ -z "$output" ]; then
  85. echo -e "\t$(date +%H:%M:%S) backup.excludes"
  86. fi
  87. cp -r $V_USERS/$user/backup.excludes $tmpdir/vesta/
  88. fi
  89. if [ -e "$V_USERS/$user/cron.conf" ]; then
  90. if [ -z "$output" ]; then
  91. echo -e "\t$(date +%H:%M:%S) cron.conf"
  92. fi
  93. cp -r $V_USERS/$user/cron.conf $tmpdir/vesta/
  94. fi
  95. if [ -e "$V_USERS/$user/db.conf" ]; then
  96. if [ -z "$output" ]; then
  97. echo -e "\t$(date +%H:%M:%S) db.conf"
  98. fi
  99. cp -r $V_USERS/$user/db.conf $tmpdir/vesta/
  100. fi
  101. if [ -e "$V_USERS/$user/dns.conf" ]; then
  102. if [ -z "$output" ]; then
  103. echo -e "\t$(date +%H:%M:%S) dns.conf"
  104. fi
  105. cp -r $V_USERS/$user/dns.conf $tmpdir/vesta/
  106. fi
  107. if [ -e "$V_USERS/$user/mail.conf" ]; then
  108. if [ -z "$output" ]; then
  109. echo -e "\t$(date +%H:%M:%S) mail.conf"
  110. fi
  111. cp -r $V_USERS/$user/mail.conf $tmpdir/vesta/
  112. fi
  113. if [ -e "$V_USERS/$user/web.conf" ]; then
  114. if [ -z "$output" ]; then
  115. echo -e "\t$(date +%H:%M:%S) web.conf"
  116. fi
  117. cp -r $V_USERS/$user/web.conf $tmpdir/vesta/
  118. fi
  119. if [ -z "$output" ]; then
  120. echo
  121. fi
  122. # Checking excludes
  123. OLD_IFS="$IFS"
  124. IFS=$'\n'
  125. if [ -e "$V_USERS/$user/backup.excludes" ]; then
  126. if [ -z "$output" ]; then
  127. echo "-- Excludes --"
  128. fi
  129. for exclude in $(cat $V_USERS/$user/backup.excludes); do
  130. if [ -z "$output" ]; then
  131. echo -e "\t$exclude"
  132. fi
  133. # Indirect variable references (a bit of black magic)
  134. eval ${exclude%%=*}=${exclude#*=}
  135. done
  136. if [ -z "$output" ]; then
  137. echo
  138. fi
  139. fi
  140. IFS="$OLD_IFS"
  141. # WEB domains
  142. if [ ! -z "$WEB_SYSTEM" ] && [ "$WEB_SYSTEM" != 'no' ] && [ "$WEB" != '*' ]
  143. then
  144. if [ -z "$output" ]; then
  145. echo "-- WEB --"
  146. fi
  147. mkdir $tmpdir/web/
  148. # Parsing unsuspeneded domains
  149. conf="$V_USERS/$user/web.conf"
  150. field='$DOMAIN'
  151. search_string='DOMAIN='
  152. domains=$(dom_clear_search)
  153. domain_list=''
  154. # Cleaning excludes
  155. for domain in $domains; do
  156. check_exl=$(echo "$WEB"|grep -w $domain)
  157. if [ -z "$check_exl" ]; then
  158. web_list="$web_list $domain"
  159. fi
  160. done
  161. web_list=$(echo "$web_list" | sed -e "s/ */\ /g" -e "s/^ //")
  162. for domain in $web_list; do
  163. if [ -z "$output" ]; then
  164. echo -e "\t$(date +%H:%M:%S) $domain"
  165. fi
  166. # Defining domain variables
  167. domain_idn=$(idn -t --quiet -a "$domain")
  168. tpl_name=$(get_web_domain_value '$TPL')
  169. ssl_cert=$(get_web_domain_value '$SSL_CERT')
  170. nginx=$(get_web_domain_value '$NGINX')
  171. # Building directory tree
  172. mkdir -p $tmpdir/web/$domain/conf $tmpdir/web/$domain/cert
  173. # Packing data folders
  174. cd $V_HOME/$user/web/$domain
  175. tar -cf $tmpdir/web/$domain/$domain.tar \
  176. public_html public_shtml private document_errors cgi-bin stats
  177. # Creating web.config
  178. cd $tmpdir/web/$domain/
  179. conf="$V_USERS/$user/web.conf"
  180. grep "DOMAIN='$domain'" $conf > conf/web.conf
  181. # Apache config
  182. if [ "$WEB_SYSTEM" = 'apache' ]; then
  183. # Parsing httpd.conf
  184. tpl_file="$V_WEBTPL/apache_$tpl_name.tpl"
  185. conf="$V_HOME/$user/conf/httpd.conf"
  186. get_web_config_brds
  187. sed -n "$top_line,$bottom_line p" $conf > conf/httpd.conf
  188. # SSL check
  189. if [ ! -z "$ssl_cert" ]; then
  190. tpl_file="$V_WEBTPL/apache_$tpl_name.stpl"
  191. conf="$V_HOME/$user/conf/shttpd.conf"
  192. get_web_config_brds
  193. sed -n "$top_line,$bottom_line p" $conf > conf/shttpd.conf
  194. fi
  195. fi
  196. # Nginx config
  197. if [ ! -z "$nginx" ] ; then
  198. tpl_file="$V_WEBTPL/ngingx_vhost_$nginx.tpl"
  199. conf="$V_HOME/$user/conf/nginx.conf"
  200. get_web_config_brds
  201. sed -n "$top_line,$bottom_line p" $conf > conf/nginx.conf
  202. # SSL check
  203. if [ ! -z "$ssl_cert" ] ; then
  204. tpl_file="$V_WEBTPL/ngingx_vhost_$nginx.stpl"
  205. conf="$V_HOME/$user/conf/snginx.conf"
  206. get_web_config_brds
  207. sed -n "$top_line,$bottom_line p" $conf > conf/snginx.conf
  208. fi
  209. fi
  210. # Suplemental configs
  211. for sconfig in $(ls $V_HOME/$user/conf/|grep ".$domain.conf"); do
  212. cp $V_HOME/$user/conf/$sconfig conf/
  213. done
  214. # SSL Certificates
  215. if [ ! -z "$ssl_cert" ] ; then
  216. cp $V_HOME/$user/conf/$ssl_cert.* cert/
  217. fi
  218. tar -rf $tmpdir/web/$domain/$domain.tar conf cert
  219. mv $tmpdir/web/$domain/$domain.tar $tmpdir/web/
  220. rm -rf $tmpdir/web/$domain
  221. if [ ! -z "$V_BACKUP_GZIP" ]; then
  222. gzip -$V_BACKUP_GZIP $tmpdir/web/$domain.tar
  223. fi
  224. done
  225. if [ -z "$output" ]; then
  226. echo
  227. fi
  228. fi
  229. # DNS domains
  230. if [ ! -z "$DNS_SYSTEM" ] && [ "$DNS_SYSTEM" != 'no' ] && [ "$DNS" != '*' ]
  231. then
  232. if [ -z "$output" ]; then
  233. echo "-- DNS --"
  234. fi
  235. mkdir $tmpdir/dns/
  236. # Parsing unsuspeneded domains
  237. conf="$V_USERS/$user/dns.conf"
  238. field='$DOMAIN'
  239. search_string='DOMAIN='
  240. domains=$(dom_clear_search)
  241. # Cleaning excludes
  242. for domain in $domains; do
  243. check_exl=$(echo "$DNS"|grep -w $domain)
  244. if [ -z "$check_exl" ]; then
  245. dns_list="$dns_list $domain"
  246. fi
  247. done
  248. dns_list=$(echo "$dns_list" | sed -e "s/ */\ /g" -e "s/^ //")
  249. for domain in $dns_list; do
  250. if [ -z "$output" ]; then
  251. echo -e "\t$(date +%H:%M:%S) $domain"
  252. fi
  253. # Building directory tree
  254. mkdir $tmpdir/dns/$domain
  255. # Creating dns_domains config
  256. cd $tmpdir/dns/$domain/
  257. conf="$V_USERS/$user/dns.conf"
  258. grep "DOMAIN='$domain'" $conf > dns.conf
  259. # Backingup dns recods
  260. cp $V_USERS/$user/zones/$domain $domain
  261. cp /etc/namedb/$domain.db $domain.db
  262. done
  263. if [ -z "$output" ]; then
  264. echo
  265. fi
  266. fi
  267. # Mail domains
  268. # TBD
  269. # DatbaBases
  270. if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ] && [ "$DB" != '*' ]; then
  271. if [ -z "$output" ]; then
  272. echo "-- DB --"
  273. fi
  274. mkdir $tmpdir/db/
  275. # Parsing unsuspeneded domains
  276. conf="$V_USERS/$user/db.conf"
  277. field='$DB'
  278. search_string='DB='
  279. dbs=$(dom_clear_search)
  280. # Cleaning excludes
  281. for database in $dbs; do
  282. check_exl=$(echo "$DB"|grep -w $database)
  283. if [ -z "$check_exl" ]; then
  284. db_list="$db_list $database"
  285. fi
  286. done
  287. db_list=$(echo "$db_list" | sed -e "s/ */\ /g" -e "s/^ //")
  288. for database in $db_list; do
  289. type=$(get_db_value '$TYPE')
  290. host=$(get_db_value '$HOST')
  291. db_user=$(get_db_value '$USER')
  292. dump="$tmpdir/db/$database.$type.sql"
  293. grants="$tmpdir/db/$database.$type.$db_user"
  294. if [ -z "$output" ]; then
  295. echo -e "\t$(date +%H:%M:%S) $database $type"
  296. fi
  297. case $type in
  298. mysql) dump_db_mysql ;;
  299. pgsql) dump_db_pgsql ;;
  300. esac
  301. if [ ! -z "$V_BACKUP_GZIP" ]; then
  302. gzip -$V_BACKUP_GZIP $dump
  303. fi
  304. done
  305. if [ -z "$output" ]; then
  306. echo
  307. fi
  308. fi
  309. # Cron jobs
  310. if [ ! -z "$CRON_SYSTEM" ] && [ "$CRON_SYSTEM" != 'no' ] && [ "$CRON" != '*' ]
  311. then
  312. if [ -z "$output" ]; then
  313. echo "-- CRON --"
  314. fi
  315. mkdir $tmpdir/cron/
  316. # Backingup cron recods
  317. if [ -z "$output" ]; then
  318. echo -e "\t$(date +%H:%M:%S) cron.conf"
  319. fi
  320. cp $V_USERS/$user/cron.conf $tmpdir/cron/
  321. if [ -z "$output" ]; then
  322. echo -e "\t$(date +%H:%M:%S) system cron"
  323. fi
  324. if [ -e "/var/spool/cron/$user" ]; then
  325. cron_list='yes'
  326. cp /var/spool/cron/$user $tmpdir/cron/
  327. fi
  328. if [ -z "$output" ]; then
  329. echo
  330. fi
  331. fi
  332. # SSL CERTIFICATES
  333. if [ ! -z "$WEB_SSL" ] && [ "$WEB_SSL" != 'no' ] && [ "$SSL" != '*' ]; then
  334. if [ -z "$output" ]; then
  335. echo "-- CERTIFICATES --"
  336. fi
  337. mkdir $tmpdir/cert
  338. # Backingup ssl certificates
  339. cert_list=$(ls $V_USERS/$user/cert/ | grep ".crt" |\
  340. sed -e "s/\.crt$//" |\
  341. tr '\n' ' ' |\
  342. sed -e 's/ $//' )
  343. for cert in $cert_list; do
  344. if [ -z "$output" ]; then
  345. echo -e "\t$(date +%H:%M:%S) $cert"
  346. fi
  347. cp $V_USERS/$user/cert/$cert.* $tmpdir/cert/
  348. done
  349. if [ -z "$output" ]; then
  350. echo
  351. fi
  352. fi
  353. # Get backup size
  354. size="$(du -shm $tmpdir | cut -f 1)"
  355. # Get current time
  356. end_time=$(date '+%s')
  357. # Defining local storage function
  358. local_backup(){
  359. if [ -z "$output" ]; then
  360. echo "-- STORAGE --"
  361. echo -e "\t$(date +%H:%M:%S) ARCHIVE $V_BACKUP/$user.$V_DATE.tar"
  362. fi
  363. # Removing dublicate for this day
  364. deprecated="$V_DATE"
  365. if [ -e "$V_BACKUP/$user.$V_DATE.tar" ]; then
  366. echo -e "\tDeleting old backup for $V_DATE"
  367. rm -f $V_BACKUP/$user.$V_DATE.tar
  368. fi
  369. # Checking retention
  370. backup_list=$(ls -lrt $V_BACKUP/ | awk '{print $9}' |grep "^$user\.")
  371. backups_count=$(echo "$backup_list" | wc -l)
  372. if [ "$BACKUPS" -le "$backups_count" ]; then
  373. backups_rm_number=$((backups_count - BACKUPS))
  374. (( ++backups_rm_number))
  375. for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
  376. # Removing old backup
  377. backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar$//")
  378. deprecated="$deprecated $backup_date"
  379. if [ -z "$output" ]; then
  380. echo -e "\tDeleteing old backup for $backup_date"
  381. fi
  382. rm -f $V_BACKUP/$backup
  383. done
  384. fi
  385. # Checking disk space
  386. disk_usage=$(df $V_BACKUP | awk '{print $5}'|tail -n1|cut -f 1 -d '%')
  387. if [ "$disk_usage" -ge "$V_BACKUP_DISK_LIMIT" ]; then
  388. echo "Error: Not enough disk space"
  389. log_event 'debug' "$E_DISK_SPACE $V_EVENT"
  390. exit $E_DISK_SPACE
  391. fi
  392. # Creating final tarball
  393. cd $tmpdir
  394. tar -cf $V_BACKUP/$user.$V_DATE.tar .
  395. chmod 640 $V_BACKUP/$user.$V_DATE.tar
  396. chown root:$user $V_BACKUP/$user.$V_DATE.tar
  397. localbackup='yes'
  398. if [ -z "$output" ]; then
  399. echo
  400. fi
  401. }
  402. # Defining ftp command function
  403. ftpc() {
  404. ftp -n $HOST $PORT <<EOF
  405. quote USER $USER
  406. quote PASS $PASSWORD
  407. binary
  408. cd $BPATH
  409. $1
  410. quit
  411. EOF
  412. }
  413. # Defining ftp storage function
  414. ftp_backup(){
  415. if [ -z "$output" ]; then
  416. echo "-- FTP --"
  417. fi
  418. # Checking config
  419. if [ -e "$V_CONF/backup.conf" ]; then
  420. ftphost_str=$(grep "TYPE='FTP'" $V_CONF/backup.conf |head -n 1)
  421. fi
  422. # Parsing config values
  423. if [ ! -z "$ftphost_str" ]; then
  424. for key in $ftphost_str; do
  425. eval ${key%%=*}=${key#*=}
  426. done
  427. else
  428. echo "Error: Parsing error"
  429. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  430. exit $E_PARSE_ERROR
  431. fi
  432. # Debug info
  433. if [ -z "$output" ]; then
  434. echo -e "\t$(date +%H:%M:%S) ftp://$USER@$HOST$BPATH/$user.$V_DATE.tar"
  435. fi
  436. # Checking ftp permission
  437. ftmpdir=$(mktemp -u -p $BPATH)
  438. command="mkdir $ftmpdir
  439. ls $ftmpdir
  440. rm $ftmpdir"
  441. if [ ! -z "$(ftpc "$command")" ] ; then
  442. echo "Error: FTP error"
  443. log_event 'debug' "$E_FTP_ERROR $V_EVENT"
  444. exit $E_FTP_ERROR
  445. fi
  446. # Checking retention
  447. backup_list=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.")
  448. backups_count=$(echo "$backup_list" | wc -l)
  449. if [ "$backups_count" -ge "$BACKUPS" ]; then
  450. # Removing old backups
  451. backups_rm_number=$((backups_count - BACKUPS))
  452. for backup in $(echo "$backup_list" | tail -n $backups_rm_number); do
  453. backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar$//")
  454. deprecated="$deprecated $backup_date"
  455. if [ -z "$output" ]; then
  456. echo -e "\tDeleteing old backup for $backup_date"
  457. fi
  458. ftpc "delete $backup"
  459. done
  460. fi
  461. # Uploading backup archive
  462. if [ "$localbackup" = 'yes' ]; then
  463. cd $V_BACKUP
  464. ftpc "put $user.$V_DATE.tar"
  465. else
  466. cd $tmpdir
  467. tar -cf $V_TMP/$user.$V_DATE.tar .
  468. cd $V_TMP/
  469. ftpc "put $user.$V_DATE.tar"
  470. rm -f $user.$V_DATE.tar
  471. fi
  472. if [ -z "$output" ]; then
  473. echo
  474. fi
  475. }
  476. # Switching on backup system types
  477. for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\n}"); do
  478. case $backup_type in
  479. local) local_backup ;;
  480. ftp) ftp_backup ;;
  481. esac
  482. done
  483. # Removing tmpdir
  484. cd /
  485. rm -rf $tmpdir
  486. # Calcilation run time
  487. run_time=$((end_time - start_time))
  488. run_time=$((run_time / 60))
  489. current_time=$(date +'%H:%M:%S')
  490. if [ "$run_time" -lt 1 ]; then
  491. run_time=1
  492. fi
  493. if [ -z "$output" ]; then
  494. min=miutes
  495. if [ "$run_time" -eq 1 ]; then
  496. min=minute
  497. fi
  498. echo "$(date +'%m-%d-%y %H:%M:%S') Backup took $run_time $min"
  499. echo
  500. echo
  501. fi
  502. #----------------------------------------------------------#
  503. # Vesta #
  504. #----------------------------------------------------------#
  505. # Deleting old backup records
  506. for backup_record in $deprecated; do
  507. sed -i "/DATE='$backup_record/d" $V_USERS/$user/backup.conf
  508. done
  509. # Concatenating string
  510. backup_str="DATE='$V_DATE' TIME='$current_time' RUNTIME='$run_time'"
  511. backup_str="$backup_str TYPE='$BACKUP_SYSTEM' SIZE='$size'"
  512. backup_str="$backup_str WEB='${web_list// /,}'"
  513. backup_str="$backup_str DNS='${dns_list// /,}'"
  514. backup_str="$backup_str MAIL='${mail_list// /,}'"
  515. backup_str="$backup_str DB='${db_list// /,}'"
  516. backup_str="$backup_str CERTIFICATES='${cert_list// /,}'"
  517. backup_str="$backup_str CRON='$cron_list'"
  518. echo "$backup_str" >> $V_USERS/$user/backup.conf
  519. # Logging
  520. log_event 'system' "$V_EVENT"
  521. exit