1) Create a new text file with a . sh extension.
2) Add #!/bin/bash to the top of it. This is necessary for the “make it executable” part.
3) Add lines that you’d normally type at the command line.
4) At the command line, run chmod u+x YourScriptFileName.sh.
5) Run it whenever you need!
Run MySQL database backup script
This is a bash script so first you need to create a file
nano your_file_name.sh
put this code inside it
# Backup storage directory
backup_folder=/var/backups
# Notification email address
recipient_email=<username@mail.com>
# MySQL user
user=<user_name>
# MySQL password
password=<password>
# Number of days to store the backup
keep_day=30
sqlfile=$backup_folder/all-database-$(date +%d-%m-%Y_%H-%M-%S).sql
zipfile=$backup_folder/all-database-$(date +%d-%m-%Y_%H-%M-%S).zip
# Create a backup
sudo mysqldump -u $user -p$password --all-databases > $sqlfile
if [ $? == 0 ]; then
echo 'Sql dump created'
else
echo 'mysqldump return non-zero code' | mailx -s 'No backup was created!' $recipient_email
exit
fi
# Compress backup
zip $zipfile $sqlfile
if [ $? == 0 ]; then
echo 'The backup was successfully compressed'
else
echo 'Error compressing backup' | mailx -s 'Backup was not created!' $recipient_email
exit
fi
rm $sqlfile
echo $zipfile | mailx -s 'Backup was successfully created' $recipient_email
# Delete old backups
#find $backupfolder -mtime +$keep_day -delete
find $backup_folder -mtime +$keep_day -delete
before you run the script you need to replace words with ‘<>’ for a example your database username, password etc. (don’t forget to remove all the < and >)
then you need to install this library package to send email when it’s done.
sudo apt-get install bsd-mailx
the you can run the script by triggering this command
bash ./your_script_file.sh
Run your script weekly basis
This way your script will automatically run every week at 8.07am.
Installing Cron
Almost every Linux distribution has some form of cron
installed by default. However, if you’re using an Ubuntu machine on which cron
isn’t installed, you can install it using APT.
Before installing cron
on an Ubuntu machine, update the computer’s local package index:
sudo apt update
Then install cron
with the following command:
sudo apt install cron
You’ll need to make sure it’s set to run in the background too:
sudo systemctl enable cron
OutputSynchronizing state of cron.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable cron
Following that, cron
will be installed on your system and ready for you to start scheduling jobs.
Understanding How Cron Works
Cron jobs are recorded and managed in a special file known as a crontab
. Each user profile on the system can have their own crontab
where they can schedule jobs, which is stored under /var/spool/cron/crontabs/
.
To schedule a job, open up your crontab
for editing and add a task written in the form of a cron expression. The syntax for cron expressions can be broken down into two elements: the schedule and the command to run.
The command can be virtually any command you would normally run on the command line. The schedule component of the syntax is broken down into 5 different fields, which are written in the following order:
Field | Allowed Values |
---|---|
minute | 0-59 |
hour | 0-23 |
Day of the month | 1-31 |
month | 1-12 or JAN-DEC |
Day of the week | 0-6 or SUN-SAT |
Together, tasks scheduled in a crontab are structured like the following:
minute hour day_of_month month day_of_week command_to_run
Here’s a functional example of a cron expression. This expression runs the command curl http://www.google.com
every Tuesday at 5:30 PM:
30 17 * * 2 curl http://www.google.com
There are also a few special characters you can include in the schedule component of a cron expression to streamline scheduling tasks:
*
: In cron expressions, an asterisk is a wildcard variable that represents “all.” Thus, a task scheduled with* * * * * ...
will run every minute of every hour of every day of every month.,
: Commas break up scheduling values to form a list. If you want to have a task run at the beginning and middle of every hour, rather than writing out two separate tasks (e.g.,0 * * * * ...
and30 * * * * ...
), you could achieve the same functionality with one (0,30 * * * * ...
).-
: A hyphen represents a range of values in the schedule field. Instead of having 30 separate scheduled tasks for a command you want to run for the first 30 minutes of every hour (as in0 * * * * ...
,1 * * * * ...
,2 * * * * ...
, and so on), instead, you could schedule it as0-29 * * * * ...
./
: You can use a forward slash with an asterisk to express a step value. For example, instead of writing out eight separate separatecron
tasks to run a command every three hours (as in,0 0 * * * ...
,0 3 * * * ...
,0 6 * * * ...
, and so on), you could schedule it to run like this:0 */3 * * * ...
.
Note: You cannot express step values arbitrarily; you can only use integers that divide evenly into the range allowed by the field in question. For instance, in the “hours” field you could only follow a forward slash with 1
, 2
, 3
, 4
, 6
, 8
, or 12
.
Here are some more examples of how to use cron’s scheduling component:
* * * * *
– Run the command every minute.12 * * * *
– Run the command 12 minutes after every hour.0,15,30,45 * * * *
– Run the command every 15 minutes.*/15 * * * *
– Run the command every 15 minutes.0 4 * * *
– Run the command every day at 4:00 AM.0 4 * * 2-4
– Run the command every Tuesday, Wednesday, and Thursday at 4:00 AM.20,40 */8 * 7-12 *
– Run the command on the 20th and 40th minute of every 8th hour every day of the last 6 months of the year.
If you find any of this confusing or if you’d like help writing schedules for your own cron
tasks, Cronitor provides a handy cron
schedule expression editor named “Crontab Guru” which you can use to check whether your cron
schedules are valid.
Managing Crontabs
Once you’ve settled on a schedule and you know the job you want to run, you’ll need to put it somewhere your daemon will be able to read it.
As mentioned previously, a crontab
is a special file that holds the schedule of jobs cron
will run. However, these are not intended to be edited directly. Instead, it’s recommended that you use the crontab
command. This allows you to edit your user profile’s crontab
without changing your privileges with sudo
. The crontab
command will also let you know if you have syntax errors in the crontab
, while editing it directly will not.
You can edit your crontab
with the following command:
crontab -e
go to the bottom of that file and paste this line
7 8 * * 0 /root/yourfilename.sh
press ctrl+x and press y
now everything is ready to work.
Other useful commands
Install MySQL on ubuntu server
apt-get install phpmyadmin php-mbstring gettext -y
Would you like to make a donation for supporting us
Here you go