backup.sh 15 KB

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