Client Area
File Backup
Backup files and put them in a tar/bzip2 file.
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:
  1. # Grab todays date
  2. d=$(date +%m_%d_%Y)
  3. # Dumps a MySQL database into a file. Remember to replace YOUR_USERNAME with your username
  4. # and YOUR_PASSWORD with your actual password and the same with DATABASE_NAME
  5. # The $d in the database file name is the date we got above.
  6. # Put the database in the same folder as the files you are backing up.
  7. mysqldump -u YOUR_USERNAME --password=YOUR_PASSWORD DATABASE_NAME > /path/to/files/to/be/backed/up/database_$d.sql
  8.  
  9. # Put all the files (including the .sql file we just created) into a bz2 file (zip file basically)
  10. # Notice the $d in there as well.
  11. tar -cjf /where/to/save/backup/BACKUP_NAME-$d.tar.bz2 /path/to/files/to/be/backed/up
  12.  
  13. # Remove the .sql file we just created so we dont have a long list of sql dumps taking up space
  14. 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
Twitter: