How to Create and Run a Shell Script with Crontab on a Linux Server Introduction

Automating tasks on a Linux server helps save time and ensures consistency. An effective way to achieve this is by creating a shell script (.sh) and scheduling it with crontab. In this article, we will guide you through creating a backup script and setting it up to run automatically on a Linux server.

What You’ll Learn

  • How to write a shell script for data backups.
  • How to schedule it using crontab.
  • Best practices for managing backups on Linux.

Step 1: Writing the Shell Script

Below is an example of a shell script that performs database and file backups:

#!/bin/bash
# Create backup directory
backup_dir="/home/backup/$(date +'%Y-%m')"
mkdir -p $backup_dir
# Backup database
cd /home/source && ./vendor/bin/drush sql-dump > "$backup_dir/db_$(date +'%Y-%m-%dT%H-%M').sql.gz" --gzip
cd /home/source && ./vendor/bin/drush sql-dump > "$backup_dir/db_shared_$(date +'%Y-%m-%dT%H-%M').sql.gz" --gzip
# Backup files directory
rsync -av --backup --suffix=.bak /home/source/web/sites/default/files/ /home/backup/files
# Delete backups older than 10 days
find /home/backup/* -type d -mtime +10 -exec rm -r {} \;

Explanation:

  • #!/bin/bash: Declares this as a Bash script.
  • mkdir -p: Creates a directory based on the year and month if it doesn’t exist.
  • drush sql-dump: Exports the database and compresses it with gzip.
  • rsync: Synchronizes files to the backup directory with a .bak suffix for overwritten files.
  • find: Deletes backups older than 10 days.

Save this script as backup.sh and make it executable:

chmod +x backup.sh

Step 2: Scheduling with Crontab

To automate the script, use crontab. Open the crontab file:

crontab -e
Add the following line to run the script daily at 2 AM:
0 2 * * * /home/backup.sh

Crontab Syntax:

  • 0 2 * * *: Runs at 2:00 AM every day (minute, hour, day of month, month, day of week).
  • /home/backup.sh: Path to the script.

Save and exit. Verify the schedule with:

crontab -l

Step 3: Testing the Script

Run the script manually to ensure it works:

./backup.sh

Check the backup directory (/home/backup) to see the generated files.

Best Practices

  • Log Output: Redirect script output to a log file (>> /var/log/backup.log 2>&1).
  • Permissions: Ensure the script has appropriate execution rights.
  • Storage: Monitor disk space to avoid filling up the server.

Conclusion

By following these steps, you can automate backups on a Linux server using a shell script and crontab. This method is efficient, reliable, and widely used by system administrators.

Keywords: Linux backup script, shell script crontab, automate tasks Linux, crontab scheduling, server backup tutorial.


SEO Notes

  • Primary Keywords: "Linux backup script", "shell script crontab", "script sao lưu Linux", "lập lịch crontab".
  • Secondary Keywords: "automate tasks Linux", "server backup tutorial", "tự động hóa Linux", "hướng dẫn sao lưu server".
  • Structure: Use headings (H1, H2), short paragraphs, and bullet points to improve readability.
  • Internal Links: If you have a blog/site, add links to related articles about Linux or crontab.

Working Contact Form with Ajax & PHP

Get a functional and working contact form with Ajax & PHP in a few minutes. Just copy and paste the files, add a little code and you’re done.

Download Now