├── .github └── ISSUE_TEMPLATE │ └── bug_report.md ├── LICENSE ├── keep-mega-alive.bat ├── keep-mega-alive.sh └── README.md /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug:** 11 | A clear and concise description of what the bug is. 12 | 13 | **Steps to Reproduce:** 14 | Steps to reproduce the behavior: 15 | 1. Run '...' 16 | 17 | **Expected behavior:** 18 | A clear and concise description of what you expected to happen. 19 | 20 | **Details:** 21 | - Version: (run `keep-mega-alive.sh --version`) 22 | - OS: [e.g. macOS 11.3.1] 23 | - Shell [e.g. Bash, ZSH] (run `echo $SHELL`) 24 | 25 | **Log:** 26 | Include the contents of the `~/keep-mega-alive.log` log file here. 27 | 28 | **Additional context:** 29 | Add any other context about the problem here. 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 3ncod3 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /keep-mega-alive.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | @rem Keep-MEGA-Alive 3 | @rem https://github.com/3ncod3/keep-mega-alive 4 | 5 | setlocal EnableDelayedExpansion 6 | set VERSION=1.2 7 | 8 | if "%~1"=="--version" ( 9 | echo Keep-MEGA-Alive v%VERSION% 10 | goto :eof 11 | ) 12 | 13 | where /q mega-version 14 | if %errorlevel% neq 0 ( 15 | echo Error: MEGAcmd is not installed. Get it from https://mega.io/cmd 16 | goto :eof 17 | ) 18 | 19 | if "%~1" == "" ( 20 | set "LOGINS=mega-logins.csv" 21 | ) else ( 22 | set "LOGINS=%1" 23 | ) 24 | 25 | call MEGAcmdServer & /dev/null 26 | call mega-logout >NUL 27 | call :log 28 | call :log Starting Keep-MEGA-Alive v%VERSION% 29 | 30 | for /F "tokens=1,2" %%i in (%LOGINS%) do ( 31 | call :process %%i %%j > keep-mega-alive.tmp 2>&1 32 | type keep-mega-alive.tmp >> keep-mega-alive.log 33 | type keep-mega-alive.tmp 34 | ) 35 | 36 | goto :cleanup 37 | 38 | 39 | 40 | :log 41 | echo [%DATE% %TIME%] %* >> keep-mega-alive.log 42 | goto :eof 43 | 44 | :process 45 | call :log Trying to login as %1 46 | echo %1 47 | call mega-login %1 %2 && ( 48 | call :log Successfully logged in as %1 49 | ) || ( 50 | call :log [ERROR] Unable to login as %1 51 | ) 52 | call mega-df -h 53 | call mega-logout >NUL 54 | call :log Logged out from %1 55 | echo. 56 | goto :eof 57 | 58 | :cleanup 59 | del keep-mega-alive.tmp 60 | call :log Finished running Keep-MEGA-Alive 61 | pause 62 | 63 | :eof 64 | -------------------------------------------------------------------------------- /keep-mega-alive.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Keep-MEGA-Alive 4 | # https://github.com/3ncod3/keep-mega-alive 5 | 6 | VERSION=1.3 7 | LOGINS=${1:-"mega-logins.csv"} 8 | LOGFILE="$HOME/keep-mega-alive.log" 9 | 10 | if [[ $1 == "--version" ]]; then 11 | echo "Keep-MEGA-Alive v$VERSION" 12 | exit 0 13 | fi 14 | 15 | # Check if MEGAcmd is installed 16 | if ! [ -x "$(command -v mega-version)" ]; then 17 | echo 'Error: MEGAcmd is not installed. Get it from https://mega.io/cmd' >&2 18 | exit 1 19 | fi 20 | 21 | # Check if login file exists and is readable 22 | if [ ! -r "$LOGINS" ]; then 23 | echo "Error: Login file $LOGINS does not exist or is not readable" >&2 24 | exit 1 25 | fi 26 | 27 | # Open log file for writing 28 | exec 4>>"$LOGFILE" 29 | exec 5> >(tee -a "$LOGFILE") 2>&1 30 | 31 | # Log start message 32 | echo -e "$(date +"%Y-%m-%dT%H:%M:%S%:z") : Starting Keep-MEGA-Alive v$VERSION" >&4 33 | 34 | mega-logout 1>/dev/null 35 | 36 | # Loop through login file 37 | while IFS="," read -r username password; do 38 | # Trim whitespace from username and password 39 | username=$(echo "$username" | tr -d '[:space:]') 40 | password=$(echo "$password" | tr -d '[:space:]') 41 | 42 | # Skip line if username or password is empty 43 | if [ -z "$username" ] || [ -z "$password" ]; then 44 | echo "Warning: Invalid login credentials in login file, skipping line" >&2 45 | continue 46 | fi 47 | 48 | echo -e "\n>>> Logging in as $username" >&5 49 | 50 | # Attempt login 51 | if ! mega-login "$username" "$password" >&5; then 52 | echo "Error: Login failed for user $username" >&2 53 | continue 54 | fi 55 | 56 | echo "Successfully logged in as $username" 57 | 58 | # Get account information 59 | mega-df -h >&5 60 | 61 | # Log out 62 | mega-logout >&5 63 | echo "Logged out from $username" 64 | sleep 10 65 | 66 | done < "$LOGINS" 67 | 68 | # Log finish message and close log file 69 | echo -e "$(date +"%Y-%m-%dT%H:%M:%S%:z") : Finished running Keep-MEGA-Alive\n" >&4 70 | exec 4>&- 71 | exec 5>&- 72 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Keep-MEGA-Alive 2 | 3 | A script to keep your mega account(s) alive and print their used storage info 4 | (`df -h`). 5 | 6 | Automatically logs every run in the `~/keep-mega-alive.log` file (since v1.2) 7 | ## Installation 8 | 9 | ### 1. Install MEGAcmd 10 | 11 | Get it from https://mega.io/cmd or using APT 12 | 13 | #### Using APT (Debian/Ubuntu) 14 | 15 | The advantage of doing it this way is that updates of MEGAcmd will be 16 | automatically fetched and installed when you upgrade your packages. 17 | 18 | Add the MEGA signing key for the repository 19 | 20 | ```sh 21 | curl -fsSL https://mega.nz/keys/MEGA_signing.key | sudo apt-key add - 22 | ``` 23 | 24 | Add the repo, replace `` with your OS version path found under 25 | https://mega.nz/linux/MEGAsync/ 26 | 27 | ```sh 28 | sudo echo "deb https://mega.nz/linux/MEGAsync// ./" > /etc/apt/sources.list.d/mega-nz.list 29 | ``` 30 | 31 | Then just install it 32 | 33 | ```sh 34 | sudo apt update 35 | sudo apt install megacmd 36 | ``` 37 | 38 | ### 2. Download the script 39 | 40 | From your home directory (`~`), download the latest version of the script and 41 | make it executable 42 | 43 | ```sh 44 | curl -O https://raw.githubusercontent.com/3ncod3/keep-mega-alive/main/keep-mega-alive.sh 45 | chmod u+x keep-mega-alive.sh 46 | ``` 47 | 48 | ### 3. Create the logins file 49 | 50 | Create a `mega-logins.csv` CSV file with your mega logins, with each email and 51 | password being separated by a comma and on a separate line, under your home 52 | directory like so: 53 | 54 | ```csv 55 | example1@example.com,password1 56 | example2@example.com,password2 57 | example3@example.com,password3 58 | ``` 59 | 60 | ### Windows 61 | 62 | For Windows, download and use the `keep-mega-alive.bat` file instead of the 63 | `keep-mega-alive.sh` file everywhere. 64 | 65 | ``` 66 | https://raw.githubusercontent.com/3ncod3/keep-mega-alive/main/keep-mega-alive.bat 67 | ``` 68 | 69 | ## Upgrade 70 | 71 | Upgrade your current version of the script by simply re-downloading it 72 | 73 | ```sh 74 | curl -O https://raw.githubusercontent.com/3ncod3/keep-mega-alive/main/keep-mega-alive.sh 75 | chmod u+x keep-mega-alive.sh 76 | ``` 77 | 78 | ## Usage 79 | 80 | Once you have created `mega-logins.csv` in your home directory and the script is 81 | executable (see Installation), just run it: 82 | 83 | ```sh 84 | ~/keep-mega-alive.sh 85 | ``` 86 | 87 | ### Specify logins file path 88 | 89 | By default, the script is going look for the `mega-logins.csv` file under the 90 | same directory the script resides under but you can specify a path to this file 91 | like so: 92 | 93 | ```sh 94 | ~/keep-mega-alive.sh path/to/logins-file.csv 95 | ``` 96 | 97 | ### Schedule regular runs 98 | 99 | You can use [`crontab`](https://linux.die.net/man/5/crontab) to schedule the 100 | script to run at a regular interval by adding an entry to your cronfile (run 101 | `crontab -e`). 102 | 103 | #### Run every month 104 | 105 | ```sh 106 | 0 0 1 * * path/to/keep-mega-alive.sh &>/dev/null 107 | ``` 108 | 109 | #### Run every other month 110 | 111 | ```sh 112 | 0 0 1 */2 * path/to/keep-mega-alive.sh &>/dev/null 113 | ``` 114 | 115 | #### Run every 3rd month 116 | 117 | ```sh 118 | 0 0 1 */3 * path/to/keep-mega-alive.sh &>/dev/null 119 | ``` 120 | 121 | ### Parse log file for login errors 122 | 123 | The script logs everything in the file `keep-mega-alive` in your home directory. If you want to look at unsuccessful login attempts run 124 | 125 | ```sh 126 | cat ~/keep-mega-alive.log | grep ERROR 127 | ``` 128 | 129 | ### Version 130 | 131 | Find your script version by running 132 | 133 | ```sh 134 | ~/keep-mega-alive.sh --version 135 | ``` --------------------------------------------------------------------------------