youni

Backup scripts. Send database, sync files to remote server

Dec 9th, 2020 (edited)
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.53 KB | None | 0 0
  1. # Youni's backup scripts.
  2. # Create backups of database & files and send them to remote server.
  3. # Cron runs scripts regularly. Twice a day db, once a day files.
  4. # Remote server should make backup of files periodically and also delete old db backups (not realised here).
  5. # Bash, scp, ssh, rsync, cron
  6.  
  7. # Database backup script /home/myhostuser/db_dump
  8.  
  9. #!/bin/sh
  10.  
  11. #configuration
  12.  
  13. host_address=85.85.85.58
  14. host_user=mysshuser
  15. host_port=9955
  16. host_dir=~/dumps
  17.  
  18. mysql_user=mytestuser
  19. mysql_pass=mycoolpass
  20. mysql_database=mytestdb
  21.  
  22.  
  23. #code starts here
  24.  
  25. fname=dump_$(date +%x_%H-%M-%S).sql
  26. tarfname=$fname.tar.gz
  27.  
  28. echo $(date +%x_%H-%M-%S)
  29. echo "Making mysql dump $fname"
  30.  
  31. mysqldump -u $mysql_user -p"$mysql_pass" $mysql_database > $fname
  32.  
  33. tar czf $tarfname $fname
  34.  
  35. echo "Uploading mysql dump to server"
  36.  
  37. scp -P$host_port  $tarfname  $host_user@$host_address:$host_dir
  38.  
  39. echo "Removing dump files from local machine"
  40. rm $fname $tarfname
  41.  
  42.  
  43.  
  44. # cron job:
  45.  
  46. # 00 03,15 * * * /home/myhostuser/db_dump >> /var/log/db_dump.log 2>&1
  47.  
  48.  
  49.  
  50. # Files backup with rsync /home/myhostuser/f_dump
  51.  
  52. #~/bin/sh
  53.  
  54. #configuration
  55.  
  56. host_address=85.85.85.58
  57. host_user=mysshuser
  58. host_port=9955
  59. host_dir=~/dumps/var/example
  60.  
  61. files_dir=/var/example
  62.  
  63.  
  64. #code starts here
  65.  
  66. date "+%d.%m.%Y %H:%M:%S"
  67.  
  68. echo "Syncing new and modified files with remote server"
  69.  
  70. rsync --rsh="ssh -p$host_port" -auv $files_dir -p $host_port $host_user@$host_address:$host_dir
  71.  
  72.  
  73. # cron job:
  74.  
  75. # 30 03 * * * /home/myhostuser/f_dump >> /var/log/f_dump.log 2>&1
  76.  
Advertisement
Add Comment
Please, Sign In to add comment