| Description: | Several months ago I was adding SVN to my local server. When importing and learning how to use SVN I managed to delete the latest copy of my FetchMerchant application. So I decided to create a bash script that I put on a cronjob to back up the files everynight along with an SQL dump. |
| OS: | Linux/Unix |
| Requirements: | Bash, MySQL |
Source Code:
# Grab todays date d=$(date +%m_%d_%Y) # Dumps a MySQL database into a file. Remember to replace YOUR_USERNAME with your username # and YOUR_PASSWORD with your actual password and the same with DATABASE_NAME # The $d in the database file name is the date we got above. # Put the database in the same folder as the files you are backing up. mysqldump -u YOUR_USERNAME --password=YOUR_PASSWORD DATABASE_NAME > /path/to/files/to/be/backed/up/database_$d.sql # Put all the files (including the .sql file we just created) into a bz2 file (zip file basically) # Notice the $d in there as well. tar -cjf /where/to/save/backup/BACKUP_NAME-$d.tar.bz2 /path/to/files/to/be/backed/up # Remove the .sql file we just created so we dont have a long list of sql dumps taking up space rm -f /path/to/files/to/be/backed/up/database_$d.sql
Example:
# Please note: The username/passwords and file paths are just an example. # You should modify them to suit your needs. d=$(date +%m_%d_%Y) mysqldump -u jeff --password=jeffrocks kreitsoft > /home/kreitsoft/public_html/database_$d.sql tar -cjf /home/kreitsoft/backup/kreitsoftwebsite-$d.tar.bz2 /home/kreitsoft.com/public_html rm -f /home/kreitsoft/public_html/database_$d.sql