I'm trying to create a bash script to perform some maintenance tasks on a remote server using ssh. I've been trying to figure it out on my own, but I'm having some difficulty. I was hoping someone here on Stack Overflow might be able to help me out.
I'm not very familiar with bash scripting, so any guidance or suggestions you could provide would be greatly appreciated.
Here's what I'm trying to accomplish:
- Connect to a remote server via ssh
- Delete files that are older than 3 months
- Update server packages
- Check disk sizes and send a notification if the disk is 75% full
Commands:
- SSH into the remote server.
- Update server packages.
- Delete files older than 3 months.
- Check disk sizes and send notification if 75% full.
# maintenance.sh
# Connect to the remote server
ssh username@remote_server_ip "cd /"
# Update server packages
ssh username@server "apt-get update && apt-get upgrade"
# Delete files older than 3 months
ssh username@server "find home/ -mtime +90 -delete"
# Check disk sizes and send notification if 75% full
ssh username@server "disk_size=$(df -h / | grep -v Filesystem | awk '{print $5}')
if [[ $disk_size > 75 ]]; then
echo 'Low disk space on server. Disk usage is currently at $disk_size%' | mail -s 'Low Disk $ip a Alert' admin@example.com "
Problems:
- The find command doesn't seem to be working as intended. It's not deleting any files.
codemastermind@ThinkPad-X230:~/scripts$ ./maintenance.sh
find: 'home/': No such file or directory
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
codemastermind@ThinkPad-X230:~/scripts$