Often times, you need an automated script to take backup of your web application and database hosted on your Linux server. Instead of executing many commands, combine those commands into one shell script file. In this blog, we have given a shell script that will be useful to you.
This is a shell script. You have to save this as backup.sh and run this on your server.
#!/bin/bash # options backup_path="/var/www/BACKUP_FOLDER_PATH" date=$(date +"%Y-%m-%d_%H-%M") # Set default file permissions cd $backup_path mkdir $date #Take website code backup tar cvzf $backup_path/$date/MY_WEBSITE_CONTENT.tar.gz /var/www/MY_WEBSITE_PATH/ #DB Backup user="ROOT_OR EQUIVALENT_USER_NAME" password="PASSWORD_GOES_HERE" host="MYSQL_HOST_NAME" db_name="MY_WEBSITE_DATABASE_NAME" mysqldump --user=$user --password=$password --host=$host $db_name >$backup_path/$date/$db_name.sql #combine .sql and website content into one tar ball tar cvzf $date.tar.gz $date # clean up folder rm -rf $date
What will this script do?
This script will make a temporary folder with the name of date and time in your specified backup path. It will zip the website content using tar command. Then it will take backup of database and save it with .sql file extension. After this, the script will again zip the folder created above, containing code and database. After zipping, it will delete the temporary folder created.
After execution of this script, your website will be backed up and available as a .tar.gz file
You can either download the zip file from a file transfer software.
- This script is designed for PHP/Python/Perl applications having MySQL running on a Linux server.
- For other types, refer other online source.