backup.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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" | sort)
  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 "hestiaweb":"$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 (Only include .tar files)
  112. if [ -z $BPATH ]; then
  113. backup_list=$(ftpc "ls" | awk '{print $9}' | grep "^$user\." | grep ".tar" | sort)
  114. else
  115. backup_list=$(ftpc "cd $BPATH" "ls" | awk '{print $9}' | grep "^$user\." | grep ".tar" | sort)
  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 "Password for (.*)@(.*)" {
  191. send "$PASSWORD\r"
  192. exp_continue
  193. }
  194. -re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
  195. set count \$argc
  196. set output "Disconnected."
  197. set rc $E_FTP
  198. exp_continue
  199. }
  200. -re ".*denied.*(publickey|password)." {
  201. set output "Permission denied, wrong publickey or password."
  202. set rc $E_CONNECT
  203. }
  204. -re "\[0-9]*%" {
  205. exp_continue
  206. }
  207. "sftp>" {
  208. if {\$count < \$argc} {
  209. set arg [lindex \$argv \$count]
  210. send "\$arg\r"
  211. incr count
  212. } else {
  213. send "exit\r"
  214. set output "Disconnected."
  215. if {[info exists rc] != 1} {
  216. set rc $OK
  217. }
  218. }
  219. exp_continue
  220. }
  221. timeout {
  222. set output "Connection timeout."
  223. set rc $E_CONNECT
  224. }
  225. }
  226. if {[info exists output] == 1} {
  227. puts "\$output"
  228. }
  229. exit \$rc
  230. EOF
  231. else
  232. expect -f "-" "$@" << EOF
  233. set timeout 60
  234. set count 0
  235. spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
  236. -o Port=$PORT -i $PASSWORD $USERNAME@$HOST
  237. expect {
  238. -nocase "password:" {
  239. send "$PASSWORD\r"
  240. exp_continue
  241. }
  242. -re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
  243. set count \$argc
  244. set output "Disconnected."
  245. set rc $E_FTP
  246. exp_continue
  247. }
  248. -re ".*denied.*(publickey|password)." {
  249. set output "Permission denied, wrong publickey or password."
  250. set rc $E_CONNECT
  251. }
  252. -re "\[0-9]*%" {
  253. exp_continue
  254. }
  255. "sftp>" {
  256. if {\$count < \$argc} {
  257. set arg [lindex \$argv \$count]
  258. send "\$arg\r"
  259. incr count
  260. } else {
  261. send "exit\r"
  262. set output "Disconnected."
  263. if {[info exists rc] != 1} {
  264. set rc $OK
  265. }
  266. }
  267. exp_continue
  268. }
  269. timeout {
  270. set output "Connection timeout."
  271. set rc $E_CONNECT
  272. }
  273. }
  274. if {[info exists output] == 1} {
  275. puts "\$output"
  276. }
  277. exit \$rc
  278. EOF
  279. fi
  280. }
  281. # SFTP backup download function
  282. sftp_download() {
  283. source_conf "$HESTIA/conf/sftp.backup.conf"
  284. if [ -z "$PORT" ]; then
  285. PORT='22'
  286. fi
  287. cd $BACKUP
  288. if [ -z $BPATH ]; then
  289. sftpc "get $1" > /dev/null 2>&1
  290. else
  291. sftpc "cd $BPATH" "get $1" > /dev/null 2>&1
  292. fi
  293. }
  294. sftp_delete() {
  295. echo "$1"
  296. source_conf "$HESTIA/conf/sftp.backup.conf"
  297. if [ -z "$PORT" ]; then
  298. PORT='22'
  299. fi
  300. echo $BPATH
  301. if [ -z $BPATH ]; then
  302. sftpc "rm $1" > /dev/null 2>&1
  303. else
  304. sftpc "cd $BPATH" "rm $1" > /dev/null 2>&1
  305. fi
  306. }
  307. sftp_backup() {
  308. # Checking config
  309. if [ ! -e "$HESTIA/conf/sftp.backup.conf" ]; then
  310. error="Can't open sftp.backup.conf"
  311. echo "$error" | $SENDMAIL -s "$subj" $email "yes"
  312. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  313. echo "$error"
  314. errorcode="$E_NOTEXIST"
  315. return "$E_NOTEXIST"
  316. fi
  317. # Parse config
  318. source_conf "$HESTIA/conf/sftp.backup.conf"
  319. # Set default port
  320. if [ -z "$(grep 'PORT=' $HESTIA/conf/sftp.backup.conf)" ]; then
  321. PORT='22'
  322. fi
  323. # Checking variables
  324. if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
  325. error="Can't parse sftp backup configuration"
  326. echo "$error" | $SENDMAIL -s "$subj" $email "yes"
  327. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  328. echo "$error"
  329. errorcode="$E_PARSING"
  330. return "$E_PARSING"
  331. fi
  332. # Debug info
  333. echo -e "$(date "+%F %T") Remote: sftp://$HOST/$BPATH/$user.$backup_new_date.tar" \
  334. | tee -a $BACKUP/$user.log
  335. # Checking network connection and write permissions
  336. if [ -z $BPATH ]; then
  337. sftmpdir="vst.bK76A9SUkt"
  338. else
  339. sftmpdir="$BPATH/vst.bK76A9SUkt"
  340. fi
  341. sftpc "mkdir $BPATH" > /dev/null 2>&1
  342. sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
  343. rc=$?
  344. if [[ "$rc" != 0 ]]; then
  345. case $rc in
  346. $E_CONNECT) error="Can't login to sftp host $HOST" ;;
  347. $E_FTP) error="Can't create temp folder on sftp $HOST" ;;
  348. esac
  349. echo "$error" | $SENDMAIL -s "$subj" $email "yes"
  350. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  351. echo "$error"
  352. errorcode="$rc"
  353. return "$rc"
  354. fi
  355. # Checking retention (Only include .tar files)
  356. if [ -z $BPATH ]; then
  357. backup_list=$(sftpc "ls -l" | awk '{print $9}' | grep "^$user\." | grep ".tar" | sort)
  358. else
  359. backup_list=$(sftpc "cd $BPATH" "ls -l" | awk '{print $9}' | grep "^$user\." | grep ".tar" | sort)
  360. fi
  361. backups_count=$(echo "$backup_list" | wc -l)
  362. if [ "$backups_count" -ge "$BACKUPS" ]; then
  363. backups_rm_number=$((backups_count - BACKUPS + 1))
  364. for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
  365. backup_date=$(echo $backup | sed -e "s/$user.//" -e "s/.tar.*$//")
  366. echo -e "$(date "+%F %T") Rotated sftp backup: $backup_date" \
  367. | tee -a $BACKUP/$user.log
  368. if [ -z $BPATH ]; then
  369. sftpc "rm $backup" > /dev/null 2>&1
  370. else
  371. sftpc "cd $BPATH" "rm $backup" > /dev/null 2>&1
  372. fi
  373. done
  374. fi
  375. # Uploading backup archive
  376. echo "$(date "+%F %T") Uploading $user.$backup_new_date.tar" | tee -a $BACKUP/$user.log
  377. if [ "$localbackup" = 'yes' ]; then
  378. cd $BACKUP
  379. if [ -z $BPATH ]; then
  380. sftpc "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  381. else
  382. sftpc "cd $BPATH" "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  383. fi
  384. else
  385. cd $tmpdir
  386. tar -cf $BACKUP/$user.$backup_new_date.tar .
  387. cd $BACKUP/
  388. if [ -z $BPATH ]; then
  389. sftpc "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  390. else
  391. sftpc "cd $BPATH" "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  392. fi
  393. rm -f $user.$backup_new_date.tar
  394. fi
  395. }
  396. # BackBlaze B2 backup function
  397. b2_backup() {
  398. # Defining backblaze b2 settings
  399. source_conf "$HESTIA/conf/b2.backup.conf"
  400. # Recreate backblaze auth file ~/.b2_account_info (for situation when key was changed in b2.backup.conf)
  401. b2 clear-account > /dev/null 2>&1
  402. b2 authorize-account $B2_KEYID $B2_KEY > /dev/null 2>&1
  403. # Uploading backup archive
  404. echo -e "$(date "+%F %T") Upload to B2: $user/$user.$backup_new_date.tar"
  405. if [ "$localbackup" = 'yes' ]; then
  406. cd $BACKUP
  407. b2 upload-file $BUCKET $user.$backup_new_date.tar $user/$user.$backup_new_date.tar > /dev/null 2>&1
  408. else
  409. cd $tmpdir
  410. tar -cf $BACKUP/$user.$backup_new_date.tar .
  411. cd $BACKUP/
  412. b2 upload-file $BUCKET $user.$backup_new_date.tar $user/$user.$backup_new_date.tar > /dev/null 2>&1
  413. rc=$?
  414. rm -f $user.$backup_new_date.tar
  415. if [ "$rc" -ne 0 ]; then
  416. check_result "$E_CONNECT" "b2 failed to upload $user.$backup_new_date.tar"
  417. fi
  418. fi
  419. # Checking retention
  420. backup_list=$(b2 ls --long $BUCKET $user | cut -f 1 -d ' ' 2> /dev/null)
  421. backups_count=$(echo "$backup_list" | wc -l)
  422. if [ "$backups_count" -ge "$BACKUPS" ]; then
  423. backups_rm_number=$(($backups_count - $BACKUPS))
  424. for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
  425. backup_file_name=$(b2 get-file-info $backup | grep fileName | cut -f 4 -d '"' 2> /dev/null)
  426. echo -e "$(date "+%F %T") Rotated b2 backup: $backup_file_name"
  427. b2 delete-file-version $backup > /dev/null 2>&1
  428. done
  429. fi
  430. }
  431. b2_download() {
  432. # Defining backblaze b2 settings
  433. source_conf "$HESTIA/conf/b2.backup.conf"
  434. # Recreate backblaze auth file ~/.b2_account_info (for situation when key was changed in b2.backup.conf)
  435. b2 clear-account > /dev/null 2>&1
  436. b2 authorize-account $B2_KEYID $B2_KEY > /dev/null 2>&1
  437. cd $BACKUP
  438. b2 download-file-by-name $BUCKET $user/$1 $1 > /dev/null 2>&1
  439. if [ "$?" -ne 0 ]; then
  440. check_result "$E_CONNECT" "b2 failed to download $user.$1"
  441. fi
  442. }
  443. b2_delete() {
  444. # Defining backblaze b2 settings
  445. source_conf "$HESTIA/conf/b2.backup.conf"
  446. # Recreate backblaze auth file ~/.b2_account_info (for situation when key was changed in b2.backup.conf)
  447. b2 clear-account > /dev/null 2>&1
  448. b2 authorize-account $B2_KEYID $B2_KEY > /dev/null 2>&1
  449. b2 delete-file-version $1/$2 > /dev/null 2>&1
  450. }
  451. rclone_backup() {
  452. # Define rclone config
  453. source_conf "$HESTIA/conf/rclone.backup.conf"
  454. echo -e "$(date "+%F %T") Upload With Rclone to $HOST: $user.$backup_new_date.tar"
  455. if [ "$localbackup" != 'yes' ]; then
  456. cd $tmpdir
  457. tar -cf $BACKUP/$user.$backup_new_date.tar .
  458. fi
  459. cd $BACKUP/
  460. if [ -z "$BPATH" ]; then
  461. rclone copy -v $user.$backup_new_date.tar $HOST:$backup
  462. if [ "$?" -ne 0 ]; then
  463. check_result "$E_CONNECT" "Unable to upload backup"
  464. fi
  465. # Only include *.tar files
  466. backup_list=$(rclone lsf $HOST: | cut -d' ' -f1 | grep "^$user\." | grep ".tar" | sort)
  467. backups_count=$(echo "$backup_list" | wc -l)
  468. backups_rm_number=$((backups_count - BACKUPS))
  469. if [ "$backups_count" -ge "$BACKUPS" ]; then
  470. for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
  471. echo "Delete file: $backup"
  472. rclone deletefile $HOST:/$backup
  473. done
  474. fi
  475. else
  476. rclone copy -v $user.$backup_new_date.tar $HOST:$BPATH
  477. if [ "$?" -ne 0 ]; then
  478. check_result "$E_CONNECT" "Unable to upload backup"
  479. fi
  480. # Only include *.tar files
  481. backup_list=$(rclone lsf $HOST:$BPATH | cut -d' ' -f1 | grep "^$user\." | grep ".tar" | sort)
  482. backups_count=$(echo "$backup_list" | wc -l)
  483. backups_rm_number=$(($backups_count - $BACKUPS))
  484. if [ "$backups_count" -ge "$BACKUPS" ]; then
  485. for backup in $(echo "$backup_list" | head -n $backups_rm_number); do
  486. echo "Delete file: $backup"
  487. rclone deletefile $HOST:$BPATH/$backup
  488. done
  489. fi
  490. fi
  491. if [ "$localbackup" != 'yes' ]; then
  492. rm -f $user.$backup_new_date.tar
  493. fi
  494. }
  495. rclone_delete() {
  496. # Defining rclone settings
  497. source_conf "$HESTIA/conf/rclone.backup.conf"
  498. if [ -z "$BPATH" ]; then
  499. rclone deletefile $HOST:/$1
  500. else
  501. rclone deletefile $HOST:$BPATH/$1
  502. fi
  503. }
  504. rclone_download() {
  505. # Defining rclone b2 settings
  506. source_conf "$HESTIA/conf/rclone.backup.conf"
  507. cd $BACKUP
  508. if [ -z "$BPATH" ]; then
  509. rclone copy -v $HOST:/$1 ./
  510. else
  511. rclone copy -v $HOST:$BPATH/$1 ./
  512. fi
  513. }