Created
October 4, 2019 03:30
-
-
Save jonom/4360b889cc298135c667c9bcae4b881b to your computer and use it in GitHub Desktop.
CloudWays - bash script to download local backups for all applications on a server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# CloudWays - Download local backups for all applications | |
# ======================================================= | |
# * Local backups must be enabled and available. See https://support.cloudways.com/how-to-download-a-full-backup/ | |
# * Add your machine's SSH key to your server so this script needs no input. | |
# * Backups will be saved inside a new folder with today's date within your nominated backup directory. (This might not be the date the backup was taken.) | |
# * Only tested on Digital Ocean VPS so far. | |
# Configuration | |
username='YOUR_MASTER_CREDENTIALS_USERNAME' | |
serverIp='YOUR_SERVER_IP' | |
backupDir='LOCAL_BACKUP_DIRECTORY_PATH' # Example: '~/CloudWays/Backups' | |
# Get the application IDs to backup | |
echo "Retrieving application IDs..." | |
apps=($(ssh $username@$serverIp ls /home/master/applications/)) | |
appCount=${#apps[@]} | |
if ! (($appCount > 0)) ; then | |
echo "😱 Download failed. Could not retrieve application IDs." | |
exit | |
fi | |
echo "$appCount applications found." | |
# Create and switch to the backup directory | |
dir="$backupDir/$(date '+%F')" | |
if ! mkdir "$dir" ; then | |
echo "😱 Download failed. Could not create backup directory." | |
exit | |
fi | |
cd "$dir" | |
# Copy each app backup separately. Not as elegant as a single scp command but should be fine. | |
doneCount=0 | |
for i in "${!apps[@]}" | |
do | |
appId="${apps[$i]}" | |
num=$(($i + 1)) | |
echo "[$num of $appCount] Downloading $appId..." | |
if scp "$username@$serverIp:/home/master/applications/$appId/local_backups/backup.tgz" "./$appId.tgz" ; then | |
echo "👍 Done." | |
doneCount=$(($doneCount + 1)) | |
else | |
echo "🙁 Could not download $appId." | |
fi | |
done | |
# Final report | |
if (($doneCount == 0)) ; then | |
echo "😱 All downloads failed." | |
elif (($doneCount == $appCount)) ; then | |
echo "😊 All downloads succeeded." | |
else | |
echo "🤨 Some downloads failed." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment