Advertisement
Guest User

Untitled

a guest
Dec 17th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.68 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. MAX_VERSIONS=5
  4.  
  5. BASE_DIR="/home/backy/sw-backups"
  6.  
  7. DB_BACKUPS="$BASE_DIR/db"
  8. DIR_BACKUPS="$BASE_DIR/dir"
  9.  
  10. MODE="0600"
  11. OWNER="backy"
  12. EXCLUDE_LIST="/etc/backup.exclude"
  13.  
  14. mkdir -p "$DB_BACKUPS" "$DIR_BACKUPS"
  15. chown -R "$OWNER" "$DB_BACKUPS" "$DIR_BACKUPS"
  16.  
  17. rotate(){
  18.     local dir="$1"
  19.     local pattern="$2"
  20.     local versions="$3"
  21.  
  22.     local files=($(find "$dir" -name "$pattern" -printf "%C@ %p\n" | sort -n | cut -d' ' -f2-))
  23.     while [[ "${#files[@]}" -ge "$versions" ]]
  24.     do
  25.         rm -rf "${files[0]}"
  26.         unset files[0]
  27.         backups=( "${files[@]}" )
  28.     done
  29. }
  30.  
  31. store_compressed(){
  32.     local dir="$1"
  33.     local name="$2"
  34.     local ext="$3"
  35.     local versions="$4"
  36.     shift 4
  37.  
  38.     local tmpfile="`mktemp`"
  39.  
  40.     $@ | bzip2 -9 -c > "$tmpfile"
  41.  
  42.     [[ "${PIPESTATUS[0]}" -eq 0 ]] || {
  43.         rm -f "$tmpfile"
  44.         return 1
  45.     }
  46.  
  47.     rotate "$dir" "${name}_*$ext" "$versions"
  48.  
  49.     local bname="$dir/${name}_`date +"%d.%m.%Y_%H.%M"`.$ext"
  50.     mv "$tmpfile" "$bname" || {
  51.         rm -f "$tmpfile"
  52.         return 1
  53.     }
  54.  
  55.     chmod "$MODE" "$bname"
  56.     chown "$OWNER" "$bname"
  57. }
  58.  
  59. backup_db(){
  60.     local dbname="$1"
  61.     local user="$2"
  62.     local password="$3"
  63.  
  64.     store_compressed "$DB_BACKUPS" "$dbname" "sql.bz2" "$MAX_VERSIONS" mysqldump -u "$user" --password="$password" -B "$dbname"
  65. }
  66.  
  67. backup_dir(){
  68.     local dir="$1"
  69.     local parent="`dirname "$dir"`"
  70.     local fname="`basename "$dir"`"
  71.  
  72.     store_compressed "$DIR_BACKUPS" "$fname" "tar.bz2" "$MAX_VERSIONS" tar -X "$EXCLUDE_LIST" -C "$parent" -cf - "$fname"
  73. }
  74.  
  75. backup_db dbname dbuser dbpassword
  76. backup_db dbname dbuser dbpassword
  77.  
  78. backup_dir /home/site1/public_html
  79. backup_dir /home/site1/public_html
  80.  
  81. # now run the syncer
  82. su - backy -c "sh -c 'cd dropbox-syncer && ruby cron.rb'"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement