v_backup_sys_user 12 KB

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