v_backup_sys_user 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 web system is enabled
  23. is_system_enabled 'backup'
  24. # Checking user
  25. is_user_valid
  26. #----------------------------------------------------------#
  27. # Action #
  28. #----------------------------------------------------------#
  29. # Creating temporary random directory
  30. tmpdir=$(mktemp -p $V_TMP -d)
  31. # Prinitng status
  32. if [ -z "$output" ]; then
  33. echo "$(date +%m-%d-%y" "%H:%m:%S) System backup for user $user"
  34. echo
  35. echo "VESTA VERSION $VERSION"
  36. echo "BACKUP VERSION 1.0"
  37. echo "TMPDIR is $tmpdir"
  38. echo
  39. fi
  40. # Addding vesta version
  41. echo "1.0" >$tmpdir/backup_version
  42. echo "$VERSION" > $tmpdir/vesta_version
  43. # WEB domains
  44. if [ ! -z "$WEB_SYSTEM" ] && [ "$WEB_SYSTEM" != 'no' ]; then
  45. if [ -z "$output" ]; then
  46. echo "-- WEB --"
  47. fi
  48. mkdir $tmpdir/web/
  49. # Parsing unsuspeneded domains
  50. conf="$V_USERS/$user/web.conf"
  51. field='$DOMAIN'
  52. search_string='DOMAIN='
  53. domains=$(dom_clear_search)
  54. for domain in $domains; do
  55. if [ -z "$output" ]; then
  56. echo -e "\t$(date +%H:%m:%S) $domain"
  57. fi
  58. # Defining domain variables
  59. domain_idn=$(idn -t --quiet -a "$domain")
  60. tpl_name=$(get_web_domain_value '$TPL')
  61. ssl_cert=$(get_web_domain_value '$SSL_CERT')
  62. nginx=$(get_web_domain_value '$NGINX')
  63. # Building directory tree
  64. mkdir -p $tmpdir/web/$domain/conf $tmpdir/web/$domain/cert
  65. # Packing data folders
  66. cd $V_HOME/$user/web/$domain
  67. tar -cf $tmpdir/web/$domain/$domain.tar \
  68. public_html public_shtml private document_errors cgi-bin stats
  69. # Creating web.config
  70. cd $tmpdir/web/$domain/
  71. conf="$V_USERS/$user/web.conf"
  72. grep "DOMAIN='$domain'" $conf > conf/web.conf
  73. # Apache config
  74. if [ "$WEB_SYSTEM" = 'apache' ]; then
  75. # Parsing httpd.conf
  76. tpl_file="$V_WEBTPL/apache_$tpl_name.tpl"
  77. conf="$V_HOME/$user/conf/httpd.conf"
  78. get_web_config_brds
  79. sed -n "$top_line,$bottom_line p" $conf > conf/httpd.conf
  80. # SSL check
  81. if [ ! -z "$ssl_cert" ]; then
  82. tpl_file="$V_WEBTPL/apache_$tpl_name.stpl"
  83. conf="$V_HOME/$user/conf/shttpd.conf"
  84. get_web_config_brds
  85. sed -n "$top_line,$bottom_line p" $conf > conf/shttpd.conf
  86. fi
  87. fi
  88. # Nginx config
  89. if [ ! -z "$nginx" ] ; then
  90. tpl_file="$V_WEBTPL/ngingx_vhost_$nginx.tpl"
  91. conf="$V_HOME/$user/conf/nginx.conf"
  92. get_web_config_brds
  93. sed -n "$top_line,$bottom_line p" $conf > conf/nginx.conf
  94. # SSL check
  95. if [ ! -z "$ssl_cert" ] ; then
  96. tpl_file="$V_WEBTPL/ngingx_vhost_$nginx.stpl"
  97. conf="$V_HOME/$user/conf/snginx.conf"
  98. get_web_config_brds
  99. sed -n "$top_line,$bottom_line p" $conf > conf/snginx.conf
  100. fi
  101. fi
  102. # Suplemental configs
  103. for sconfig in $(ls $V_HOME/$user/conf/|grep ".$domain.conf"); do
  104. cp $V_HOME/$user/conf/$sconfig conf/
  105. done
  106. # SSL Certificates
  107. if [ ! -z "$ssl_cert" ] ; then
  108. cp $V_HOME/$user/conf/$ssl_cert.* cert/
  109. fi
  110. tar -rf $tmpdir/web/$domain/$domain.tar conf cert
  111. mv $tmpdir/web/$domain/$domain.tar $tmpdir/web/
  112. rm -rf $tmpdir/web/$domain
  113. gzip -$V_BACKUP_GZIP $tmpdir/web/$domain.tar
  114. done
  115. if [ -z "$output" ]; then
  116. echo
  117. fi
  118. fi
  119. # DNS domains
  120. if [ ! -z "$DNS_SYSTEM" ] && [ "$DNS_SYSTEM" != 'no' ]; then
  121. if [ -z "$output" ]; then
  122. echo "-- DNS --"
  123. fi
  124. mkdir $tmpdir/dns/
  125. # Parsing unsuspeneded domains
  126. conf="$V_USERS/$user/dns.conf"
  127. field='$DOMAIN'
  128. search_string='DOMAIN='
  129. domains=$(dom_clear_search)
  130. for domain in $domains; do
  131. if [ -z "$output" ]; then
  132. echo -e "\t$(date +%H:%m:%S) $domain"
  133. fi
  134. # Building directory tree
  135. mkdir $tmpdir/dns/$domain
  136. # Creating dns_domains config
  137. cd $tmpdir/dns/$domain/
  138. conf="$V_USERS/$user/dns.conf"
  139. grep "DOMAIN='$domain'" $conf > dns.conf
  140. # Backingup dns recods
  141. cp $V_USERS/$user/zones/$domain $domain
  142. cp /etc/namedb/$domain.db $domain.db
  143. done
  144. if [ -z "$output" ]; then
  145. echo
  146. fi
  147. fi
  148. # Mail domains
  149. # TBD
  150. # DatbaBases
  151. if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ]; then
  152. if [ -z "$output" ]; then
  153. echo "-- DB --"
  154. fi
  155. mkdir $tmpdir/db/
  156. # Parsing unsuspeneded domains
  157. conf="$V_USERS/$user/db.conf"
  158. field='$DB'
  159. search_string='DB='
  160. dbs=$(dom_clear_search)
  161. for db in $dbs; do
  162. if [ -z "$output" ]; then
  163. echo -e "\t$(date +%H:%m:%S) $db"
  164. fi
  165. done
  166. if [ -z "$output" ]; then
  167. echo
  168. fi
  169. fi
  170. # Cron jobs
  171. if [ ! -z "$CRON_SYSTEM" ] && [ "$CRON_SYSTEM" != 'no' ]; then
  172. if [ -z "$output" ]; then
  173. echo "-- CRON --"
  174. fi
  175. mkdir $tmpdir/cron/
  176. # Backingup cron recods
  177. if [ -z "$output" ]; then
  178. echo -e "\t$(date +%H:%m:%S) cron.conf"
  179. fi
  180. cp $V_USERS/$user/cron.conf $tmpdir/cron/
  181. if [ -z "$output" ]; then
  182. echo -e "\t$(date +%H:%m:%S) system cron"
  183. fi
  184. cp /var/spool/cron/$user $tmpdir/cron/
  185. if [ -z "$output" ]; then
  186. echo
  187. fi
  188. fi
  189. # SSL CERTIFICATES
  190. if [ ! -z "$WEB_SSL" ] && [ "$WEB_SSL" != 'no' ]; then
  191. if [ -z "$output" ]; then
  192. echo "-- CERTIFICATES --"
  193. fi
  194. mkdir $tmpdir/cert
  195. # Backingup ssl certificates
  196. for cert in $(ls $V_USERS/$user/cert/); do
  197. if [ -z "$output" ]; then
  198. echo -e "\t$(date +%H:%m:%S) $cert"
  199. fi
  200. cp $V_USERS/$user/cert/$cert $tmpdir/cert/
  201. done
  202. if [ -z "$output" ]; then
  203. echo
  204. fi
  205. fi
  206. # Vesta
  207. if [ -z "$output" ]; then
  208. echo "-- VESTA --"
  209. fi
  210. mkdir $tmpdir/vesta
  211. # Backingup vesta configs
  212. if [ -z "$output" ]; then
  213. echo -e "\t$(date +%H:%m:%S) user.conf"
  214. fi
  215. cp -r $V_USERS/$user/user.conf $tmpdir/vesta/
  216. if [ -e "$V_USERS/$user/billing.log" ]; then
  217. if [ -z "$output" ]; then
  218. echo -e "\t$(date +%H:%m:%S) billing.log"
  219. fi
  220. cp -r $V_USERS/$user/billing.log $tmpdir/vesta/
  221. fi
  222. if [ -e "$V_USERS/$user/reseller.conf" ]; then
  223. if [ -z "$output" ]; then
  224. echo -e "\t$(date +%H:%m:%S) reseller.conf"
  225. fi
  226. cp -r $V_USERS/$user/reseller.conf $tmpdir/vesta/
  227. fi
  228. if [ -e "$V_USERS/$user/history.log" ]; then
  229. if [ -z "$output" ]; then
  230. echo -e "\t$(date +%H:%m:%S) history.log"
  231. fi
  232. cp -r $V_USERS/$user/history.log $tmpdir/vesta/
  233. fi
  234. if [ -z "$output" ]; then
  235. echo
  236. fi
  237. #----------------------------------------------------------#
  238. # Vesta #
  239. #----------------------------------------------------------#
  240. # Logging
  241. log_event 'system' "$V_EVENT"
  242. exit $OK