For a system administrator, we need to delete the old backup files or upload files. We can finish this task manually if you only have a few server backup, but as the number of servers grows, we will need to automate this.
Automatically upload to FTP
#!/bin/bash TODAY=`date +%d%b%Y` HOST=192.168.10.200 USER=username PASSWORD=password ftp -inv $HOST <<EOF user $USER $PASSWORD put /tmp/"Website"$TODAY"db.tar.gz" ./Website"$TODAY"db.tar.gz" put /tmp/"Website"$TODAY"web.tar.gz" ./Website"$TODAY"web.tar.gz" bye EOF
553 Permission denied error
in the first place, I used command put /tmp/”Website”$TODAY”db.tar.gz” to upload the file, and got the below error:
553 /tmp/Website01Sep2020db.tar.gz: Permission denied.
The issue is FTP tried to use the same path on the remote FTP server, which is /tmp/Website01Sep2020db.tar.gz.
To avoid this error, I changed the command into put /tmp/”Website”$TODAY”web.tar.gz” ./Website”$TODAY”web.tar.gz”, which upload it into the user’s root FTP directory.
Delete old backups
In this example, the backup for the server (named monitoring, a virtual machine sitting in Hyper-v) is backuped on weekly basis on Saturday and named as the date format “YEARMonthDay.zip” under the machines/monitoring folder in the FTP server. we only keep the most recent 3 copies,
The script (/var/scripts/webbackup_ftp_upload.sh) is like this:
HOST='ftp_server_hostname' USER='xxxxx' PASSWD='xxxxx' YEAR="$(date +"%Y")" MONTH="$(date +"%m")" ###Use GNU date, we can get the date of 21 days ago by following command DAY="$(date +"%d" -d "21 days ago")" FILE="$YEAR$MONTH$DAY.zip" names='monitoring logging db' for name in $names do ftp -n $HOST << END_SCRIPT quote user $USER quote PASS $PASSWD mdelete machines/monitoring/$FILE quit END_SCRIPT done exit 0
Script explained:
- We save the hostname, username, password into variables.
- store year, month, and day in variables.
- FTP log on from script: Getting the password to the FTP server without having the FTP client program read the password from /dev/tty requires two tricks:
- Using the -n option on the FTP client program to prevent the FTP client from trying to log in immediately. That way, the FTP client does not ask for a user ID and password. No use of /dev/tty.
- Use the FTP client program command quote to send a user ID and password to the FTP server.
- ftp -n $HOST << END_SCRIP means, from next line until the line “END_SCRIPT” are FTP commands.
- The mdelete will delete the files.
Then add the script to crontab, and make it work on Saturday after the backup, therefore, we keep 3 backups.
To give the script enough permission, use sudo crontab -e and add below line:
0 1 * * 6 /var/scripts/webbackup_ftp_upload.sh
To be improved
This script only deletes the file which is 3 weeks old, does not delete the older files, hence we need to delete the older ones manually, but luckily only once.
Tried to list the file names in the backup folder on the FTP server by ls command, but was getting a line of file info ( permission, created date, etc), needs to figure out how to only grab the names of the files and then we can loop through those files and delete the older ones.
Reference
http://tldp.org/LDP/abs/html/here-docs.html
http://www.stratigery.com/scripting.ftp.html
Bash – Get Time: https://www.cyberciti.biz/faq/unix-linux-bash-get-time/