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") #DB Credentials user="ROOT_OR EQUIVALENT_USER_NAME" password="PASSWORD_GOES_HERE" host="MYSQL_HOST_NAME" db_name="MY_WEBSITE_DATABASE_NAME" # Set default file permissions cd $backup_path mkdir $date $date/code_dump $date/db_dump #Take website code backup tar cvzf $backup_path/$date/code_dump/MY_WEBSITE_CONTENT.tar.gz /var/www/MY_WEBSITE_PATH/ # DB backup mysqldump --user=$user --password=$password --host=$host $db_name >$backup_path/$date/db_dump/$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
What next?
You can either download the zip file from a file transfer software.
Note
- This script is designed for PHP/Python applications having MySQL running on a Linux server.
- For other types, refer other online source.