Shell script to take a database backup.
To avoid Windows EOL Use Notepad++.
In Notepad++: Edit > EOL Conversion > UNIX/OSX Format
then type the above script.
Shell Command (SSH)
To run a shell script: sh filename.sh
To open file in read mode with EOL format: cat -A filename.sh
To create a file: touch filename.format
To edit a file: pico filename.format (After editing Ctrl+X -> Enter -> Y)
#!/bin/sh
#----------------------------------------------------
# MySQL Database backup script; which the created file to an another server.
# Created on: 28 Oct, 2016.
# Version: 2
#----------------------------------------------------
FILE=/home/username/public_html/backup_dir/my_db_file.sql.`date +"%Y%m%d"`
# (1) set up all the mysqldump variables
DBSERVER=db_host
DATABASE=db_name
USER=db_user
PASS=db_password
# (2) in case you run this more than once a day, remove the previous version of the file
rm -f "$FILE" "$FILE.gz"
# (3) do the mysql database backup (dump)
# use this command for a database server on a separate host:
#mysqldump --opt --protocol=TCP --user=${USER} --password=${PASS} --host=${DBSERVER} ${DATABASE} > ${FILE}
# use this command for a database server on localhost. add other options if need be.
mysqldump --opt --user=${USER} --password=${PASS} ${DATABASE} > ${FILE}
# (4) gzip the mysql database dump file
gzip $FILE
# (5) show the user the result
echo "${FILE}.gz was created."
#ls -l ${FILE}.gz
#$ scp -r /var/www/db-backup/${FILE}.gz your_username@remotehost.edu:/some/remote/directory/bar