backup.sh 14 KB

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