v_backup_sys_user 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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.sh
  12. source $V_FUNC/domain_func.sh
  13. source $V_FUNC/db_func.sh
  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=$((i + 1))
  43. done
  44. #----------------------------------------------------------#
  45. # Action #
  46. #----------------------------------------------------------#
  47. # Creating temporary random directory
  48. tmpdir=$(mktemp -p $V_TMP -d)
  49. # Prinitng status
  50. if [ -z "$output" ]; then
  51. echo "$(date +%m-%d-%y" "%H:%m:%S) System backup for user $user"
  52. echo "TMPDIR is $tmpdir"
  53. echo
  54. fi
  55. # Addding vesta version
  56. echo "1.0" >$tmpdir/backup_version
  57. echo "$VERSION" > $tmpdir/vesta_version
  58. # Checking excludes
  59. OLD_IFS="$IFS"
  60. IFS=$'\n'
  61. if [ -e "$V_USERS/$user/backup.excludes" ]; then
  62. if [ -z "$output" ]; then
  63. echo "-- Excludes --"
  64. fi
  65. for exclude in $(cat $V_USERS/$user/backup.excludes); do
  66. if [ -z "$output" ]; then
  67. echo -e "\t $exclude"
  68. fi
  69. # Indirect variable references (a bit of black magic)
  70. eval ${exclude%%=*}=${exclude#*=}
  71. done
  72. if [ -z "$output" ]; then
  73. echo
  74. fi
  75. fi
  76. IFS="$OLD_IFS"
  77. # WEB domains
  78. if [ ! -z "$WEB_SYSTEM" ] && [ "$WEB_SYSTEM" != 'no' ] && [ "$WEB" != '*' ]
  79. then
  80. if [ -z "$output" ]; then
  81. echo "-- WEB --"
  82. fi
  83. mkdir $tmpdir/web/
  84. # Parsing unsuspeneded domains
  85. conf="$V_USERS/$user/web.conf"
  86. field='$DOMAIN'
  87. search_string='DOMAIN='
  88. domains=$(dom_clear_search)
  89. domain_list=''
  90. # Cleaning excludes
  91. for domain in $domains; do
  92. check_exl=$(echo "$WEB"|grep -w $domain)
  93. if [ -z "$check_exl" ]; then
  94. web_list="$web_list $domain"
  95. fi
  96. done
  97. for domain in $web_list; do
  98. if [ -z "$output" ]; then
  99. echo -e "\t$(date +%H:%m:%S) $domain"
  100. fi
  101. # Defining domain variables
  102. domain_idn=$(idn -t --quiet -a "$domain")
  103. tpl_name=$(get_web_domain_value '$TPL')
  104. ssl_cert=$(get_web_domain_value '$SSL_CERT')
  105. nginx=$(get_web_domain_value '$NGINX')
  106. # Building directory tree
  107. mkdir -p $tmpdir/web/$domain/conf $tmpdir/web/$domain/cert
  108. # Packing data folders
  109. cd $V_HOME/$user/web/$domain
  110. tar -cf $tmpdir/web/$domain/$domain.tar \
  111. public_html public_shtml private document_errors cgi-bin stats
  112. # Creating web.config
  113. cd $tmpdir/web/$domain/
  114. conf="$V_USERS/$user/web.conf"
  115. grep "DOMAIN='$domain'" $conf > conf/web.conf
  116. # Apache config
  117. if [ "$WEB_SYSTEM" = 'apache' ]; then
  118. # Parsing httpd.conf
  119. tpl_file="$V_WEBTPL/apache_$tpl_name.tpl"
  120. conf="$V_HOME/$user/conf/httpd.conf"
  121. get_web_config_brds
  122. sed -n "$top_line,$bottom_line p" $conf > conf/httpd.conf
  123. # SSL check
  124. if [ ! -z "$ssl_cert" ]; then
  125. tpl_file="$V_WEBTPL/apache_$tpl_name.stpl"
  126. conf="$V_HOME/$user/conf/shttpd.conf"
  127. get_web_config_brds
  128. sed -n "$top_line,$bottom_line p" $conf > conf/shttpd.conf
  129. fi
  130. fi
  131. # Nginx config
  132. if [ ! -z "$nginx" ] ; then
  133. tpl_file="$V_WEBTPL/ngingx_vhost_$nginx.tpl"
  134. conf="$V_HOME/$user/conf/nginx.conf"
  135. get_web_config_brds
  136. sed -n "$top_line,$bottom_line p" $conf > conf/nginx.conf
  137. # SSL check
  138. if [ ! -z "$ssl_cert" ] ; then
  139. tpl_file="$V_WEBTPL/ngingx_vhost_$nginx.stpl"
  140. conf="$V_HOME/$user/conf/snginx.conf"
  141. get_web_config_brds
  142. sed -n "$top_line,$bottom_line p" $conf > conf/snginx.conf
  143. fi
  144. fi
  145. # Suplemental configs
  146. for sconfig in $(ls $V_HOME/$user/conf/|grep ".$domain.conf"); do
  147. cp $V_HOME/$user/conf/$sconfig conf/
  148. done
  149. # SSL Certificates
  150. if [ ! -z "$ssl_cert" ] ; then
  151. cp $V_HOME/$user/conf/$ssl_cert.* cert/
  152. fi
  153. tar -rf $tmpdir/web/$domain/$domain.tar conf cert
  154. mv $tmpdir/web/$domain/$domain.tar $tmpdir/web/
  155. rm -rf $tmpdir/web/$domain
  156. if [ ! -z "$V_BACKUP_GZIP" ]; then
  157. gzip -$V_BACKUP_GZIP $tmpdir/web/$domain.tar
  158. fi
  159. done
  160. if [ -z "$output" ]; then
  161. echo
  162. fi
  163. fi
  164. # DNS domains
  165. if [ ! -z "$DNS_SYSTEM" ] && [ "$DNS_SYSTEM" != 'no' ] && [ "$DNS" != '*' ]
  166. then
  167. if [ -z "$output" ]; then
  168. echo "-- DNS --"
  169. fi
  170. mkdir $tmpdir/dns/
  171. # Parsing unsuspeneded domains
  172. conf="$V_USERS/$user/dns.conf"
  173. field='$DOMAIN'
  174. search_string='DOMAIN='
  175. domains=$(dom_clear_search)
  176. # Cleaning excludes
  177. for domain in $domains; do
  178. check_exl=$(echo "$DNS"|grep -w $domain)
  179. if [ -z "$check_exl" ]; then
  180. dns_list="$dns_list $domain"
  181. fi
  182. done
  183. for domain in $dns_list; do
  184. if [ -z "$output" ]; then
  185. echo -e "\t$(date +%H:%m:%S) $domain"
  186. fi
  187. # Building directory tree
  188. mkdir $tmpdir/dns/$domain
  189. # Creating dns_domains config
  190. cd $tmpdir/dns/$domain/
  191. conf="$V_USERS/$user/dns.conf"
  192. grep "DOMAIN='$domain'" $conf > dns.conf
  193. # Backingup dns recods
  194. cp $V_USERS/$user/zones/$domain $domain
  195. cp /etc/namedb/$domain.db $domain.db
  196. done
  197. if [ -z "$output" ]; then
  198. echo
  199. fi
  200. fi
  201. # Mail domains
  202. # TBD
  203. # DatbaBases
  204. if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ] && [ "$DB" != '*' ]; then
  205. if [ -z "$output" ]; then
  206. echo "-- DB --"
  207. fi
  208. mkdir $tmpdir/db/
  209. # Parsing unsuspeneded domains
  210. conf="$V_USERS/$user/db.conf"
  211. field='$DB'
  212. search_string='DB='
  213. dbs=$(dom_clear_search)
  214. # Cleaning excludes
  215. for database in $dbs; do
  216. check_exl=$(echo "$DB"|grep -w $database)
  217. if [ -z "$check_exl" ]; then
  218. db_list="$db_list $database"
  219. fi
  220. done
  221. for database in $db_list; do
  222. type=$(get_db_value '$TYPE')
  223. host=$(get_db_value '$HOST')
  224. db_user=$(get_db_value '$USER')
  225. dump="$tmpdir/db/$database.$type.sql"
  226. grants="$tmpdir/db/$database.$type.$db_user"
  227. if [ -z "$output" ]; then
  228. echo -e "\t$(date +%H:%m:%S) $database $type"
  229. fi
  230. case $type in
  231. mysql) dump_db_mysql ;;
  232. pgsql) dump_db_pgsql ;;
  233. esac
  234. if [ ! -z "$V_BACKUP_GZIP" ]; then
  235. gzip -$V_BACKUP_GZIP $dump
  236. fi
  237. done
  238. if [ -z "$output" ]; then
  239. echo
  240. fi
  241. fi
  242. # Cron jobs
  243. if [ ! -z "$CRON_SYSTEM" ] && [ "$CRON_SYSTEM" != 'no' ] && [ "$CRON" != '*' ]
  244. then
  245. if [ -z "$output" ]; then
  246. echo "-- CRON --"
  247. fi
  248. mkdir $tmpdir/cron/
  249. # Backingup cron recods
  250. if [ -z "$output" ]; then
  251. echo -e "\t$(date +%H:%m:%S) cron.conf"
  252. fi
  253. cp $V_USERS/$user/cron.conf $tmpdir/cron/
  254. if [ -z "$output" ]; then
  255. echo -e "\t$(date +%H:%m:%S) system cron"
  256. fi
  257. if [ -e "/var/spool/cron/$user" ]; then
  258. cp /var/spool/cron/$user $tmpdir/cron/
  259. fi
  260. if [ -z "$output" ]; then
  261. echo
  262. fi
  263. fi
  264. # SSL CERTIFICATES
  265. if [ ! -z "$WEB_SSL" ] && [ "$WEB_SSL" != 'no' ] && [ "$SSL" != '*' ]; then
  266. if [ -z "$output" ]; then
  267. echo "-- CERTIFICATES --"
  268. fi
  269. mkdir $tmpdir/cert
  270. # Backingup ssl certificates
  271. for cert in $(ls $V_USERS/$user/cert/); do
  272. if [ -z "$output" ]; then
  273. echo -e "\t$(date +%H:%m:%S) $cert"
  274. fi
  275. cp $V_USERS/$user/cert/$cert $tmpdir/cert/
  276. done
  277. if [ -z "$output" ]; then
  278. echo
  279. fi
  280. fi
  281. # Vesta
  282. if [ -z "$output" ]; then
  283. echo "-- VESTA --"
  284. fi
  285. mkdir $tmpdir/vesta
  286. # Backingup vesta configs
  287. if [ -z "$output" ]; then
  288. echo -e "\t$(date +%H:%m:%S) user.conf"
  289. fi
  290. cp -r $V_USERS/$user/user.conf $tmpdir/vesta/
  291. if [ -e "$V_USERS/$user/billing.log" ]; then
  292. if [ -z "$output" ]; then
  293. echo -e "\t$(date +%H:%m:%S) billing.log"
  294. fi
  295. cp -r $V_USERS/$user/billing.log $tmpdir/vesta/
  296. fi
  297. if [ -e "$V_USERS/$user/child.conf" ]; then
  298. if [ -z "$output" ]; then
  299. echo -e "\t$(date +%H:%m:%S) child.conf"
  300. fi
  301. cp -r $V_USERS/$user/child.conf $tmpdir/vesta/
  302. fi
  303. if [ -e "$V_USERS/$user/history.log" ]; then
  304. if [ -z "$output" ]; then
  305. echo -e "\t$(date +%H:%m:%S) history.log"
  306. fi
  307. cp -r $V_USERS/$user/history.log $tmpdir/vesta/
  308. fi
  309. if [ -e "$V_USERS/$user/backup.excludes" ]; then
  310. if [ -z "$output" ]; then
  311. echo -e "\t$(date +%H:%m:%S) backup.excludes"
  312. fi
  313. cp -r $V_USERS/$user/backup.excludes $tmpdir/vesta/
  314. fi
  315. if [ -z "$output" ]; then
  316. echo
  317. fi
  318. # Defining local storage function
  319. local_backup(){
  320. if [ -z "$output" ]; then
  321. echo "-- STORAGE --"
  322. echo -e "\t$(date +%H:%m:%S) ARCHIVE $V_BACKUP/$user.$V_DATE.tar"
  323. fi
  324. # Checking retention
  325. archives=$(ls -lrt $V_BACKUP/ |awk '{print $9}' |grep "^$user\.")
  326. archives_q=$(echo "$archives" |wc -l)
  327. if [ "$archives_q" -ge "$backups" ]; then
  328. archives_r=$((archives_q - backups))
  329. for archive in $(echo "$archives" | head -n $archives_r); do
  330. # Removing old archives
  331. if [ -z "$output" ]; then
  332. echo -e "\tRemoving old $archive"
  333. fi
  334. rm -f $V_BACKUP/$archive
  335. done
  336. fi
  337. # Checking disk space
  338. disk_usage=$(df $V_BACKUP | awk '{print $5}'|tail -n1|cut -f 1 -d '%')
  339. if [ "$disk_usage" -ge "$V_BACKUP_DISK_LIMIT" ]; then
  340. echo "Error: Not enough disk space"
  341. log_event 'debug' "$E_DISK_SPACE $V_EVENT"
  342. exit $E_DISK_SPACE
  343. fi
  344. # Creating final tarball
  345. cd $tmpdir
  346. tar -cf $V_BACKUP/$user.$V_DATE.tar .
  347. chmod 640 $V_BACKUP/$user.$V_DATE.tar
  348. chown root:$user $V_BACKUP/$user.$V_DATE.tar
  349. localbackup='yes'
  350. if [ -z "$output" ]; then
  351. echo
  352. fi
  353. }
  354. # Defining ftp command function
  355. ftpc() {
  356. ftp -n $HOST $PORT <<EOF
  357. quote USER $USER
  358. quote PASS $PASSWORD
  359. binary
  360. cd $BPATH
  361. $1
  362. quit
  363. EOF
  364. }
  365. # Defining ftp storage function
  366. ftp_backup(){
  367. if [ -z "$output" ]; then
  368. echo "-- FTP --"
  369. fi
  370. # Checking config
  371. if [ -e "$V_CONF/backup.conf" ]; then
  372. ftphost_str=$(grep "TYPE='FTP'" $V_CONF/backup.conf |head -n 1)
  373. fi
  374. # Parsing config values
  375. if [ ! -z "$ftphost_str" ]; then
  376. for key in $ftphost_str; do
  377. eval ${key%%=*}=${key#*=}
  378. done
  379. else
  380. echo "Error: Parsing error"
  381. log_event 'debug' "$E_PARSE_ERROR $V_EVENT"
  382. exit $E_PARSE_ERROR
  383. fi
  384. # Debug info
  385. if [ -z "$output" ]; then
  386. echo -e "\t$(date +%H:%m:%S) ftp://$USER@$HOST$BPATH/$user.$V_DATE.tar"
  387. fi
  388. # Checking ftp permission
  389. ftmpdir=$(mktemp -u -p $BPATH)
  390. command="mkdir $ftmpdir
  391. ls $ftmpdir
  392. rm $ftmpdir"
  393. if [ ! -z "$(ftpc "$command")" ] ; then
  394. echo "Error: FTP error"
  395. log_event 'debug' "$E_FTP_ERROR $V_EVENT"
  396. exit $E_FTP_ERROR
  397. fi
  398. # Checking retention
  399. archives=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.")
  400. archives_q=$(echo "$archives" | wc -l)
  401. if [ "$archives_q" -ge "$backups" ]; then
  402. # Removing old backups
  403. archives_r=$((archives_q - backups))
  404. for archive in $(echo "$archives" | tail -n $archives_r); do
  405. if [ -z "$output" ]; then
  406. echo -e "\tRemoving old $archive"
  407. fi
  408. ftpc "delete $archive"
  409. done
  410. fi
  411. # Uploading backup archive
  412. if [ "$localbackup" = 'yes' ]; then
  413. cd $V_BACKUP
  414. ftpc "put $user.$V_DATE.tar"
  415. else
  416. cd $tmpdir
  417. tar -cf $V_TMP/$user.$V_DATE.tar .
  418. cd $V_TMP/
  419. ftpc "put $user.$V_DATE.tar"
  420. rm -f $user.$V_DATE.tar
  421. fi
  422. if [ -z "$output" ]; then
  423. echo
  424. fi
  425. }
  426. # Switching on backup system types
  427. for backup_type in $(echo -e "${BACKUP_SYSTEM//,/\n}"); do
  428. case $backup_type in
  429. local) local_backup ;;
  430. ftp) ftp_backup ;;
  431. esac
  432. done
  433. # Removing tmpdir
  434. cd /
  435. rm -rf $tmpdir
  436. if [ -z "$output" ]; then
  437. echo "$(date +'%m-%d-%y %H:%m:%S')"
  438. echo
  439. echo
  440. fi
  441. #----------------------------------------------------------#
  442. # Vesta #
  443. #----------------------------------------------------------#
  444. # Logging
  445. log_event 'system' "$V_EVENT"
  446. exit $OK