Here's a script that performs a full system backup on a Linux system:
#!/bin/bash
# Set the backup destination directory
BACKUP_DIR="/path/to/backup/directory"
# Get the current date and time
DATE=$(date +%Y-%m-%d_%H-%M-%S)
# Create a new directory for the backup
BACKUP_DEST="$BACKUP_DIR/backup_$DATE"
mkdir -p "$BACKUP_DEST"
# Backup all files using tar
tar -cpvzf "$BACKUP_DEST/full_system_backup.tar.gz" --exclude="$BACKUP_DIR" /
# Print the backup completion message
echo "Full system backup completed successfully."
echo "Backup location: $BACKUP_DEST/full_system_backup.tar.gz"
Here's what the script does:
-
We set the BACKUP_DIR variable to the directory where you want to store the backups. Replace /path/to/backup/directory with the desired backup location.
-
We get the current date and time using the date command and store it in the DATE variable. The format is set to YYYY-MM-DD_HH-MM-SS.
-
We create a new directory for the backup using the mkdir command. The directory name includes the current date and time to keep each backup separate.
-
We use the tar command to create a compressed archive of the entire system. The options used are:
-c: Create a new archive.
-p: Preserve file permissions.
-v: Verbose mode to display the progress.
-z: Compress the archive using gzip.
-f: Specify the output file name.
--exclude: Exclude the backup directory itself from the backup to avoid recursion.
The / at the end specifies that we want to backup the entire system starting from the root directory.
-
Finally, we print a completion message indicating that the backup has been successfully created and provide the location of the backup file.
To use this script:
-
Save the script to a file with a .sh extension (e.g., full_system_backup.sh).
-
Make the script executable by running the following command:
chmod +x full_system_backup.sh
-
Replace /path/to/backup/directory in the script with the desired backup location.
-
Run the script with sudo or as the root user to ensure it has the necessary permissions to access all system files:
sudo ./full_system_backup.sh
The script will create a compressed tar archive of the entire system in the specified backup directory, with a timestamp in the filename.
Please note that backing up the entire system may take a considerable amount of time and disk space, depending on the size of your system and the amount of data you have. Make sure you have sufficient storage space in the backup directory before running the script.