Advertisement
ceskyDJ

Local MySQL database backup

Dec 6th, 2018
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.72 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Script for backing up local MySQL database
  4. # It's good to use it with Cron tasks
  5. # Author: Michal ŠMAHEL (ceskyDJ) <ceskyDJ@seznam.cz>
  6. # Copyright: (C) Michal ŠMAHEL (ceskyDJ) 2018
  7.  
  8. # Control vars defaults definition
  9. force=false;
  10. remove=false;
  11. time=false;
  12. user="root";
  13. password="";
  14. validity="31";
  15.  
  16. # Load config from arguments
  17. i=0; # Iteration
  18. skip=false; # Sign for skip next argument (because of value)
  19.  
  20. for arg in $@;
  21. do
  22.     if [ $skip == true ]; then
  23.         skip=false;
  24.         i=$(echo "$i + 1"|bc);
  25.         continue;
  26.     fi
  27.  
  28.     case $arg in
  29.     # Display help
  30.     "-h" | "--help")
  31.         echo "Usage: $0 DIRECTORY [OPTIONS...]";
  32.        
  33.         echo -e "\nCommand structure:";
  34.         echo -e "  DIRECTORY\t\t\tdirectory for saving backups";
  35.         echo -e "  OPTIONS\t\t\tadditional arguments";
  36.    
  37.         echo -e "\nAvailable arguments:";
  38.         echo -e "  -f, --force\t\t\tforce rewrite backup file";
  39.         echo -e "  -r, --remove\t\t\tremove old backup files (after month)";
  40.         echo -e "  -t, --time\t\t\tadd time to backup file names";
  41.         echo -e "  -u USER, --user=USER\t\tset database user (with full read permissions) - default: 'root'";
  42.         echo -e "  -p PASS, --password=PASS\tset password for database user - default: ''";
  43.         echo -e "  -v DAYS, --validity=DAYS\tset backup file validity in days (useful only with remove argument) - default: '31'";
  44.         echo -e "  -h, --help\t\t\tdisplay this help";
  45.    
  46.         exit;
  47.         ;;
  48.     # Allow backup rewriting
  49.     "-f" | "--force")
  50.         force=true;
  51.         ;;
  52.     # Allow old backup files removing
  53.     "-r" | "--remove")
  54.         remove=true;
  55.         ;;
  56.     # Adding time
  57.     "-t" | "--time")
  58.         time=true;
  59.         ;;
  60.     # User change
  61.     "-u")
  62.         user=${@:$i+2:1}; # +1 (it starts from 1), +1 (move to next iteration index)
  63.         skip=true;
  64.         ;;
  65.     --user=*)
  66.         user=$(echo $arg | cut -b 8-);
  67.         ;;
  68.     # Password change
  69.     "-p")
  70.         password=${@:$i+2:1}; # +1 (it starts from 1), +1 (move to next iteration index)
  71.         skip=true;
  72.         ;;
  73.     --password=*)
  74.         password=$(echo $arg | cut -b 12-);
  75.         ;;
  76.     # Backup validity change
  77.     "-v")
  78.         validity=${@:$i+2:1}; # +1 (it starts from 1), +1 (move to next iteration index)
  79.         skip=true;
  80.         ;;
  81.     --validity=*)
  82.         validity=$(echo $arg | cut -b 12-);
  83.         ;;
  84.     # Bad argument
  85.     *)
  86.         # First argument + valid directory
  87.         if [ $i == 0 ] && [ -d $arg ]; then
  88.             dir=$arg;
  89.         # First argument + not existing directory
  90.         elif [ $i == 0 ] && [ ! -d $arg ]; then
  91.             echo "Directory you specified is wrong. Enter valid directory or use -h or --help argument for display help.";
  92.            
  93.             exit;
  94.         # Second and next argument
  95.         else
  96.             # Invalid argument
  97.             echo "You entered bad argument '$arg'. Try -h or --help argument to display help.";
  98.        
  99.             exit;
  100.         fi
  101.         ;;
  102.     esac
  103.    
  104.     i=$(echo "$i + 1"|bc);
  105. done
  106.  
  107. # Directory verification
  108. if [ ! $dir ]; then
  109.     echo "Directory you specified is wrong. Enter valid directory or use -h or --help argument for display help.";
  110.    
  111.     exit;
  112. fi
  113.  
  114. # Date and time format setting
  115. if [ $time == true ]; then
  116.     format="%Y-%m-%d_%T";
  117. else
  118.     format="%Y-%m-%d";
  119. fi
  120.  
  121. # Backup creation
  122. backupFile="$dir/database_`date +$format`.sql.gz";
  123.  
  124. # Only if backup wasn't created (or rewrite is allowed)
  125. if [ $force == true ] || [ ! -f $backupFile ]; then
  126.     mysqldump -u $user --password=$password --all-databases | gzip > $backupFile
  127. fi
  128.  
  129. # Old backups removing
  130. if [ $remove == true ]; then
  131.     for file in $dir/*.sql.gz; do
  132.         length=$(expr length $dir); # -> length of directory string
  133.    
  134.         start=$(echo "11 + $length"|bc); # -> start index
  135.         end=$(echo "20 + $length"|bc); # -> end index
  136.         dateString=$(echo $file | cut -b $start-$end); # -> YYYY-MM-DD 
  137.    
  138.         dateFormat=$(date -d $dateString +"%Y%m%d"); # -> YYYYMMDD
  139.    
  140.         minimum=$(date -d -$validity+"day" +"%Y%m%d");
  141.  
  142.         if [ $minimum -ge $dateFormat ]; then
  143.             rm $file;
  144.         fi
  145.     done
  146. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement