v_backup_sys_user 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. #----------------------------------------------------------#
  29. # Action #
  30. #----------------------------------------------------------#
  31. # Creating temporary random directory
  32. tmpdir=$(mktemp -p $V_TMP -d)
  33. # Prinitng status
  34. if [ -z "$output" ]; then
  35. echo "$(date +%m-%d-%y" "%H:%m:%S) System backup for user $user"
  36. echo
  37. echo "VESTA VERSION $VERSION"
  38. echo "BACKUP VERSION 1.0"
  39. echo "TMPDIR is $tmpdir"
  40. echo
  41. fi
  42. # Addding vesta version
  43. echo "1.0" >$tmpdir/backup_version
  44. echo "$VERSION" > $tmpdir/vesta_version
  45. # Checking excludes
  46. OLD_IFS="$IFS"
  47. IFS=$'\n'
  48. if [ -e "$V_USERS/$user/backup.excludes" ]; then
  49. if [ -z "$output" ]; then
  50. echo "-- Excludes --"
  51. fi
  52. for exclude in $(cat $V_USERS/$user/backup.excludes); do
  53. if [ -z "$output" ]; then
  54. echo -e "\t $exclude"
  55. fi
  56. # Indirect variable references (a bit of black magic)
  57. eval ${exclude%%=*}=${exclude#*=}
  58. done
  59. if [ -z "$output" ]; then
  60. echo
  61. fi
  62. fi
  63. IFS="$OLD_IFS"
  64. # WEB domains
  65. if [ ! -z "$WEB_SYSTEM" ] && [ "$WEB_SYSTEM" != 'no' ] && [ "$WEB" != '*' ]
  66. then
  67. if [ -z "$output" ]; then
  68. echo "-- WEB --"
  69. fi
  70. mkdir $tmpdir/web/
  71. # Parsing unsuspeneded domains
  72. conf="$V_USERS/$user/web.conf"
  73. field='$DOMAIN'
  74. search_string='DOMAIN='
  75. domains=$(dom_clear_search)
  76. domain_list=''
  77. # Cleaning excludes
  78. for domain in $domains; do
  79. check_exl=$(echo "$WEB"|grep -w $domain)
  80. if [ -z "$check_exl" ]; then
  81. web_list="$web_list $domain"
  82. fi
  83. done
  84. for domain in $web_list; do
  85. if [ -z "$output" ]; then
  86. echo -e "\t$(date +%H:%m:%S) $domain"
  87. fi
  88. # Defining domain variables
  89. domain_idn=$(idn -t --quiet -a "$domain")
  90. tpl_name=$(get_web_domain_value '$TPL')
  91. ssl_cert=$(get_web_domain_value '$SSL_CERT')
  92. nginx=$(get_web_domain_value '$NGINX')
  93. # Building directory tree
  94. mkdir -p $tmpdir/web/$domain/conf $tmpdir/web/$domain/cert
  95. # Packing data folders
  96. cd $V_HOME/$user/web/$domain
  97. tar -cf $tmpdir/web/$domain/$domain.tar \
  98. public_html public_shtml private document_errors cgi-bin stats
  99. # Creating web.config
  100. cd $tmpdir/web/$domain/
  101. conf="$V_USERS/$user/web.conf"
  102. grep "DOMAIN='$domain'" $conf > conf/web.conf
  103. # Apache config
  104. if [ "$WEB_SYSTEM" = 'apache' ]; then
  105. # Parsing httpd.conf
  106. tpl_file="$V_WEBTPL/apache_$tpl_name.tpl"
  107. conf="$V_HOME/$user/conf/httpd.conf"
  108. get_web_config_brds
  109. sed -n "$top_line,$bottom_line p" $conf > conf/httpd.conf
  110. # SSL check
  111. if [ ! -z "$ssl_cert" ]; then
  112. tpl_file="$V_WEBTPL/apache_$tpl_name.stpl"
  113. conf="$V_HOME/$user/conf/shttpd.conf"
  114. get_web_config_brds
  115. sed -n "$top_line,$bottom_line p" $conf > conf/shttpd.conf
  116. fi
  117. fi
  118. # Nginx config
  119. if [ ! -z "$nginx" ] ; then
  120. tpl_file="$V_WEBTPL/ngingx_vhost_$nginx.tpl"
  121. conf="$V_HOME/$user/conf/nginx.conf"
  122. get_web_config_brds
  123. sed -n "$top_line,$bottom_line p" $conf > conf/nginx.conf
  124. # SSL check
  125. if [ ! -z "$ssl_cert" ] ; then
  126. tpl_file="$V_WEBTPL/ngingx_vhost_$nginx.stpl"
  127. conf="$V_HOME/$user/conf/snginx.conf"
  128. get_web_config_brds
  129. sed -n "$top_line,$bottom_line p" $conf > conf/snginx.conf
  130. fi
  131. fi
  132. # Suplemental configs
  133. for sconfig in $(ls $V_HOME/$user/conf/|grep ".$domain.conf"); do
  134. cp $V_HOME/$user/conf/$sconfig conf/
  135. done
  136. # SSL Certificates
  137. if [ ! -z "$ssl_cert" ] ; then
  138. cp $V_HOME/$user/conf/$ssl_cert.* cert/
  139. fi
  140. tar -rf $tmpdir/web/$domain/$domain.tar conf cert
  141. mv $tmpdir/web/$domain/$domain.tar $tmpdir/web/
  142. rm -rf $tmpdir/web/$domain
  143. if [ ! -z "$V_BACKUP_GZIP" ]; then
  144. gzip -$V_BACKUP_GZIP $tmpdir/web/$domain.tar
  145. fi
  146. done
  147. if [ -z "$output" ]; then
  148. echo
  149. fi
  150. fi
  151. # DNS domains
  152. if [ ! -z "$DNS_SYSTEM" ] && [ "$DNS_SYSTEM" != 'no' ] && [ "$DNS" != '*' ]
  153. then
  154. if [ -z "$output" ]; then
  155. echo "-- DNS --"
  156. fi
  157. mkdir $tmpdir/dns/
  158. # Parsing unsuspeneded domains
  159. conf="$V_USERS/$user/dns.conf"
  160. field='$DOMAIN'
  161. search_string='DOMAIN='
  162. domains=$(dom_clear_search)
  163. # Cleaning excludes
  164. for domain in $domains; do
  165. check_exl=$(echo "$DNS"|grep -w $domain)
  166. if [ -z "$check_exl" ]; then
  167. dns_list="$dns_list $domain"
  168. fi
  169. done
  170. for domain in $dns_list; do
  171. if [ -z "$output" ]; then
  172. echo -e "\t$(date +%H:%m:%S) $domain"
  173. fi
  174. # Building directory tree
  175. mkdir $tmpdir/dns/$domain
  176. # Creating dns_domains config
  177. cd $tmpdir/dns/$domain/
  178. conf="$V_USERS/$user/dns.conf"
  179. grep "DOMAIN='$domain'" $conf > dns.conf
  180. # Backingup dns recods
  181. cp $V_USERS/$user/zones/$domain $domain
  182. cp /etc/namedb/$domain.db $domain.db
  183. done
  184. if [ -z "$output" ]; then
  185. echo
  186. fi
  187. fi
  188. # Mail domains
  189. # TBD
  190. # DatbaBases
  191. if [ ! -z "$DB_SYSTEM" ] && [ "$DB_SYSTEM" != 'no' ] && [ "$DB" != '*' ]; then
  192. if [ -z "$output" ]; then
  193. echo "-- DB --"
  194. fi
  195. mkdir $tmpdir/db/
  196. # Parsing unsuspeneded domains
  197. conf="$V_USERS/$user/db.conf"
  198. field='$DB'
  199. search_string='DB='
  200. dbs=$(dom_clear_search)
  201. # Cleaning excludes
  202. for database in $dbs; do
  203. check_exl=$(echo "$DB"|grep -w $database)
  204. if [ -z "$check_exl" ]; then
  205. db_list="$db_list $database"
  206. fi
  207. done
  208. for database in $db_list; do
  209. type=$(get_db_value '$TYPE')
  210. host=$(get_db_value '$HOST')
  211. db_user=$(get_db_value '$USER')
  212. dump="$tmpdir/db/$database.$type.sql"
  213. grants="$tmpdir/db/$database.$type.$db_user"
  214. if [ -z "$output" ]; then
  215. echo -e "\t$(date +%H:%m:%S) $database $type"
  216. fi
  217. case $type in
  218. mysql) dump_db_mysql ;;
  219. pgsql) dump_db_pgsql ;;
  220. esac
  221. if [ ! -z "$V_BACKUP_GZIP" ]; then
  222. gzip -$V_BACKUP_GZIP $dump
  223. fi
  224. done
  225. if [ -z "$output" ]; then
  226. echo
  227. fi
  228. fi
  229. # Cron jobs
  230. if [ ! -z "$CRON_SYSTEM" ] && [ "$CRON_SYSTEM" != 'no' ] && [ "$CRON" != '*' ]
  231. then
  232. if [ -z "$output" ]; then
  233. echo "-- CRON --"
  234. fi
  235. mkdir $tmpdir/cron/
  236. # Backingup cron recods
  237. if [ -z "$output" ]; then
  238. echo -e "\t$(date +%H:%m:%S) cron.conf"
  239. fi
  240. cp $V_USERS/$user/cron.conf $tmpdir/cron/
  241. if [ -z "$output" ]; then
  242. echo -e "\t$(date +%H:%m:%S) system cron"
  243. fi
  244. cp /var/spool/cron/$user $tmpdir/cron/
  245. if [ -z "$output" ]; then
  246. echo
  247. fi
  248. fi
  249. # SSL CERTIFICATES
  250. if [ ! -z "$WEB_SSL" ] && [ "$WEB_SSL" != 'no' ] && [ "$SSL" != '*' ]; then
  251. if [ -z "$output" ]; then
  252. echo "-- CERTIFICATES --"
  253. fi
  254. mkdir $tmpdir/cert
  255. # Backingup ssl certificates
  256. for cert in $(ls $V_USERS/$user/cert/); do
  257. if [ -z "$output" ]; then
  258. echo -e "\t$(date +%H:%m:%S) $cert"
  259. fi
  260. cp $V_USERS/$user/cert/$cert $tmpdir/cert/
  261. done
  262. if [ -z "$output" ]; then
  263. echo
  264. fi
  265. fi
  266. # Vesta
  267. if [ -z "$output" ]; then
  268. echo "-- VESTA --"
  269. fi
  270. mkdir $tmpdir/vesta
  271. # Backingup vesta configs
  272. if [ -z "$output" ]; then
  273. echo -e "\t$(date +%H:%m:%S) user.conf"
  274. fi
  275. cp -r $V_USERS/$user/user.conf $tmpdir/vesta/
  276. if [ -e "$V_USERS/$user/billing.log" ]; then
  277. if [ -z "$output" ]; then
  278. echo -e "\t$(date +%H:%m:%S) billing.log"
  279. fi
  280. cp -r $V_USERS/$user/billing.log $tmpdir/vesta/
  281. fi
  282. if [ -e "$V_USERS/$user/reseller.conf" ]; then
  283. if [ -z "$output" ]; then
  284. echo -e "\t$(date +%H:%m:%S) reseller.conf"
  285. fi
  286. cp -r $V_USERS/$user/reseller.conf $tmpdir/vesta/
  287. fi
  288. if [ -e "$V_USERS/$user/history.log" ]; then
  289. if [ -z "$output" ]; then
  290. echo -e "\t$(date +%H:%m:%S) history.log"
  291. fi
  292. cp -r $V_USERS/$user/history.log $tmpdir/vesta/
  293. fi
  294. if [ -e "$V_USERS/$user/backup.excludes" ]; then
  295. if [ -z "$output" ]; then
  296. echo -e "\t$(date +%H:%m:%S) backup.excludes"
  297. fi
  298. cp -r $V_USERS/$user/backup.excludes $tmpdir/vesta/
  299. fi
  300. if [ -z "$output" ]; then
  301. echo
  302. fi
  303. # Move tmp backup to local storage
  304. if [ "$BACKUP_SYSTEM" = 'local' ]; then
  305. if [ -z "$output" ]; then
  306. echo "ARCHIVE $V_BACKUP/$user.$V_DATE.tar"
  307. fi
  308. # Checking retention
  309. check_ret=$()
  310. cd $tmpdir
  311. tar -cf $V_BACKUP/$user.$V_DATE.tar .
  312. fi
  313. cd /
  314. rm -rf $tmpdir
  315. if [ -z "$output" ]; then
  316. echo
  317. fi
  318. #----------------------------------------------------------#
  319. # Vesta #
  320. #----------------------------------------------------------#
  321. # Logging
  322. log_event 'system' "$V_EVENT"
  323. exit $OK