Advertisement
Guest User

Untitled

a guest
Aug 28th, 2013
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. DAV_SERV="https://webdav.yandex.ru/backup/" # with path and trailing /
  4. DAV_USER="юзер"
  5. DAV_PASS="пароль"
  6.  
  7. DB_HOST="localhost"
  8. DB_USER="root"
  9. DB_PASS="пароль_бд"
  10.  
  11. STORE_PASS="пароль_архивов"                # Pass to encrypt file before uploading
  12.  
  13. # get list of backups
  14. curl --user ${DAV_USER}:${DAV_PASS} -o backup-log.txt ${DAV_SERV}backup-log.txt &> /dev/null
  15.  
  16. # get list of existing databases
  17. DBLIST="$(echo "show databases;" | /usr/bin/mysql -s -h ${DB_HOST} -u ${DB_USER} -p${DB_PASS} | /bin/grep -vE "^(information_schema)$")"    #"
  18.  
  19. # dump, compress and encrypt them
  20. FILES_LIST=""
  21. for DB in ${DBLIST}; do
  22.     echo "Dumping ${DB}."
  23.     FILENAME="$(date +%Y-%m-%d)-DB-${DB}.sql.lzma.gpg"
  24.     /usr/bin/mysqldump -c -h ${DB_HOST} -u ${DB_USER} -p${DB_PASS} --databases ${DB} | \
  25.     /usr/bin/xz -z -9 -q -F lzma --memlimit=150MiB | \
  26.     /usr/bin/gpg --batch -q --no-tty --compress-algo none --cipher-algo AES --passphrase "${STORE_PASS}" -c > ${FILENAME}
  27.     FILES_LIST="${FILES_LIST},${FILENAME}"
  28. done
  29.  
  30. # upload compressed files
  31. FILES_LIST="$(echo "${FILES_LIST}" | sed -r 's/^,(.+)$/\1/')"   # remove unneeded leading ','
  32. #echo "${FILES_LIST}"
  33. echo "Uploading to cloud."
  34. if ! curl --user ${DAV_USER}:${DAV_PASS} -T {${FILES_LIST}} ${DAV_SERV}; then
  35.     echo "Upload failed. removing files and exiting."
  36.     FILES_LIST="$(echo "${FILES_LIST}" | tr ',' ' ')"   # convert ',' to spaces
  37.     rm -f ${FILES_LIST}
  38.     exit
  39. fi
  40.  
  41. # remove compressed files
  42. FILES_LIST="$(echo "${FILES_LIST}" | tr ',' ' ')"   # convert ',' to spaces
  43. echo "Removing ${FILES_LIST}."
  44. rm -f ${FILES_LIST}
  45.  
  46. # parse log and remove old backups
  47. if [ -e backup-log.txt1 ]; then
  48.     rm -f backup-log.txt1
  49.     touch backup-log.txt1
  50. fi
  51.  
  52. echo "Cleaning old backups."
  53. NOW="$(date +%s)"
  54. while read line; do
  55.     FIRSTPART="$(echo "${line}" | cut -f1 -d ' ')"
  56.     TIMESTAMP="$(echo "${line}" | sed -r 's/^([0-9]+) .*/\1/' )"
  57.     if [ ! -z "${TIMESTAMP}" ] && [ "${FIRSTPART}" == "${TIMESTAMP}" ]; then
  58.     let DDIF="NOW - TIMESTAMP"
  59.     if [ "${DDIF}" -gt "2678400" ]; then        # 60 seconds *60*24*31 = 2678400 = month
  60.         # remove file from cloud and skip line
  61.         FILENAME="$(echo "${line}" | cut -f2- -d ' ')"
  62.         if curl --user ${DAV_USER}:${DAV_PASS} --request DELETE "${DAV_SERV}${FILENAME}" &> /dev/null; then
  63.         echo "${FILENAME} removed."
  64.         else
  65.         echo "Can't remove ${FILENAME}."
  66.         echo "${line}" >> backup-log.txt1
  67.         fi
  68.     else
  69.         echo "${line}" >> backup-log.txt1
  70.     fi
  71.     fi
  72. done < backup-log.txt
  73.  
  74. echo "Fixing log."
  75. for FILENAME in ${FILES_LIST}; do
  76.     echo "${NOW} ${FILENAME}" >> backup-log.txt1
  77. done
  78. cat backup-log.txt1 > backup-log.txt
  79. curl --user ${DAV_USER}:${DAV_PASS} -T backup-log.txt ${DAV_SERV}
  80. rm -f backup-log.txt backup-log.txt1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement