backup.sh 15 KB

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