This tutorial will guide you through creating a script to compress a folder, upload it to Google Drive using rclone
, and schedule this task to run automatically every 12 hours on an linux server.
- Access to an Ubuntu 22.04 server.
- Google Drive account.
rclone
installed and configured.- Administrator permissions to install packages and configure
crontab
.
Before starting, install the zip
utility if it's not already installed:
sudo apt update
sudo apt install zip -y
Set up rclone
to access Google Drive:
-
Run the configuration command:
rclone config
-
Follow the instructions to create a new remote configuration for Google Drive.
-
Make sure to authenticate
rclone
with your Google account.
Create a script named backup_volume.sh
with the following content:
#!/bin/bash
SOURCE_DIRECTORY="/home/anythingllm/volume"
BACKUP_DIRECTORY="/home/rclone/backuptmp"
REMOTE_NAME="DockerBackup"
REMOTE_PATH="server/anythingllm"
DATE=$(date +%Y-%m-%d)
mkdir -p "$BACKUP_DIRECTORY"
ZIP_FILE="${BACKUP_DIRECTORY}/volume_backup_${DATE}.zip"
zip -r "$ZIP_FILE" "$SOURCE_DIRECTORY"
if [ -f "$ZIP_FILE" ]; then
echo "Zip file created successfully: $ZIP_FILE"
rclone copy "$ZIP_FILE" "$REMOTE_NAME:$REMOTE_PATH"
if [ $? -eq 0 ]; then
echo "Zip file uploaded successfully to Google Drive: $REMOTE_NAME:$REMOTE_PATH"
rm "$ZIP_FILE"
echo "Local zip file removed: $ZIP_FILE"
else
echo "Error uploading zip file to Google Drive"
fi
else
echo "Error creating zip file"
fi
Make the script executable with the following command:
chmod +x /home/rclone/script/backup_volume.sh
Schedule the script to run every 12 hours:
-
Open
crontab
for editing:crontab -e
-
Add the following line to the file:
0 */12 * * * /bin/bash /home/rclone/script/backup_volume.sh
-
Save and close the editor.
Your backup script is now set up to run automatically every 12 hours, compressing the specified folder and uploading the zip file to Google Drive. Be sure to periodically check the logs to ensure everything is functioning as expected.