backup.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. expect -f "-" <<EOF "$@"
  180. set timeout 60
  181. set count 0
  182. spawn /usr/bin/sftp -o StrictHostKeyChecking=no \
  183. -o Port=$PORT $USERNAME@$HOST
  184. expect {
  185. -nocase "password:" {
  186. send "$PASSWORD\r"
  187. exp_continue
  188. }
  189. -re "Password for (.*)@(.*)" {
  190. send "$password\r"
  191. exp_continue
  192. }
  193. -re "Couldn't|(.*)disconnect|(.*)stalled|(.*)not found" {
  194. set count \$argc
  195. set output "Disconnected."
  196. set rc $E_FTP
  197. exp_continue
  198. }
  199. -re ".*denied.*(publickey|password)." {
  200. set output "Permission denied, wrong publickey or password."
  201. set rc $E_CONNECT
  202. }
  203. -re "\[0-9]*%" {
  204. exp_continue
  205. }
  206. "sftp>" {
  207. if {\$count < \$argc} {
  208. set arg [lindex \$argv \$count]
  209. send "\$arg\r"
  210. incr count
  211. } else {
  212. send "exit\r"
  213. set output "Disconnected."
  214. if {[info exists rc] != 1} {
  215. set rc $OK
  216. }
  217. }
  218. exp_continue
  219. }
  220. timeout {
  221. set output "Connection timeout."
  222. set rc $E_CONNECT
  223. }
  224. }
  225. if {[info exists output] == 1} {
  226. puts "\$output"
  227. }
  228. exit \$rc
  229. EOF
  230. }
  231. # SFTP backup download function
  232. sftp_download() {
  233. source_conf "$HESTIA/conf/sftp.backup.conf"
  234. if [ -z "$PORT" ]; then
  235. PORT='22'
  236. fi
  237. cd $BACKUP
  238. if [ -z $BPATH ]; then
  239. sftpc "get $1" > /dev/null 2>&1
  240. else
  241. sftpc "cd $BPATH" "get $1" > /dev/null 2>&1
  242. fi
  243. }
  244. sftp_delete() {
  245. echo "$1"
  246. source_conf "$HESTIA/conf/sftp.backup.conf"
  247. if [ -z "$PORT" ]; then
  248. PORT='22'
  249. fi
  250. echo $BPATH
  251. if [ -z $BPATH ]; then
  252. sftpc "rm $1" > /dev/null 2>&1
  253. else
  254. sftpc "cd $BPATH" "rm $1" > /dev/null 2>&1
  255. fi
  256. }
  257. sftp_backup() {
  258. # Checking config
  259. if [ ! -e "$HESTIA/conf/sftp.backup.conf" ]; then
  260. error="Can't open sftp.backup.conf"
  261. echo "$error" |$SENDMAIL -s "$subj" $email "yes"
  262. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  263. echo "$error"
  264. errorcode="$E_NOTEXIST"
  265. return "$E_NOTEXIST"
  266. fi
  267. # Parse config
  268. source_conf "$HESTIA/conf/sftp.backup.conf"
  269. # Set default port
  270. if [ -z "$(grep 'PORT=' $HESTIA/conf/sftp.backup.conf)" ]; then
  271. PORT='22'
  272. fi
  273. # Checking variables
  274. if [ -z "$HOST" ] || [ -z "$USERNAME" ] || [ -z "$PASSWORD" ]; then
  275. error="Can't parse sftp backup configuration"
  276. echo "$error" |$SENDMAIL -s "$subj" $email "yes"
  277. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  278. echo "$error"
  279. errorcode="$E_PARSING"
  280. return "$E_PARSING"
  281. fi
  282. # Debug info
  283. echo -e "$(date "+%F %T") Remote: sftp://$HOST/$BPATH/$user.$backup_new_date.tar" |\
  284. tee -a $BACKUP/$user.log
  285. # Checking network connection and write permissions
  286. if [ -z $BPATH ]; then
  287. sftmpdir="vst.bK76A9SUkt"
  288. else
  289. sftmpdir="$BPATH/vst.bK76A9SUkt"
  290. fi
  291. sftpc "mkdir $BPATH" > /dev/null 2>&1
  292. sftpc "mkdir $sftmpdir" "rmdir $sftmpdir" > /dev/null 2>&1
  293. rc=$?
  294. if [[ "$rc" != 0 ]]; then
  295. case $rc in
  296. $E_CONNECT) error="Can't login to sftp host $HOST" ;;
  297. $E_FTP) error="Can't create temp folder on sftp $HOST" ;;
  298. esac
  299. echo "$error" |$SENDMAIL -s "$subj" $email "yes"
  300. sed -i "/ $user /d" $HESTIA/data/queue/backup.pipe
  301. echo "$error"
  302. errorcode="$rc"
  303. return "$rc"
  304. fi
  305. # Checking retention
  306. if [ -z $BPATH ]; then
  307. backup_list=$(sftpc "ls -l" |awk '{print $9}'|grep "^$user\.")
  308. else
  309. backup_list=$(sftpc "cd $BPATH" "ls -l" |awk '{print $9}'|grep "^$user\.")
  310. fi
  311. backups_count=$(echo "$backup_list" |wc -l)
  312. if [ "$backups_count" -ge "$BACKUPS" ]; then
  313. backups_rm_number=$((backups_count - BACKUPS + 1))
  314. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  315. backup_date=$(echo $backup |sed -e "s/$user.//" -e "s/.tar.*$//")
  316. echo -e "$(date "+%F %T") Rotated sftp backup: $backup_date" |\
  317. tee -a $BACKUP/$user.log
  318. if [ -z $BPATH ]; then
  319. sftpc "rm $backup" > /dev/null 2>&1
  320. else
  321. sftpc "cd $BPATH" "rm $backup" > /dev/null 2>&1
  322. fi
  323. done
  324. fi
  325. # Uploading backup archive
  326. echo "$(date "+%F %T") Uploading $user.$backup_new_date.tar"|tee -a $BACKUP/$user.log
  327. if [ "$localbackup" = 'yes' ]; then
  328. cd $BACKUP
  329. if [ -z $BPATH ]; then
  330. sftpc "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  331. else
  332. sftpc "cd $BPATH" "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  333. fi
  334. else
  335. cd $tmpdir
  336. tar -cf $BACKUP/$user.$backup_new_date.tar .
  337. cd $BACKUP/
  338. if [ -z $BPATH ]; then
  339. sftpc "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  340. else
  341. sftpc "cd $BPATH" "put $user.$backup_new_date.tar" "chmod 0600 $user.$backup_new_date.tar" > /dev/null 2>&1
  342. fi
  343. rm -f $user.$backup_new_date.tar
  344. fi
  345. }
  346. # Google backup download function
  347. google_backup() {
  348. # Defining google settings
  349. source_conf "$HESTIA/conf/google.backup.conf"
  350. gsutil="$HESTIA/3rdparty/gsutil/gsutil"
  351. export BOTO_CONFIG="$HESTIA/conf/.google.backup.boto"
  352. # Debug info
  353. echo -e "$(date "+%F %T") Remote: gs://$BUCKET/$BPATH/$user.$backup_new_date.tar"
  354. # Checking retention
  355. backup_list=$(${gsutil} ls gs://$BUCKET/$BPATH/$user.* 2>/dev/null)
  356. backups_count=$(echo "$backup_list" |wc -l)
  357. if [ "$backups_count" -ge "$BACKUPS" ]; then
  358. backups_rm_number=$((backups_count - BACKUPS))
  359. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  360. echo -e "$(date "+%F %T") Rotated gcp backup: $backup"
  361. $gsutil rm $backup > /dev/null 2>&1
  362. done
  363. fi
  364. # Uploading backup archive
  365. echo -e "$(date "+%F %T") Uploading $user.$backup_new_date.tar ..."
  366. if [ "$localbackup" = 'yes' ]; then
  367. cd $BACKUP
  368. ${gsutil} cp $user.$backup_new_date.tar gs://$BUCKET/$BPATH/ > /dev/null 2>&1
  369. else
  370. cd $tmpdir
  371. tar -cf $BACKUP/$user.$backup_new_date.tar .
  372. cd $BACKUP/
  373. ${gsutil} cp $user.$backup_new_date.tar gs://$BUCKET/$BPATH/ > /dev/null 2>&1
  374. rc=$?
  375. rm -f $user.$backup_new_date.tar
  376. if [ "$rc" -ne 0 ]; then
  377. check_result "$E_CONNECT" "gsutil failed to upload $user.$backup_new_date.tar"
  378. fi
  379. fi
  380. }
  381. google_download() {
  382. source_conf "$HESTIA/conf/google.backup.conf"
  383. gsutil="$HESTIA/3rdparty/gsutil/gsutil"
  384. export BOTO_CONFIG="$HESTIA/conf/.google.backup.boto"
  385. ${gsutil} cp gs://$BUCKET/$BPATH/$1 $BACKUP/ > /dev/null 2>&1
  386. if [ "$?" -ne 0 ]; then
  387. check_result "$E_CONNECT" "gsutil failed to download $1"
  388. fi
  389. }
  390. # BackBlaze B2 backup function
  391. b2_backup() {
  392. # Defining backblaze b2 settings
  393. source_conf "$HESTIA/conf/b2.backup.conf"
  394. # Recreate backblaze auth file ~/.b2_account_info (for situation when key was changed in b2.backup.conf)
  395. b2 clear-account > /dev/null 2>&1
  396. b2 authorize-account $B2_KEYID $B2_KEY > /dev/null 2>&1
  397. # Uploading backup archive
  398. echo -e "$(date "+%F %T") Upload to B2: $user/$user.$backup_new_date.tar"
  399. if [ "$localbackup" = 'yes' ]; then
  400. cd $BACKUP
  401. b2 upload-file $BUCKET $user.$backup_new_date.tar $user/$user.$backup_new_date.tar > /dev/null 2>&1
  402. else
  403. cd $tmpdir
  404. tar -cf $BACKUP/$user.$backup_new_date.tar .
  405. cd $BACKUP/
  406. b2 upload-file $BUCKET $user.$backup_new_date.tar $user/$user.$backup_new_date.tar > /dev/null 2>&1
  407. rc=$?
  408. rm -f $user.$backup_new_date.tar
  409. if [ "$rc" -ne 0 ]; then
  410. check_result "$E_CONNECT" "b2 failed to upload $user.$backup_new_date.tar"
  411. fi
  412. fi
  413. # Checking retention
  414. backup_list=$(b2 ls --long $BUCKET $user | cut -f 1 -d ' ' 2>/dev/null)
  415. backups_count=$(echo "$backup_list" |wc -l)
  416. if [ "$backups_count" -ge "$BACKUPS" ]; then
  417. backups_rm_number=$((backups_count - BACKUPS))
  418. for backup in $(echo "$backup_list" |head -n $backups_rm_number); do
  419. backup_file_name=$(b2 get-file-info $backup | grep fileName | cut -f 4 -d '"' 2>/dev/null)
  420. echo -e "$(date "+%F %T") Rotated b2 backup: $backup_file_name"
  421. b2 delete-file-version $backup > /dev/null 2>&1
  422. done
  423. fi
  424. }
  425. b2_download() {
  426. # Defining backblaze b2 settings
  427. source_conf "$HESTIA/conf/b2.backup.conf"
  428. # Recreate backblaze auth file ~/.b2_account_info (for situation when key was changed in b2.backup.conf)
  429. b2 clear-account > /dev/null 2>&1
  430. b2 authorize-account $B2_KEYID $B2_KEY > /dev/null 2>&1
  431. cd $BACKUP
  432. b2 download-file-by-name $BUCKET $user/$1 $1 > /dev/null 2>&1
  433. if [ "$?" -ne 0 ]; then
  434. check_result "$E_CONNECT" "b2 failed to download $user.$1"
  435. fi
  436. }
  437. b2_delete(){
  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. b2 delete-file-version $1/$2 > /dev/null 2>&1
  444. }