How to Automatic Cleanup of Older Backups or Transferring Backups to Remote Locations?
To make the backup script clean up old backups and send them to other places automatically, you can change it. Here's a new version of the script that has automatic cleanup and moves backups to a different server using rsync
:
#!/bin/bash
# Define backup directories and locations
local_backup_dir="/path/to/local/backup/folder"
remote_backup_dir="user@remote-server:/path/to/remote/backup/folder"
source_dirs=("/path/to/source1" "/path/to/source2")
# Number of backups to keep locally and remotely
local_backup_count=7
remote_backup_count=7
# Create a timestamp for the backup
timestamp=$(date +"%Y%m%d%H%M%S")
backup_file="backup_$timestamp.tar.gz"
# Create the backup directory if it doesn't exist
mkdir -p "$local_backup_dir"
# Create a compressed tarball containing the specified directories
tar -czvf "$local_backup_dir/$backup_file" "${source_dirs[@]}"
# Check if the backup was successful
if [ $? -eq 0 ]; then
echo "Local backup completed successfully. Backup stored in: $local_backup_dir/$backup_file"
# Remove old local backups if exceeding the specified count
local_backups=("$local_backup_dir"/*)
if [ ${#local_backups[@]} -gt $local_backup_count ]; then
num_to_remove=$(( ${#local_backups[@]} - local_backup_count ))
for ((i = 0; i < num_to_remove; i++)); do
rm "${local_backups[i]}"
done
echo "Removed $num_to_remove old local backups."
fi
# Transfer the local backup to the remote server using rsync
rsync -av --remove-source-files "$local_backup_dir/$backup_file" "$remote_backup_dir"
echo "Backup transferred to remote server."
# Remove old remote backups if exceeding the specified count
remote_backups=$(ssh "$remote_backup_dir" ls -t | tail -n +$remote_backup_count)
if [ -n "$remote_backups" ]; then
ssh "$remote_backup_dir" "rm -f $remote_backups"
echo "Removed old backups on the remote server."
else
echo "No old backups found on the remote server."
fi
else
echo "Local backup failed."
fi
In this extended script:
local_backup_count
andremote_backup_count
variables determine how many backups you want to retain locally and on the remote server.After creating the local backup, it checks and removes old backups exceeding the specified count.
It uses
rsync
to transfer the local backup to the remote server while removing the source file.It also checks and removes old backups on the remote server to ensure you don't exceed the specified count.
Make sure to change the placeholder paths, remote server information, and counts to your real settings. Also, ensure you've set up passwordless SSH access to the remote server so you can use rsync
and ssh
without entering a password.