Skip to main content

Automate Linux Backups with Bash Scripting and Cron Jobs

HardReset.InfoJanuary 1, 202611 min41 views
2 connections·4 entities in this video→

Setting Up Backup Environment

  • πŸ“‚ Create a 'data' folder in the home directory and populate it with two sample files, file1.txt and file2.txt.
  • πŸ—„οΈ Establish a 'backup' directory in the home directory to store the automated backup archives.

Developing the Backup Script (backup.sh)

  • ✍️ Create a Bash script named backup.sh using a text editor like nano.
  • ⏰ Define a date variable to include the year, month, day, hour, and minute in the backup filename for precise timestamping.
  • πŸ“‚ Specify the source directory (e.g., /home/data) and the destination directory (e.g., /home/backup) within the script.
  • πŸ“¦ Utilize the tar command with czf options to create a compressed archive (.tar.gz) of the source data, incorporating the date variable into the filename.

Script Execution and Verification

  • πŸš€ Make the script executable using chmod +x backup.sh.
  • βœ… Manually test the script by running ./backup.sh and verify that a timestamped .tar.gz file is created in the backup directory.
  • πŸ” Extract a backup archive to confirm that the original files (file1.txt, file2.txt) are present within the archive.

Scheduling Backups with Cron

  • βš™οΈ Edit the cron table by running crontab -e.
  • πŸ“… Schedule the backup script to run automatically at desired intervals (e.g., daily at 5 AM: 0 5 * * * /home/backup.sh, or every minute for testing: * * * * * /home/backup.sh).
  • πŸ“œ Verify cron job execution by checking system logs, specifically using sudo grep cron /var/log/syslog, to confirm that the backup script has run at the scheduled times.
  • πŸ“Š Observe the creation of multiple backup files in the backup directory, confirming the automation is working as expected.
Knowledge graph4 entities Β· 2 connections

How they connect

An interactive map of every person, idea, and reference from this conversation. Hover to trace connections, click to explore.

Hover Β· drag to explore
4 entities
Chapters4 moments

Key Moments

Transcript39 segments

Full Transcript

Topics12 themes

What’s Discussed

LinuxBash ScriptingCron JobsAutomated BackupsTar CommandFile CompressionTimestampingExecutable PermissionsCrontabSystem LogsLog VerificationShell Scripting
Smart Objects4 Β· 2 links
ProductsΒ· 2
ConceptΒ· 1
LocationΒ· 1