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.txtandfile2.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.shusing a text editor likenano. - β° 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
tarcommand withczfoptions 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.shand verify that a timestamped.tar.gzfile 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