backup.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. #!/bin/bash
  2. #===========================================================================#
  3. # #
  4. # Hestia Control Panel - Backup Function Library #
  5. # #
  6. #===========================================================================#
  7. # Local storage
  8. # Defining local storage function
  9. local_backup(){
  10. rm -f $BACKUP/$user.$backup_new_date.tar
  11. # Checking retention
  12. backup_list=$(ls -lrt $BACKUP/ |awk '{print $9}' |grep "^$user\." | grep ".tar")
  13. backups_count=$(echo "$backup_list" |wc -l)
  14. if [ "$BACKUPS" -le "$backups_count" ]; then
  15. backups_rm_number=$((backups_count - BACKUPS + 1))
  16. # Removing old backup
  17. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  18. backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar$//")
  19. echo -e "$(date "+%F %T") Rotated: $backup_date" |\
  20. tee -a $BACKUP/$user.log
  21. rm -f $BACKUP/$backup
  22. done
  23. fi
  24. # Checking disk space
  25. disk_usage=$(df $BACKUP |tail -n1 |tr ' ' '\n' |grep % |cut -f 1 -d %)
  26. if [ "$disk_usage" -ge "$BACKUP_DISK_LIMIT" ]; then
  27. rm -rf $tmpdir
  28. rm -f $BACKUP/$user.log
  29. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  30. echo "Not enough disk space" | $SENDMAIL -s "$subj" "$email" "yes"
  31. check_result "$E_DISK" "Not enough dsk space"
  32. fi
  33. # Creating final tarball
  34. cd $tmpdir
  35. tar -cf $BACKUP/$user.$backup_new_date.tar .
  36. chmod 640 $BACKUP/$user.$backup_new_date.tar
  37. chown admin:$user $BACKUP/$user.$backup_new_date.tar
  38. localbackup='yes'
  39. echo -e "$(date "+%F %T") Local: $BACKUP/$user.$backup_new_date.tar" |\
  40. tee -a $BACKUP/$user.log
  41. }
  42. # FTP Functions
  43. # Defining ftp command function
  44. ftpc() {
  45. /usr/bin/ftp -np $HOST $PORT <<EOF
  46. quote USER $USERNAME
  47. quote PASS $PASSWORD
  48. binary
  49. $1
  50. $2
  51. $3
  52. quit
  53. EOF
  54. }
  55. # Defining ftp storage function
  56. ftp_backup() {
  57. # Checking config
  58. if [ ! -e "$HESTIA/conf/ftp.backup.conf" ]; then
  59. error="ftp.backup.conf doesn't exist"
  60. echo "$error" |$SENDMAIL -s "$subj" $email "yes"
  61. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  62. echo "$error"
  63. errorcode="$E_NOTEXIST"
  64. return "$E_NOTEXIST"
  65. fi
  66. # Parse config
  67. source_conf "$HESTIA/conf/ftp.backup.conf"
  68. # Set default port
  69. if [ -z "$(grep 'PORT=' $HESTIA/conf/ftp.backup.conf)" ]; then
  70. PORT='21'
  71. fi
  72. # Checking variables
  73. if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
  74. error="Can't parse ftp backup configuration"
  75. echo "$error" |$SENDMAIL -s "$subj" $email "yes"
  76. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  77. echo "$error"
  78. errorcode="$E_PARSING"
  79. return "$E_PARSING"
  80. fi
  81. # Debug info
  82. echo -e "$(date "+%F %T") Remote: ftp://$HOST$BPATH/$user.$backup_new_date.tar"
  83. # Checking ftp connection
  84. fconn=$(ftpc)
  85. ferror=$(echo $fconn |grep -i -e failed -e error -e "Can't" -e "not conn")
  86. if [ -n "$ferror" ]; then
  87. error="Error: can't login to ftp ftp://$USERNAME@$HOST"
  88. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  89. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  90. echo "$error"
  91. errorcode="$E_CONNECT"
  92. return "$E_CONNECT"
  93. fi
  94. # Check ftp permissions
  95. if [ -z $BPATH ]; then
  96. ftmpdir="vst.bK76A9SUkt"
  97. else
  98. ftpc "mkdir $BPATH" > /dev/null 2>&1
  99. ftmpdir="$BPATH/vst.bK76A9SUkt"
  100. fi
  101. ftpc "mkdir $ftmpdir" "rm $ftmpdir"
  102. ftp_result=$(ftpc "mkdir $ftmpdir" "rm $ftmpdir" |grep -v Trying)
  103. if [ -n "$ftp_result" ] ; then
  104. error="Can't create ftp backup folder ftp://$HOST$BPATH"
  105. echo "$error" |$SENDMAIL -s "$subj" $email $notify
  106. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  107. echo "$error"
  108. errorcode="$E_FTP"
  109. return "$E_FTP"
  110. fi
  111. # Checking retention
  112. if [ -z $BPATH ]; then
  113. backup_list=$(ftpc "ls" |awk '{print $9}' |grep "^$user\.")
  114. else
  115. backup_list=$(ftpc "cd $BPATH" "ls" |awk '{print $9}' |grep "^$user\.")
  116. fi
  117. backups_count=$(echo "$backup_list" |wc -l)
  118. if [ "$backups_count" -ge "$BACKUPS" ]; then
  119. backups_rm_number=$((backups_count - BACKUPS + 1))
  120. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  121. backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar$//")
  122. echo -e "$(date "+%F %T") Rotated ftp backup: $backup_date" |\
  123. tee -a $BACKUP/$user.log
  124. if [ -z $BPATH ]; then
  125. ftpc "delete $backup"
  126. else
  127. ftpc "cd $BPATH" "delete $backup"
  128. fi
  129. done
  130. fi
  131. # Uploading backup archive
  132. if [ "$localbackup" = 'yes' ]; then
  133. cd $BACKUP
  134. if [ -z $BPATH ]; then
  135. ftpc "put $user.$backup_new_date.tar"
  136. else
  137. ftpc "cd $BPATH" "put $user.$backup_new_date.tar"
  138. fi
  139. else
  140. cd $tmpdir
  141. tar -cf $BACKUP/$user.$backup_new_date.tar .
  142. cd $BACKUP/
  143. if [ -z $BPATH ]; then
  144. ftpc "put $user.$backup_new_date.tar"
  145. else
  146. ftpc "cd $BPATH" "put $user.$backup_new_date.tar"
  147. fi
  148. rm -f $user.$backup_new_date.tar
  149. fi
  150. }
  151. # FTP backup download function
  152. ftp_download() {
  153. source_conf "$HESTIA/conf/ftp.backup.conf"
  154. if [ -z "$PORT" ]; then
  155. PORT='21'
  156. fi
  157. cd $BACKUP
  158. if [ -z $BPATH ]; then
  159. ftpc "get $1"
  160. else
  161. ftpc "cd $BPATH" "get $1"
  162. fi
  163. }
  164. #FTP Delete function
  165. ftp_delete() {
  166. source_conf "$HESTIA/conf/ftp.backup.conf"
  167. if [ -z "$PORT" ]; then
  168. PORT='21'
  169. fi
  170. if [ -z $BPATH ]; then
  171. ftpc "delete $1"
  172. else
  173. ftpc "cd $BPATH" "delete $1"
  174. fi
  175. }
  176. # SFTP Functions
  177. # sftp command function
  178. sftpc() {
  179. if [ $PRIVATEKEY != "yes" ]; then
  180. expect -f "-" <<EOF "$@"
  181. set timeout 60
  182. set count 0
  183. spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
  184. -o Port=$PORT $USERNAME@$HOST
  185. expect {
  186. -nocase "password:" {
  187. send "$PASSWORD\r"
  188. exp_continue
  189. }
  190. -re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
  191. set count \$argc
  192. set output "Disconnected."
  193. set rc $E_FTP
  194. exp_continue
  195. }
  196. -re ".*denied.*(publickey|password)." {
  197. set output "Permission denied, wrong publickey or password."
  198. set rc $E_CONNECT
  199. }
  200. -re "\[0-9]*%" {
  201. exp_continue
  202. }
  203. "sftp>" {
  204. if {\$count < \$argc} {
  205. set arg [lindex \$argv \$count]
  206. send "\$arg\r"
  207. incr count
  208. } else {
  209. send "exit\r"
  210. set output "Disconnected."
  211. if {[info exists rc] != 1} {
  212. set rc $OK
  213. }
  214. }
  215. exp_continue
  216. }
  217. timeout {
  218. set output "Connection timeout."
  219. set rc $E_CONNECT
  220. }
  221. }
  222. if {[info exists output] == 1} {
  223. puts "\$output"
  224. }
  225. exit \$rc
  226. EOF
  227. else
  228. expect -f "-" <<EOF "$@"
  229. set timeout 60
  230. set count 0
  231. spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
  232. -o Port=$PORT -i $PASSWORD $USERNAME@$HOST
  233. expect {
  234. -nocase "password:" {
  235. send "$PASSWORD\r"
  236. exp_continue
  237. }
  238. -re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
  239. set count \$argc
  240. set output "Disconnected."
  241. set rc $E_FTP
  242. exp_continue
  243. }
  244. -re ".*denied.*(publickey|password)." {
  245. set output "Permission denied, wrong publickey or password."
  246. set rc $E_CONNECT
  247. }
  248. -re "\[0-9]*%" {
  249. exp_continue
  250. }
  251. "sftp>" {
  252. if {\$count < \$argc} {
  253. set arg [lindex \$argv \$count]
  254. send "\$arg\r"
  255. incr count
  256. } else {
  257. send "exit\r"
  258. set output "Disconnected."
  259. if {[info exists rc] != 1} {
  260. set rc $OK
  261. }
  262. }
  263. exp_continue
  264. }
  265. timeout {
  266. set output "Connection timeout."
  267. set rc $E_CONNECT
  268. }
  269. }
  270. if {[info exists output] == 1} {
  271. puts "\$output"
  272. }
  273. exit \$rc
  274. EOF
  275. fi
  276. }
  277. # SFTP backup download function
  278. sftp_download() {
  279. source_conf "$HESTIA/conf/sftp.backup.conf"
  280. if [ -z "$PORT" ]; then
  281. PORT='22'
  282. fi
  283. cd $BACKUP
  284. if [ -z $BPATH ]; then
  285. sftpc "get $1" > /dev/null 2>&1
  286. else
  287. sftpc "cd $BPATH" "get $1" > /dev/null 2>&1
  288. fi
  289. }
  290. sftp_delete() {
  291. echo "$1"
  292. source_conf "$HESTIA/conf/sftp.backup.conf"
  293. if [ -z "$PORT" ]; then
  294. PORT='22'
  295. fi
  296. echo $BPATH
  297. if [ -z $BPATH ]; then
  298. sftpc "rm $1" > /dev/null 2>&1
  299. else
  300. sftpc "cd $BPATH" "rm $1" > /dev/null 2>&1
  301. fi
  302. }
  303. sftp_backup() {
  304. # Checking config
  305. if [ ! -e "$HESTIA/conf/sftp.backup.conf" ]; then
  306. error="Can't open sftp.backup.conf"
  307. echo "$error" |$SENDMAIL -s "$subj" $email "yes"
  308. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  309. echo "$error"
  310. errorcode="$E_NOTEXIST"
  311. return "$E_NOTEXIST"
  312. fi
  313. # Parse config
  314. source_conf "$HESTIA/conf/sftp.backup.conf"
  315. # Set default port
  316. if [ -z "$(grep 'PORT=' $HESTIA/conf/sftp.backup.conf)" ]; then
  317. PORT='22'
  318. fi
  319. # Checking variables
  320. if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
  321. error="Can't parse sftp backup configuration"
  322. echo "$error" |$SENDMAIL -s "$subj" $email "yes"
  323. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  324. echo "$error"
  325. errorcode="$E_PARSING"
  326. return "$E_PARSING"
  327. fi
  328. # Debug info
  329. echo -e "$(date "+%F %T") Remote: sftp://$HOST/$BPATH/$user.$backup_new_date.tar" |\
  330. tee -a $BACKUP/$user.log
  331. # Checking network connection and write permissions
  332. if [ -z $BPATH ]; then
  333. sftmpdir="vst.bK76A9SUkt"
  334. else
  335. sftmpdir="$BPATH/vst.bK76A9SUkt"
  336. fi
  337. sftpc "mkdir $BPATH" > /dev/null 2>&1
  338. sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
  339. rc=$?
  340. if [[ "$rc" != 0 ]]; then
  341. case $rc in
  342. $E_CONNECT) error="Can't login to sftp host $HOST" ;;
  343. $E_FTP) error="Can't create temp folder on sftp $HOST" ;;
  344. esac
  345. echo "$error" |$SENDMAIL -s "$subj" $email "yes"
  346. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  347. echo "$error"
  348. errorcode="$rc"
  349. return "$rc"
  350. fi
  351. # Checking retention
  352. if [ -z $BPATH ]; then
  353. backup_list=$(sftpc "ls -l" |awk '{print $9}'|grep "^$user\.")
  354. else
  355. backup_list=$(sftpc "cd $BPATH" "ls -l" |awk '{print $9}'|grep "^$user\.")
  356. fi
  357. backups_count=$(echo "$backup_list" |wc -l)
  358. if [ "$backups_count" -ge "$BACKUPS" ]; then
  359. backups_rm_number=$((backups_count - BACKUPS + 1))
  360. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  361. backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar.*$//")
  362. echo -e "$(date "+%F %T") Rotated sftp backup: $backup_date" |\
  363. tee -a $BACKUP/$user.log
  364. if [ -z $BPATH ]; then
  365. sftpc "rm $backup" > /dev/null 2>&1
  366. else
  367. sftpc "cd $BPATH" "rm $backup" > /dev/null 2>&1
  368. fi
  369. done
  370. fi
  371. # Uploading backup archive
  372. echo "$(date "+%F %T") Uploading $user.$backup_new_date.tar"|tee -a $BACKUP/$user.log
  373. if [ "$localbackup" = 'yes' ]; then
  374. cd $BACKUP
  375. if [ -z $BPATH ]; then
  376. sftpc "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  377. else
  378. sftpc "cd $BPATH" "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  379. fi
  380. else
  381. cd $tmpdir
  382. tar -cf $BACKUP/$user.$backup_new_date.tar .
  383. cd $BACKUP/
  384. if [ -z $BPATH ]; then
  385. sftpc "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  386. else
  387. sftpc "cd $BPATH" "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  388. fi
  389. rm -f $user.$backup_new_date.tar
  390. fi
  391. }
  392. # Google backup download function
  393. google_backup() {
  394. # Defining google settings
  395. source_conf "$HESTIA/conf/google.backup.conf"
  396. gsutil="$HESTIA/3rdparty/gsutil/gsutil"
  397. export BOTO_CONFIG="$HESTIA/conf/.google.backup.boto"
  398. # Debug info
  399. echo -e "$(date "+%F %T") Remote: gs://$BUCKET/$BPATH/$user.$backup_new_date.tar"
  400. # Checking retention
  401. backup_list=$(${gsutil} ls gs://$BUCKET/$BPATH/$user.* 2>/dev/null)
  402. backups_count=$(echo "$backup_list" |wc -l)
  403. if [ "$backups_count" -ge "$BACKUPS" ]; then
  404. backups_rm_number=$((backups_count - BACKUPS))
  405. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  406. echo -e "$(date "+%F %T") Rotated gcp backup: $backup"
  407. $gsutil rm $backup > /dev/null 2>&1
  408. done
  409. fi
  410. # Uploading backup archive
  411. echo -e "$(date "+%F %T") Uploading $user.$backup_new_date.tar ..."
  412. if [ "$localbackup" = 'yes' ]; then
  413. cd $BACKUP
  414. ${gsutil} cp $user.$backup_new_date.tar gs://$BUCKET/$BPATH/ > /dev/null 2>&1
  415. else
  416. cd $tmpdir
  417. tar -cf $BACKUP/$user.$backup_new_date.tar .
  418. cd $BACKUP/
  419. ${gsutil} cp $user.$backup_new_date.tar gs://$BUCKET/$BPATH/ > /dev/null 2>&1
  420. rc=$?
  421. rm -f $user.$backup_new_date.tar
  422. if [ "$rc" -ne 0 ]; then
  423. check_result "$E_CONNECT" "gsutil failed to upload $user.$backup_new_date.tar"
  424. fi
  425. fi
  426. }
  427. google_download() {
  428. source_conf "$HESTIA/conf/google.backup.conf"
  429. gsutil="$HESTIA/3rdparty/gsutil/gsutil"
  430. export BOTO_CONFIG="$HESTIA/conf/.google.backup.boto"
  431. ${gsutil} cp gs://$BUCKET/$BPATH/$1 $BACKUP/ > /dev/null 2>&1
  432. if [ "$?" -ne 0 ]; then
  433. check_result "$E_CONNECT" "gsutil failed to download $1"
  434. fi
  435. }
  436. # BackBlaze B2 backup function
  437. b2_backup() {
  438. # Defining backblaze b2 settings
  439. source_conf "$HESTIA/conf/b2.backup.conf"
  440. # Recreate backblaze auth file ~/.b2_account_info (for situation when key was changed in b2.backup.conf)
  441. b2 clear-account > /dev/null 2>&1
  442. b2 authorize-account $B2_KEYID $B2_KEY > /dev/null 2>&1
  443. # Uploading backup archive
  444. echo -e "$(date "+%F %T") Upload to B2: $user/$user.$backup_new_date.tar"
  445. if [ "$localbackup" = 'yes' ]; then
  446. cd $BACKUP
  447. b2 upload-file $BUCKET $user.$backup_new_date.tar $user/$user.$backup_new_date.tar > /dev/null 2>&1
  448. else
  449. cd $tmpdir
  450. tar -cf $BACKUP/$user.$backup_new_date.tar .
  451. cd $BACKUP/
  452. b2 upload-file $BUCKET $user.$backup_new_date.tar $user/$user.$backup_new_date.tar > /dev/null 2>&1
  453. rc=$?
  454. rm -f $user.$backup_new_date.tar
  455. if [ "$rc" -ne 0 ]; then
  456. check_result "$E_CONNECT" "b2 failed to upload $user.$backup_new_date.tar"
  457. fi
  458. fi
  459. # Checking retention
  460. backup_list=$(b2 ls --long $BUCKET $user | cut -f 1 -d ' ' 2>/dev/null)
  461. backups_count=$(echo "$backup_list" |wc -l)
  462. if [ "$backups_count" -ge "$BACKUPS" ]; then
  463. backups_rm_number=$((backups_count - BACKUPS))
  464. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  465. backup_file_name=$(b2 get-file-info $backup | grep fileName | cut -f 4 -d '"' 2>/dev/null)
  466. echo -e "$(date "+%F %T") Rotated b2 backup: $backup_file_name"
  467. b2 delete-file-version $backup > /dev/null 2>&1
  468. done
  469. fi
  470. }
  471. b2_download() {
  472. # Defining backblaze b2 settings
  473. source_conf "$HESTIA/conf/b2.backup.conf"
  474. # Recreate backblaze auth file ~/.b2_account_info (for situation when key was changed in b2.backup.conf)
  475. b2 clear-account > /dev/null 2>&1
  476. b2 authorize-account $B2_KEYID $B2_KEY > /dev/null 2>&1
  477. cd $BACKUP
  478. b2 download-file-by-name $BUCKET $user/$1 $1 > /dev/null 2>&1
  479. if [ "$?" -ne 0 ]; then
  480. check_result "$E_CONNECT" "b2 failed to download $user.$1"
  481. fi
  482. }
  483. b2_delete(){
  484. # Defining backblaze b2 settings
  485. source_conf "$HESTIA/conf/b2.backup.conf"
  486. # Recreate backblaze auth file ~/.b2_account_info (for situation when key was changed in b2.backup.conf)
  487. b2 clear-account > /dev/null 2>&1
  488. b2 authorize-account $B2_KEYID $B2_KEY > /dev/null 2>&1
  489. b2 delete-file-version $1/$2 > /dev/null 2>&1
  490. }